From f6120c205dcb59c24e930493190217883f9fbd52 Mon Sep 17 00:00:00 2001 From: Juho Saarinen Date: Tue, 8 Jan 2013 11:16:22 +0200 Subject: [PATCH 1/3] Update src/ImapLibrary/__init__.py Added possibility to remove all mails from mailbox --- src/ImapLibrary/__init__.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/ImapLibrary/__init__.py b/src/ImapLibrary/__init__.py index 1f28772..64b2628 100644 --- a/src/ImapLibrary/__init__.py +++ b/src/ImapLibrary/__init__.py @@ -15,7 +15,7 @@ class ImapLibrary(object): ROBOT_LIBRARY_VERSION = VERSION ROBOT_LIBRARY_SCOPE = 'GLOBAL' - port = 993 + port = 143 def open_mailbox(self, server, user, password): """ @@ -100,6 +100,15 @@ def get_email_body(self, mailNumber): """ body = self.imap.fetch(mailNumber, '(BODY[TEXT])')[1][0][1].decode('quoted-printable') return body + + 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 = [] From a5c3c6b7322a9b1c1285e87e7703328c73019562 Mon Sep 17 00:00:00 2001 From: Juho Saarinen Date: Tue, 8 Jan 2013 13:22:11 +0200 Subject: [PATCH 2/3] Update src/ImapLibrary/__init__.py Mail title and body in readable form for tests --- src/ImapLibrary/__init__.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/ImapLibrary/__init__.py b/src/ImapLibrary/__init__.py index 64b2628..381ebc1 100644 --- a/src/ImapLibrary/__init__.py +++ b/src/ImapLibrary/__init__.py @@ -99,7 +99,20 @@ 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] + print subject + title = subject.decode(encoding) + return title def remove_all_mails(self) """ From d928d2d9096c618a1efb19d70786fbc420de3cac Mon Sep 17 00:00:00 2001 From: Juho Saarinen Date: Tue, 8 Jan 2013 15:06:35 +0200 Subject: [PATCH 3/3] Update src/ImapLibrary/__init__.py Connection making without breaking old code --- src/ImapLibrary/__init__.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/ImapLibrary/__init__.py b/src/ImapLibrary/__init__.py index 381ebc1..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 = 143 - - 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() @@ -110,7 +112,6 @@ def get_email_title(self, mailNumber): """ subject = self.imap.fetch(mailNumber, '(BODY[HEADER.FIELDS (SUBJECT)])')[1][0][1].lstrip('Subject: ').strip() + ' ' subject, encoding = email.Header.decode_header(subject)[0] - print subject title = subject.decode(encoding) return title