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 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..8667968 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 @@ -41,7 +41,7 @@ def wait_for_mail(self, fromEmail=None, toEmail=None, status=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: @@ -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':