Skip to content
This repository was archived by the owner on Sep 27, 2021. It is now read-only.
Open
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
21 changes: 17 additions & 4 deletions src/ImapLibrary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import imaplib
import time
import urllib2
from email.parser import HeaderParser

THIS_DIR = os.path.dirname(os.path.abspath(__file__))
execfile(os.path.join(THIS_DIR, 'version.py'))
Expand All @@ -15,14 +16,15 @@ 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, ssl=True):
"""
Open the mailbox on a mail server with a valid
authentication.
"""
self.imap = imaplib.IMAP4_SSL(server, self.port)
if ssl == True:
self.imap = imaplib.IMAP4_SSL(server, 993)
else:
self.imap = imaplib.IMAP4(server, 143)
self.imap.login(user, password)
self.imap.select()

Expand Down Expand Up @@ -101,6 +103,17 @@ def get_email_body(self, mailNumber):
body = self.imap.fetch(mailNumber, '(BODY[TEXT])')[1][0][1].decode('quoted-printable')
return body

def get_email_subject(self, mailNumber):
"""
Returns an email subject

`mailNumber` is the index number of the mail to open
"""
header_data = self.imap.fetch(mailNumber,'(BODY[HEADER.FIELDS (SUBJECT)])')[1][0][1]
parser = HeaderParser()
data = parser.parsestr(header_data)
return data['Subject']

def _criteria(self, fromEmail, toEmail, status):
crit = []
if fromEmail:
Expand Down