Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python, Imap & Gmail

Python, Imap & Gmail

Shrayas Rajagopal

April 25, 2015
Tweet

More Decks by Shrayas Rajagopal

Other Decks in Technology

Transcript

  1. Chennaipy
    APRIL 2015

    View full-size slide

  2. Python
    Imap
    & Gmail

    View full-size slide

  3. @shrayasr
    Logic Soft

    View full-size slide

  4. Email
    POP3 IMAP

    View full-size slide

  5. POP3
    Post Office Protocol

    View full-size slide

  6. SIMPLE
    POP3
    Post Office Protocol

    View full-size slide

  7. SIMPLE ONE MAILBOX
    POP3
    Post Office Protocol

    View full-size slide

  8. SIMPLE
    SSL/TLS
    ONE MAILBOX
    POP3
    Post Office Protocol

    View full-size slide

  9. SIMPLE
    SSL/TLS
    ONE MAILBOX
    MOVE
    POP3
    Post Office Protocol

    View full-size slide

  10. SIMPLE
    SSL/TLS
    ONE MAILBOX
    MOVE ALL PARTS
    POP3
    Post Office Protocol

    View full-size slide

  11. IMAP
    Internet Msg Access Protocol

    View full-size slide

  12. IMAP
    Internet Msg Access Protocol
    ASYNC

    View full-size slide

  13. IMAP
    Internet Msg Access Protocol
    ASYNC MAILBOXes

    View full-size slide

  14. IMAP
    Internet Msg Access Protocol
    ASYNC CLIENTS
    MAILBOXes

    View full-size slide

  15. IMAP
    Internet Msg Access Protocol
    ASYNC CLIENTS
    MAILBOXes
    SEARCHES

    View full-size slide

  16. IMAP
    Internet Msg Access Protocol
    ASYNC CLIENTS
    MAILBOXes
    SEARCHES EXTENSIONS

    View full-size slide

  17. Gmail
    @gmail.com

    View full-size slide

  18. Settings > Forwarding and POP/IMAP
    /settings/security/lesssecureapps
    Requirements

    View full-size slide

  19. >>>  import  imaplib  

    View full-size slide

  20. >>>  import  imaplib  
    Standard Library

    View full-size slide

  21. Check the count
    of mails in my INBOX
    1

    View full-size slide

  22. email_count.py
    import imaplib
    client = IMAP4_SSL("imap.gmail.com", "993")
    client.login("[email protected]","barfoo")
    result, response = client.select("INBOX")
    if result == "OK":
    print "count is %s" % response[0]

    View full-size slide

  23. Gmail
    Handling folders

    View full-size slide

  24. “[Gmail]/”  
    Prefix

    View full-size slide

  25. [Gmail]/Spam  
    SPAM

    View full-size slide

  26. [Gmail]/Drafts  
    DRAFTS

    View full-size slide

  27. Gmail IMAP
    EXT ENSIONS!

    View full-size slide

  28. Gmail IMAP
    EXTENSIONS!

    View full-size slide

  29. Mails sent to Vijay
    from 11 Apr to date
    2

    View full-size slide

  30. to:  ()  after:  before:    

    View full-size slide

  31. SEARCH X-­‐GM-­‐RAW  
    IMAP
    Gmail

    View full-size slide

  32. Mails sent to Vijay
    from 11 Apr to date
    2

    View full-size slide

  33. # ...
    client.select("[Gmail]/Sent Mail")
    client.search(None, '(X-GM-RAW
    "to: ([email protected])
    after:2015/4/11
    before:2015/4/26")')
    # ...
    advanced_search.py

    View full-size slide

  34. # ...
    client.select("[Gmail]/Sent Mail")
    client.search(None, '(AND
    (TO "[email protected]")
    (FROM "[email protected]"))')
    # ...
    imap_search.py

    View full-size slide

  35. Add label “Foo” to all
    mails sent to me from
    [email protected]
    3

    View full-size slide

  36. STORE X-­‐GM-­‐LABELS  
    IMAP
    Gmail

    View full-size slide

  37. # ...
    client.select("INBOX")
    emails = client.search(None, '(FROM
    "[email protected]")')
    for email in emails[1][0].split():
    client.store(email, '+X-GM-LABELS', "foo")
    # ...
    add_foo_label.py

    View full-size slide

  38. •  IMAP, POP3

    View full-size slide

  39. •  IMAP, POP3
    •  imaplib  

    View full-size slide

  40. •  IMAP, POP3
    •  imaplib  
    •  Gmail IMAP extensions
             X-­‐GM-­‐RAW,  X-­‐GM-­‐LABELS  

    View full-size slide

  41. /Fin
    @shrayasr

    View full-size slide