I assume you have enabled POP3/IMAP access to your GMail account.
This is sample code:
import imaplib
conn= imaplib.IMAP4_SSL('imap.googlemail.com')
conn.login('yourusername', 'yourpassword')
code, dummy= conn.select('INBOX')
if code != 'OK':
raise RuntimeError, "Failed to select inbox"
code, data= self.conn.search(None, ALL)
if code == 'OK':
msgid_list= data[0].split()
else:
raise RuntimeError, "Failed to get message IDs"
for msgid in msgid_list:
code, data= conn.fetch(msgid, '(RFC822)')
# you can also use '(RFC822.HEADER)' only for headers
if code == 'OK':
pass # your code here
else:
raise RuntimeError, "could not retrieve msgid %r" % msgid
conn.close()
conn.logout()
or something like this.