SHARE
TWEET

Untitled

a guest May 23rd, 2018 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import imaplib
  2. import email
  3. import os
  4.  
  5. username = 'wnotest.test123'
  6. password = 'Test1234!'
  7. mail = imaplib.IMAP4_SSL('imap.gmail.com')
  8. mail.login(username, password)
  9. mail.list()
  10. # Out: list of "folders" aka labels in gmail.
  11. mail.select("inbox")  # connect to inbox.
  12.  
  13. result, data = mail.search(None, "ALL")
  14.  
  15. ids = data[0]  # data is a list.
  16. id_list = ids.split()  # ids is a space separated string
  17. latest_email_id = id_list[-1]  # get the latest
  18.  
  19. result, data = mail.fetch(latest_email_id, "(RFC822)")  # fetch the email body (RFC822) for the given ID
  20.  
  21. raw_email = data[0][1]  # here's the body, which is raw text of the whole email
  22. print(raw_email)
  23.  
  24. m = email.message_from_bytes(raw_email)
  25.  
  26. for part in m.walk():
  27.     if part.get_content_maintype() == 'multipart':
  28.         continue
  29.     if part.get('Content-Disposition') is None:
  30.         continue
  31.  
  32.     filename = part.get_filename()
  33.     if filename is not None:
  34.         sv_path = os.path.join("c:/Projekty/Git", filename)
  35.         if not os.path.isfile(sv_path):
  36.             print
  37.             sv_path
  38.             fp = open(sv_path, 'wb')
  39.             fp.write(part.get_payload(decode=True))
  40.             fp.close()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top