From 92fda40e9cdef3110cf4eb0a5b929f9e3013a738 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lie=20Bouttier?= Date: Wed, 29 Nov 2017 22:31:00 +0100 Subject: [PATCH] fetch mails from imap server without SSL --- mailing/utils.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/mailing/utils.py b/mailing/utils.py index 1e15fac..4317e89 100644 --- a/mailing/utils.py +++ b/mailing/utils.py @@ -22,11 +22,17 @@ class InvalidKeyException(Exception): pass -def fetch_imap_box(user, password, host, port=993, inbox='INBOX', trash='Trash'): +def fetch_imap_box(user, password, host, port=993, ssl=True, inbox='INBOX', trash='Trash'): logging.basicConfig(level=logging.DEBUG) context = ssl.create_default_context() success, failure = 0, 0 - with imaplib.IMAP4_SSL(host=host, port=port, ssl_context=context) as M: + kwargs = {'host': host, 'port': port} + if ssl: + IMAP4 = imaplib.IMAP4_SSL + kwargs.update({'ssl_context': ssl.create_default_context()}) + else: + IMAP4 = imaplib.IMAP4 + with IMAP4(**kwargs) as M: typ, data = M.login(user, password) if typ != 'OK': raise Exception(data[0].decode('utf-8'))