From 5253ca9b670a4113a741325cbc94127345d58b4b Mon Sep 17 00:00:00 2001 From: Prashant Shewale Date: Wed, 2 Sep 2015 20:32:13 +0530 Subject: [PATCH 1/5] added support for subject and mail body text in search criteria --- README.rst | 2 ++ src/ImapLibrary/__init__.py | 18 +++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index 5d2382b..1c21cdf 100644 --- a/README.rst +++ b/README.rst @@ -43,6 +43,8 @@ These keyword actions are available:: Arguments: - fromEmail: the email address of the sender (not required) - toEmail: the email address of the receiver (not required) + - subject: the subject of the email (not required) + - text: some text from email body (not required) - status: the status of the email (not required) - timeout: the timeout how long the mailbox shall check emails in seconds (defaults to 60 seconds) diff --git a/src/ImapLibrary/__init__.py b/src/ImapLibrary/__init__.py index 68f4b5c..5cc4f67 100644 --- a/src/ImapLibrary/__init__.py +++ b/src/ImapLibrary/__init__.py @@ -28,8 +28,8 @@ def open_mailbox(self, server, user, password): self.imap.select() self._init_walking_multipart() - def wait_for_mail(self, fromEmail=None, toEmail=None, status=None, - timeout=60): + def wait_for_mail(self, fromEmail=None, toEmail=None, + subject=None, text=None, status=None, timeout=60): """ Wait for an incoming mail from a specific sender to a specific mail receiver. Check the mailbox every 10 @@ -188,20 +188,24 @@ def get_multipart_field(self, field): """ return self._mp_msg[field] - def _criteria(self, fromEmail, toEmail, status): + def _criteria(self, fromEmail, toEmail, subject, text, status): crit = [] if fromEmail: - crit += ['FROM', fromEmail] + crit += ['FROM', '"' + fromEmail + '"'] if toEmail: - crit += ['TO', toEmail] + crit += ['TO', '"' + toEmail + '"'] + if subject: + crit += ['SUBJECT', '"' + subject + '"'] + if text: + crit += ['TEXT', '"' + text + '"'] if status: crit += [status] if not crit: crit = ['UNSEEN'] return crit - def _check_emails(self, fromEmail, toEmail, status): - crit = self._criteria(fromEmail, toEmail, status) + def _check_emails(self, fromEmail, toEmail, subject, text, status): + crit = self._criteria(fromEmail, toEmail, subject, text, status) # Calling select before each search is necessary with gmail status, data = self.imap.select() if status != 'OK': From b6aff03684c691a6aae8d1852ed6db7258f288f0 Mon Sep 17 00:00:00 2001 From: Prashant Shewale Date: Wed, 2 Sep 2015 20:40:01 +0530 Subject: [PATCH 2/5] corrected indentation --- src/ImapLibrary/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImapLibrary/__init__.py b/src/ImapLibrary/__init__.py index 5cc4f67..100d6a1 100644 --- a/src/ImapLibrary/__init__.py +++ b/src/ImapLibrary/__init__.py @@ -194,7 +194,7 @@ def _criteria(self, fromEmail, toEmail, subject, text, status): crit += ['FROM', '"' + fromEmail + '"'] if toEmail: crit += ['TO', '"' + toEmail + '"'] - if subject: + if subject: crit += ['SUBJECT', '"' + subject + '"'] if text: crit += ['TEXT', '"' + text + '"'] From e7abb3bfbbe1dfad7dd7f361b17df7436b103150 Mon Sep 17 00:00:00 2001 From: Prashant Shewale Date: Wed, 2 Sep 2015 20:47:11 +0530 Subject: [PATCH 3/5] corrected indentation --- src/ImapLibrary/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImapLibrary/__init__.py b/src/ImapLibrary/__init__.py index 100d6a1..f2eb5db 100644 --- a/src/ImapLibrary/__init__.py +++ b/src/ImapLibrary/__init__.py @@ -194,7 +194,7 @@ def _criteria(self, fromEmail, toEmail, subject, text, status): crit += ['FROM', '"' + fromEmail + '"'] if toEmail: crit += ['TO', '"' + toEmail + '"'] - if subject: + if subject: crit += ['SUBJECT', '"' + subject + '"'] if text: crit += ['TEXT', '"' + text + '"'] From bcf7517582e40397e059f0408dba4f1350b754e5 Mon Sep 17 00:00:00 2001 From: Prashant Shewale Date: Wed, 2 Sep 2015 20:51:33 +0530 Subject: [PATCH 4/5] call _check_emails with correct arguments --- src/ImapLibrary/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImapLibrary/__init__.py b/src/ImapLibrary/__init__.py index f2eb5db..8667968 100644 --- a/src/ImapLibrary/__init__.py +++ b/src/ImapLibrary/__init__.py @@ -41,7 +41,7 @@ def wait_for_mail(self, fromEmail=None, toEmail=None, """ endTime = time.time() + int(timeout) while (time.time() < endTime): - self.mails = self._check_emails(fromEmail, toEmail, status) + self.mails = self._check_emails(fromEmail, toEmail, subject, text, status) if len(self.mails) > 0: return self.mails[-1] if time.time() < endTime: From 80d0695bc44e74d2b14e90643790556a702d83f3 Mon Sep 17 00:00:00 2001 From: Prashant Shewale Date: Thu, 3 Sep 2015 10:54:59 +0530 Subject: [PATCH 5/5] ignore .idea for pycharm --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 09bf34c..2d8df49 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,6 @@ pip-log.txt #vim *.swp + +#pycharm +.idea