import imaplib mail = imaplib.IMAP4_SSL('imap.gmail.com') mail.login('eagletiger1@gmail.com', '********') mail.list() # Out: list of "folders" aka labels in gmail. mail.select("inbox") # connect to inbox. result, data = mail.uid('search', None, '(FROM "danielsammons@gmail.com")') # search and return uids instead latest_email_uid = data[0].split()[-1] result, data = mail.uid('fetch', latest_email_uid, '(RFC822)') raw_email = data[0][1] import email email_message = email.message_from_string(raw_email) print email_message['To'] print email.utils.parseaddr(email_message['From']) maintype = email_message.get_content_maintype() if maintype == 'multipart': for part in email_message.get_payload(): if part.get_content_maintype() == 'text': print part.get_payload() break elif maintype == 'text': email_message.get_payload()
import imaplib mail = imaplib.IMAP4_SSL('pod51004.outlook.com') mail.login('lchen@ecsu.edu', '********') mail.list() # Out: list of "folders" aka labels in gmail. mail.select("inbox") # connect to inbox. result, data = mail.uid('search', None, '(FROM "lchen@ecsu.edu")') # search and return uids instead #result, data = mail.uid('search', None, '(FROM "danielsammons@gmail.com")') # search and return uids instead latest_email_uid = data[0].split()[-1] result, data = mail.uid('fetch', latest_email_uid, '(RFC822)') raw_email = data[0][1] import email email_message = email.message_from_string(raw_email) print email_message['To'] print email.utils.parseaddr(email_message['From']) # for parsing "Yuji Tomita"maintype = email_message.get_content_maintype() if maintype == 'multipart': for part in email_message.get_payload(): if part.get_content_maintype() == 'text': print part.get_payload() break elif maintype == 'text': email_message.get_payload()