Skip to content

Instantly share code, notes, and snippets.

@jleedev
Created August 16, 2012 16:00
Show Gist options
  • Save jleedev/3371328 to your computer and use it in GitHub Desktop.
Save jleedev/3371328 to your computer and use it in GitHub Desktop.
import email
import email.parser
import imaplib
import getpass
login = raw_input("Gmail address: ")
password = getpass.getpass()
sock = imaplib.IMAP4_SSL("imap.gmail.com", 993)
sock.login(login, password)
ok, _ = sock.select("[Gmail]/Chats", readonly=True)
if ok == 'NO':
sock.select("[Google Mail]/Chats", readonly=True)
ok, [ids] = sock.search(None, '(ALL)')
ids = ids.split()
msg_id = ids[-1]
resp, data = sock.fetch(msg_id, '(RFC822)')
msg = email.parser.Parser().parsestr(data[0][1])
for part in msg.walk():
if part.get_content_type() != 'text/html':
continue
print part.get_payload(decode=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment