diff --git a/src/ImapLibrary/__init__.py b/src/ImapLibrary/__init__.py index 1f28772..93697d8 100644 --- a/src/ImapLibrary/__init__.py +++ b/src/ImapLibrary/__init__.py @@ -15,14 +15,16 @@ 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, port=993, secured=True): """ Open the mailbox on a mail server with a valid authentication. """ - self.imap = imaplib.IMAP4_SSL(server, self.port) + port = int(port) + if secured: + self.imap = imaplib.IMAP4_SSL(server, port) + else: + self.imap = imaplib.IMAP4(server, port) self.imap.login(user, password) self.imap.select() @@ -99,8 +101,29 @@ def get_email_body(self, mailNumber): `mailNumber` is the index number of the mail to open """ body = self.imap.fetch(mailNumber, '(BODY[TEXT])')[1][0][1].decode('quoted-printable') + body = body.decode('utf-8') return body + def get_email_title(self, mailNumber): + """ + Returns an email title + + `mailNumber` is the index number of the mail to open + """ + subject = self.imap.fetch(mailNumber, '(BODY[HEADER.FIELDS (SUBJECT)])')[1][0][1].lstrip('Subject: ').strip() + ' ' + subject, encoding = email.Header.decode_header(subject)[0] + title = subject.decode(encoding) + return title + + def remove_all_mails(self) + """ + Marks all received mails as deleted and removes those + """ + + for mail in self.mails: + self.imap.store(mail,'+FLAGS', '\DELETED') + self.imap.expunge() + def _criteria(self, fromEmail, toEmail, status): crit = [] if fromEmail: