From 1d47fbb3c5a53e8cda137865c9900a2068768f6a Mon Sep 17 00:00:00 2001 From: Worros Date: Wed, 7 Jul 2010 10:47:02 +0800 Subject: [PATCH] Add simple IMAP email fetcher --- pyfpdb/IMAPEmailFetcher.py | 61 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 pyfpdb/IMAPEmailFetcher.py diff --git a/pyfpdb/IMAPEmailFetcher.py b/pyfpdb/IMAPEmailFetcher.py new file mode 100644 index 00000000..840ccd2d --- /dev/null +++ b/pyfpdb/IMAPEmailFetcher.py @@ -0,0 +1,61 @@ +import imaplib +import Configuration +import os +import pprint + +pp = pprint.PrettyPrinter(indent=4) + +def open_imap_connection(verbose=False): + # Read the config file + # FIXME + hostname = 'imap.gmail.com' + port = 993 + username = 'slartibartfast' + password = '42' + + # Connect + if verbose: print "Connecting to %s" % hostname + connection = imaplib.IMAP4_SSL(hostname) + + # Login to our account + if verbose: print "Logging in as %s" % username + connection.login(username, password) + return connection + +if __name__ == '__main__': + # Read the config file + # FIXME + folder = "INBOX" + c = open_imap_connection(verbose=True) + + try: + typ, data = c.list(directory=folder) + print typ, data + + c.select('INBOX', readonly=True) + + typ, msg_ids = c.search(None, '(SUBJECT "Results for PokerStars Tournament *")') + print typ, msg_ids + msgidlist = msg_ids[0].split(' ') + print msgidlist + + for msg in msgidlist: + print 'HEADER:' + typ, msg_data = c.fetch(msg, '(BODY.PEEK[HEADER])') + for response_part in msg_data: + if isinstance(response_part, tuple): + print response_part[1] + + print 'BODY TEXT:' + typ, msg_data = c.fetch(msg, '(BODY.PEEK[TEXT])') + for response_part in msg_data: + if isinstance(response_part, tuple): + print response_part[1] + + + finally: + try: + c.close() + except: + pass + c.logout()