Python – processing GMail IMAP email

Here is an example of processing your GMail IMAP email in Python.

The script below will:

  • login to GMail account using IMAP
  • open your Inbox
  • retrieve and print all messages
  • close mailbox
  • logout
# Script adapted from Corey Goldberg
# http://coreygoldberg.blogspot.com/2011/07/python-processing-gmail-imap-email.html
#!/usr/bin/env python import imaplib USER = 'username@gmail.com' PASSWORD = 'xxx' mail = imaplib.IMAP4_SSL('imap.gmail.com', 993) mail.login(USER, PASSWORD) mail.select('Inbox') status, data = mail.search(None, 'ALL') for num in data[0].split(): status, data = mail.fetch(num, '(RFC822)') print 'Message %s\n%s\n' % (num, data[0][1]) mail.close() mail.logout()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s