a guest May 23rd, 2018 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- import imaplib
- import email
- import os
- username = 'wnotest.test123'
- password = 'Test1234!'
- mail = imaplib.IMAP4_SSL('imap.gmail.com')
- mail.login(username, password)
- # Out: list of "folders" aka labels in gmail.
- mail.select("inbox") # connect to inbox.
- result, data = mail.search(None, "ALL")
- ids = data # data is a list.
- id_list = ids.split() # ids is a space separated string
- latest_email_id = id_list[-1] # get the latest
- result, data = mail.fetch(latest_email_id, "(RFC822)") # fetch the email body (RFC822) for the given ID
- raw_email = data # here's the body, which is raw text of the whole email
- m = email.message_from_bytes(raw_email)
- for part in m.walk():
- if part.get_content_maintype() == 'multipart':
- if part.get('Content-Disposition') is None:
- filename = part.get_filename()
- if filename is not None:
- sv_path = os.path.join("c:/Projekty/Git", filename)
- if not os.path.isfile(sv_path):
- fp = open(sv_path, 'wb')
RAW Paste Data