Advertisement
Guest User

Untitled

a guest
May 28th, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. import imaplib
  2. mail = imaplib.IMAP4_SSL('imap.gmail.com')
  3. mail.login('email', 'password')
  4. mail.list()
  5. # Out: list of "folders" aka labels in gmail.
  6. mail.select("inbox") # connect to inbox.
  7.  
  8. result, data = mail.search(None, "ALL")
  9.  
  10. ids = data[0] # data is a list.
  11. id_list = ids.split() # ids is a space separated string
  12. latest_email_id = id_list[-1] # get the latest
  13.  
  14. result, data = mail.fetch(latest_email_id, "(RFC822)") # fetch the email body (RFC822) for the given ID
  15.  
  16. raw_email = data[0][1] # here's the body, which is raw text of the whole email
  17. # including headers and alternate payloads
  18.  
  19. print(raw_email)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement