2014-08-22 19:25:14 -07:00
|
|
|
from imaplib import IMAP4, IMAP4_SSL
|
2014-05-29 23:41:11 -06:00
|
|
|
import logging
|
|
|
|
|
|
2014-08-22 19:25:14 -07:00
|
|
|
from django.conf import settings
|
2012-06-30 22:30:13 -07:00
|
|
|
|
2014-04-25 16:37:51 -07:00
|
|
|
from .base import EmailTransport, MessageParseError
|
2013-06-22 14:35:47 -07:00
|
|
|
|
2014-08-22 19:25:14 -07:00
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2014-05-28 20:39:13 -06:00
|
|
|
|
2014-04-25 16:37:51 -07:00
|
|
|
|
|
|
|
|
class ImapTransport(EmailTransport):
|
2014-05-23 09:17:14 +01:00
|
|
|
def __init__(self, hostname, port=None, ssl=False, archive=''):
|
2014-05-30 16:32:57 -07:00
|
|
|
self.max_message_size = getattr(
|
2014-05-29 23:41:11 -06:00
|
|
|
settings,
|
|
|
|
|
'DJANGO_MAILBOX_MAX_MESSAGE_SIZE',
|
|
|
|
|
False
|
|
|
|
|
)
|
2012-06-30 22:30:13 -07:00
|
|
|
self.hostname = hostname
|
|
|
|
|
self.port = port
|
2014-05-23 09:17:14 +01:00
|
|
|
self.archive = archive
|
2012-06-30 22:30:13 -07:00
|
|
|
if ssl:
|
|
|
|
|
self.transport = IMAP4_SSL
|
|
|
|
|
if not self.port:
|
|
|
|
|
self.port = 993
|
|
|
|
|
else:
|
|
|
|
|
self.transport = IMAP4
|
|
|
|
|
if not self.port:
|
|
|
|
|
self.port = 143
|
|
|
|
|
|
|
|
|
|
def connect(self, username, password):
|
|
|
|
|
self.server = self.transport(self.hostname, self.port)
|
|
|
|
|
typ, msg = self.server.login(username, password)
|
|
|
|
|
self.server.select()
|
|
|
|
|
|
2014-05-28 20:39:13 -06:00
|
|
|
def _get_all_message_ids(self):
|
|
|
|
|
# Fetch all the message uids
|
2014-05-29 23:41:11 -06:00
|
|
|
response, message_ids = self.server.uid('search', None, 'ALL')
|
2014-08-14 21:38:52 -07:00
|
|
|
message_id_string = message_ids[0].strip()
|
|
|
|
|
# Usually `message_id_string` will be a list of space-separated
|
|
|
|
|
# ids; we must make sure that it isn't an empty string before
|
|
|
|
|
# splitting into individual UIDs.
|
|
|
|
|
if message_id_string:
|
|
|
|
|
return message_id_string.split(' ')
|
|
|
|
|
return []
|
2014-05-28 20:39:13 -06:00
|
|
|
|
|
|
|
|
def _get_small_message_ids(self, message_ids):
|
|
|
|
|
# Using existing message uids, get the sizes and
|
|
|
|
|
# return only those that are under the size
|
|
|
|
|
# limit
|
|
|
|
|
safe_message_ids = []
|
|
|
|
|
|
|
|
|
|
status, data = self.server.uid(
|
|
|
|
|
'fetch',
|
|
|
|
|
','.join(message_ids),
|
2014-05-29 23:41:11 -06:00
|
|
|
'(RFC822.SIZE)'
|
2014-05-28 20:39:13 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for each_msg in data:
|
2014-05-29 23:41:11 -06:00
|
|
|
try:
|
|
|
|
|
uid = each_msg.split(' ')[2]
|
|
|
|
|
size = each_msg.split(' ')[4].rstrip(')')
|
2014-05-30 16:32:57 -07:00
|
|
|
if int(size) <= int(self.max_message_size):
|
2014-05-29 23:41:11 -06:00
|
|
|
safe_message_ids.append(uid)
|
2014-05-29 23:47:48 -06:00
|
|
|
except ValueError as e:
|
2014-05-30 16:32:57 -07:00
|
|
|
logger.warning(
|
|
|
|
|
"ValueError: %s working on %s" % (e, each_msg[0])
|
|
|
|
|
)
|
2014-05-29 23:41:11 -06:00
|
|
|
pass
|
2014-05-28 20:39:13 -06:00
|
|
|
return safe_message_ids
|
|
|
|
|
|
2012-06-30 22:30:13 -07:00
|
|
|
def get_message(self):
|
2014-05-28 20:39:13 -06:00
|
|
|
message_ids = self._get_all_message_ids()
|
|
|
|
|
|
|
|
|
|
# Limit the uids to the small ones if we care about that
|
2014-05-30 16:32:57 -07:00
|
|
|
if self.max_message_size:
|
2014-05-28 20:39:13 -06:00
|
|
|
message_ids = self._get_small_message_ids(message_ids)
|
|
|
|
|
|
|
|
|
|
if not message_ids:
|
2013-04-11 15:48:49 +04:00
|
|
|
return
|
|
|
|
|
|
2014-05-23 09:17:14 +01:00
|
|
|
if self.archive:
|
|
|
|
|
typ, folders = self.server.list(pattern=self.archive)
|
2014-05-25 12:57:38 -07:00
|
|
|
if folders[0] is None:
|
|
|
|
|
# If the archive folder does not exist, create it
|
|
|
|
|
self.server.create(self.archive)
|
2014-05-23 09:17:14 +01:00
|
|
|
|
2014-05-28 20:39:13 -06:00
|
|
|
for uid in message_ids:
|
2012-06-30 22:30:13 -07:00
|
|
|
try:
|
2014-05-28 20:39:13 -06:00
|
|
|
typ, msg_contents = self.server.uid('fetch', uid, '(RFC822)')
|
2014-04-25 16:37:51 -07:00
|
|
|
message = self.get_email_from_bytes(msg_contents[0][1])
|
2012-06-30 22:30:13 -07:00
|
|
|
yield message
|
2014-04-25 16:37:51 -07:00
|
|
|
except MessageParseError:
|
2012-06-30 22:30:13 -07:00
|
|
|
continue
|
2014-05-23 09:17:14 +01:00
|
|
|
|
|
|
|
|
if self.archive:
|
2014-05-28 20:39:13 -06:00
|
|
|
self.server.uid('copy', uid, self.archive)
|
2014-05-23 09:17:14 +01:00
|
|
|
|
2014-08-22 19:27:36 -07:00
|
|
|
self.server.uid('store', uid, "+FLAGS", "(\\Deleted)")
|
2012-06-30 22:30:13 -07:00
|
|
|
self.server.expunge()
|
|
|
|
|
return
|