diff --git a/src/ImapLibrary/__init__.py b/src/ImapLibrary/__init__.py index 1f28772..646fc96 100644 --- a/src/ImapLibrary/__init__.py +++ b/src/ImapLibrary/__init__.py @@ -3,6 +3,7 @@ import imaplib import time import urllib2 +from email.parser import HeaderParser THIS_DIR = os.path.dirname(os.path.abspath(__file__)) execfile(os.path.join(THIS_DIR, 'version.py')) @@ -15,14 +16,15 @@ class ImapLibrary(object): ROBOT_LIBRARY_VERSION = VERSION ROBOT_LIBRARY_SCOPE = 'GLOBAL' - port = 993 - - def open_mailbox(self, server, user, password): + def open_mailbox(self, server, user, password, ssl=True): """ Open the mailbox on a mail server with a valid authentication. """ - self.imap = imaplib.IMAP4_SSL(server, self.port) + if ssl == True: + self.imap = imaplib.IMAP4_SSL(server, 993) + else: + self.imap = imaplib.IMAP4(server, 143) self.imap.login(user, password) self.imap.select() @@ -101,6 +103,17 @@ def get_email_body(self, mailNumber): body = self.imap.fetch(mailNumber, '(BODY[TEXT])')[1][0][1].decode('quoted-printable') return body + def get_email_subject(self, mailNumber): + """ + Returns an email subject + + `mailNumber` is the index number of the mail to open + """ + header_data = self.imap.fetch(mailNumber,'(BODY[HEADER.FIELDS (SUBJECT)])')[1][0][1] + parser = HeaderParser() + data = parser.parsestr(header_data) + return data['Subject'] + def _criteria(self, fromEmail, toEmail, status): crit = [] if fromEmail: