-
-
Save morenoh149/f2ed47dc8c57bacf5bc27a899c642d6a to your computer and use it in GitHub Desktop.
Basic example of using Python3 and IMAP to read emails in a gmail folder/label. Remove legacy email.header api use
This file contains hidden or 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
#!/usr/bin/env python | |
# | |
# Basic example of using Python3 and IMAP to read emails in a gmail folder/label. | |
# Remove legacy email.header api use. | |
import sys | |
import imaplib | |
import getpass | |
import email | |
import datetime | |
EMAIL_ACCOUNT = "[email protected]" | |
# Use 'INBOX' to read inbox. Note that whatever folder is specified, | |
# after successfully running this script all emails in that folder | |
# will be marked as read. | |
EMAIL_FOLDER = "Top Secret/PRISM Documents" | |
def process_mailbox(M): | |
""" | |
Do something with emails messages in the folder. | |
For the sake of this example, print some headers. | |
""" | |
rv, data = M.search(None, "ALL") | |
if rv != 'OK': | |
print("No messages found!") | |
return | |
for num in data[0].split(): | |
rv, data = M.fetch(num, '(RFC822)') | |
if rv != 'OK': | |
print("ERROR getting message", num) | |
return | |
msg = email.message_from_bytes(data[0][1]) | |
print('Message %s: %s' % (num, msg['Subject'])) | |
print('Raw Date:', msg['Date']) | |
# Now convert to local date-time | |
date_tuple = email.utils.parsedate_tz(msg['Date']) | |
if date_tuple: | |
local_date = datetime.datetime.fromtimestamp( | |
email.utils.mktime_tz(date_tuple)) | |
print ("Local Date:", \ | |
local_date.strftime("%a, %d %b %Y %H:%M:%S")) | |
M = imaplib.IMAP4_SSL('imap.gmail.com') | |
try: | |
rv, data = M.login(EMAIL_ACCOUNT, getpass.getpass()) | |
except imaplib.IMAP4.error: | |
print ("LOGIN FAILED!!! ") | |
sys.exit(1) | |
print(rv, data) | |
rv, mailboxes = M.list() | |
if rv == 'OK': | |
print("Mailboxes:") | |
print(mailboxes) | |
rv, data = M.select(EMAIL_FOLDER) | |
if rv == 'OK': | |
print("Processing mailbox...\n") | |
process_mailbox(M) | |
M.close() | |
else: | |
print("ERROR: Unable to open mailbox ", rv) | |
M.logout() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment