Skip to content

Instantly share code, notes, and snippets.

@hannesdelbeke
Created January 21, 2024 11:11
Show Gist options
  • Save hannesdelbeke/25c11ec7b5fe7761849b3e0665e96e8d to your computer and use it in GitHub Desktop.
Save hannesdelbeke/25c11ec7b5fe7761849b3e0665e96e8d to your computer and use it in GitHub Desktop.
read gmail with python
#https://codehandbook.org/how-to-read-email-from-gmail-using-python/
#https://gist.github.com/robulouski/7441883
import smtplib
import time
import imaplib
import email
import getpass
#https://stackoverflow.com/questions/753052/strip-html-from-strings-in-python
from HTMLParser import HTMLParser
class MLStripper(HTMLParser):
def __init__(self):
self.reset()
self.fed = []
def handle_data(self, d):
self.fed.append(d)
def get_data(self):
return ''.join(self.fed)
def strip_tags(html):
if not html:
return html
s = MLStripper()
s.feed(html)
return s.get_data()
FROM_EMAIL = "[email protected]"
FROM_PWD = "" #getpass.getpass()
SMTP_SERVER = "imap.gmail.com"
SMTP_PORT = 993
# -------------------------------------------------
#
# Utility to read email from Gmail Using Python
#
# ------------------------------------------------
def read_email_from_gmail():
try:
mail = imaplib.IMAP4_SSL(SMTP_SERVER)
print "logging in"
mail.login(FROM_EMAIL,FROM_PWD)
print "done"
for x in mail.list():
print x
# mail.select('inbox')
mail.select("[Gmail]/Spam") #'(\\HasNoChildren \\Junk) "/" "[Gmail]/Spam"',
_type, data = mail.search(None, 'ALL')
mail_ids = data[0]
id_list = mail_ids.split()
first_email_id = int(id_list[0])
latest_email_id = int(id_list[-1])
all_email_text = []
for i in range(latest_email_id,first_email_id, -1):
typ, data = mail.fetch(i, '(RFC822)' )
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
email_subject = msg['subject']
email_from = msg['from']
message_text = msg.get_payload(decode=True)
if msg.is_multipart():
messages = msg.get_payload(decode=False) # this wil lreturn a list instead of string
for child_msg in messages:
txt = child_msg.get_payload(decode=True)
all_email_text.append(txt)
else:
all_email_text.append(message_text)
# if not message_text:
# message_text = msg.get_payload(decode=False)
# todo handle multiple emails in chain-list thingy
# instead of string it contains isntance of email class
if type(message_text)==list:
print message_text
except Exception, e:
print str(e)
read_email_from_gmail()
print "FINISHED!! :)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment