-
-
Save anupamchugh/c4d0146ed6701ccbf40462ce9bad67fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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