fetch email from gmail using python site 3astackoverflow com

Solutions on MaxInterview for fetch email from gmail using python site 3astackoverflow com by the best coders in the world

showing results for - "fetch email from gmail using python site 3astackoverflow com"
Erika
26 Jan 2021
1import imaplib
2import email
3import datetime
4import time
5
6mail = imaplib.IMAP4_SSL('imap.gmail.com', 993)
7mail.login(user, password)
8mail.list()
9mail.select('inbox')
10
11status, data = mail.search(None, 'ALL')
12for num in data[0].split():
13    status, data = mail.fetch(num, '(RFC822)')
14    email_msg = data[0][1]
15    email_msg = email.message_from_bytes(email_msg)
16    maintype = email_msg.get_content_maintype()
17    if maintype == 'multipart':
18        for part in email_msg.get_payload():
19            if part.get_content_maintype() == 'text':
20                print(part.get_payload())
21    elif maintype == 'text':
22        print(email_msg.get_payload())
23