diff options
Diffstat (limited to 'emailcanary/emailutils.py')
-rw-r--r-- | emailcanary/emailutils.py | 52 |
1 files changed, 36 insertions, 16 deletions
diff --git a/emailcanary/emailutils.py b/emailcanary/emailutils.py index 83c2f46..54323a1 100644 --- a/emailcanary/emailutils.py +++ b/emailcanary/emailutils.py @@ -1,27 +1,47 @@ +import sys import imaplib, email def get_imap(server, username, password): - '''Connect and login via IMAP''' - mail = imaplib.IMAP4_SSL(server) - mail.login(username, password) - return mail + '''Connect and login via IMAP''' + try: + mail = imaplib.IMAP4_SSL(server) + mail.login(username, password) + return mail + except Exception as e: + sys.stderr.write("Error connecting to %s@%s: %s\n\n" % (username, server, str(e))) + return None def get_mail_uids(mail): - '''Return a list of message UIDs in the inbox''' - mail.select("inbox") # connect to inbox. - result, data = mail.uid('search', None, "ALL") # search and return uids instead - return data[0].split() + '''Return a list of message UIDs in the inbox''' + if mail is None: + return [] + mail.select("inbox") # connect to inbox. + result, data = mail.uid('search', None, "ALL") # search and return uids instead + return data[0].split() def get_message(mail, uid): - '''Get a single email message object by UID''' - result, data = mail.uid('fetch', uid, '(RFC822)') - raw_email = data[0][1] - return email.message_from_string(raw_email) + '''Get a single email message object by UID''' + if mail is None: + return None + result, data = mail.uid('fetch', uid, '(RFC822)') + if result == 'OK': + dat0 = data[0] + if dat0 is None: + return None + else: + raw_email = str(dat0[1]) + return email.message_from_string(raw_email) + else: + raise Exception("Bad response from server: %s" % (result)) def delete_message(mail, uid): - result = mail.uid('store', uid, '+FLAGS', '(\Deleted)') + if mail is None: + return + result = mail.uid('store', uid, '+FLAGS', '(\Deleted)') def close(mail): - mail.expunge() - mail.close() - mail.logout() + if mail is None: + return + mail.expunge() + mail.close() + mail.logout() |