Skip to content

Instantly share code, notes, and snippets.

@anupamchugh
Created August 31, 2020 21:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anupamchugh/c4d0146ed6701ccbf40462ce9bad67fa to your computer and use it in GitHub Desktop.
Save anupamchugh/c4d0146ed6701ccbf40462ce9bad67fa to your computer and use it in GitHub Desktop.
import imaplib
import email
def read_email_from_gmail():
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('username@gmail.com','password_or_2_factor_auth')
mail.list()
mail.select('inbox')
(retcode, messages) = mail.search(None, 'ALL')
if retcode == 'OK':
id_list = messages[0].split()
latest_emails = id_list[-10:]
for num in latest_emails:
typ, data = mail.fetch(num,'(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
original = email.message_from_bytes(response_part[1])
print(original['From'])
mail.close()
mail.logout()
read_email_from_gmail()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment