Skip to content
This repository was archived by the owner on Sep 27, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ pip-log.txt

#vim
*.swp

#pycharm
.idea
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 12 additions & 8 deletions src/ImapLibrary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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':
Expand Down