Skip to content
This repository has been archived by the owner on Sep 27, 2021. It is now read-only.

New keywords and encoding handling #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
31 changes: 27 additions & 4 deletions src/ImapLibrary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ 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, 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()

Expand Down Expand Up @@ -99,8 +101,29 @@ 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]
title = subject.decode(encoding)
return title

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 = []
if fromEmail:
Expand Down