I wanted to write a script to find out everybody who emailed me last year (by accessing my IMAP server). Since I am on a Python binge lately, I decided to write it in Python. Unfortunately, Python's imaplib documentation is a little short on examples.

After much experimentation, I finally figured it out:

import getpass, imaplib

M = imaplib.IMAP4_SSL( 'localhost' ) M.login(getpass.getuser(), getpass.getpass()) M.select( "INBOX.GmailMirror" )

typ, data = M.search( None, '(SENTSINCE "1-Jan-2007")' ) for num in data[0].split(): typ, data = M.uid( 'FETCH', num, '(BODY[HEADER.FIELDS(FROM)])' ) try: print data[0][1].strip() except TypeError: print data

M.close() M.logout()

tags: python code imap email
permalink | comments | technorati
blog comments powered by Disqus