1
0
Fork 1
mirror of https://github.com/coddingtonbear/django-mailbox.git synced 2026-07-09 22:38:19 +02:00

Added email archiving option to IMAP transport

This commit is contained in:
Daniel 2014-05-23 09:17:14 +01:00
parent 872f4786c7
commit 0e0c6cf170
4 changed files with 49 additions and 14 deletions

View file

@ -146,6 +146,10 @@ class Mailbox(models.Model):
def use_ssl(self): def use_ssl(self):
return '+ssl' in self._protocol_info.scheme.lower() return '+ssl' in self._protocol_info.scheme.lower()
@property
def archive(self):
return self._protocol_info.query
def get_connection(self): def get_connection(self):
if not self.uri: if not self.uri:
return None return None
@ -153,7 +157,8 @@ class Mailbox(models.Model):
conn = ImapTransport( conn = ImapTransport(
self.location, self.location,
port=self.port if self.port else None, port=self.port if self.port else None,
ssl=self.use_ssl ssl=self.use_ssl,
archive=self.archive
) )
conn.connect(self.username, self.password) conn.connect(self.username, self.password)
elif self.type == 'pop3': elif self.type == 'pop3':

View file

@ -10,10 +10,12 @@ class TestImapTransport(EmailMessageTestCase):
self.arbitrary_hostname = 'one.two.three' self.arbitrary_hostname = 'one.two.three'
self.arbitrary_port = 100 self.arbitrary_port = 100
self.ssl = False self.ssl = False
self.archive = 'Archive'
self.transport = ImapTransport( self.transport = ImapTransport(
self.arbitrary_hostname, self.arbitrary_hostname,
self.arbitrary_port, self.arbitrary_port,
self.ssl self.ssl,
self.archive
) )
self.transport.server = None self.transport.server = None
super(TestImapTransport, self).setUp() super(TestImapTransport, self).setUp()
@ -36,7 +38,18 @@ class TestImapTransport(EmailMessageTestCase):
')', ')',
] ]
) )
server.list.return_value = (
'OK',
[
'(\\HasNoChildren) "/" "Archive"'
]
)
server.copy.return_value = (
'OK',
[
'[COPYUID 1 2 2] (Success)'
]
)
actual_messages = list(self.transport.get_message()) actual_messages = list(self.transport.get_message())
self.assertEqual(len(actual_messages), 1) self.assertEqual(len(actual_messages), 1)

View file

@ -4,9 +4,10 @@ from .base import EmailTransport, MessageParseError
class ImapTransport(EmailTransport): class ImapTransport(EmailTransport):
def __init__(self, hostname, port=None, ssl=False): def __init__(self, hostname, port=None, ssl=False, archive=''):
self.hostname = hostname self.hostname = hostname
self.port = port self.port = port
self.archive = archive
if ssl: if ssl:
self.transport = IMAP4_SSL self.transport = IMAP4_SSL
if not self.port: if not self.port:
@ -27,6 +28,11 @@ class ImapTransport(EmailTransport):
if not inbox[0]: if not inbox[0]:
return return
if self.archive:
typ, folders = self.server.list(pattern=self.archive)
if folders[0] == None:
self.archive = False
for key in inbox[0].split(): for key in inbox[0].split():
try: try:
typ, msg_contents = self.server.fetch(key, '(RFC822)') typ, msg_contents = self.server.fetch(key, '(RFC822)')
@ -34,6 +40,10 @@ class ImapTransport(EmailTransport):
yield message yield message
except MessageParseError: except MessageParseError:
continue continue
if self.archive:
self.server.copy(key, self.archive)
self.server.store(key, "+FLAGS", "\\Deleted") self.server.store(key, "+FLAGS", "\\Deleted")
self.server.expunge() self.server.expunge()
return return

View file

@ -7,24 +7,25 @@ POP3 and IMAP as well as local file-based mailboxes.
.. table:: 'Protocol' Options .. table:: 'Protocol' Options
============ ============== =============================================== ============ ============== ===============================================================
Mailbox Type 'Protocol':// Notes Mailbox Type 'Protocol':// Notes
============ ============== =============================================== ============ ============== ===============================================================
POP3 ``pop3://`` Can also specify SSL with ``pop3+ssl://`` POP3 ``pop3://`` Can also specify SSL with ``pop3+ssl://``
IMAP ``imap://`` Can also specify SSL with ``imap+ssl://`` IMAP ``imap://`` Can also specify SSL and archive with ``imap+ssl://?myarchive``
Maildir ``maildir://`` Maildir ``maildir://``
Mbox ``mbox://`` Mbox ``mbox://``
Babyl ``babyl://`` Babyl ``babyl://``
MH ``mh://`` MH ``mh://``
MMDF ``mmdf://`` MMDF ``mmdf://``
Piped Mail *empty* See :ref:`receiving-mail-from-exim4-or-postfix` Piped Mail *empty* See :ref:`receiving-mail-from-exim4-or-postfix`
============ ============== =============================================== ============ ============== ===============================================================
.. warning:: .. warning::
This will delete any messages it can find in the inbox you specify; This will delete or move any messages it can find in the inbox you specify;
do not use an e-mail inbox that you would like to share between do not use an e-mail inbox that you would like to share between
applications. applications. For the IMAP protocol you can specify an archive folder where
emails are moved to.
POP3 and IMAP Mailboxes POP3 and IMAP Mailboxes
@ -44,11 +45,17 @@ Also, if your username or password include any non-ascii characters,
they should be URL-encoded (for example, if your username includes an they should be URL-encoded (for example, if your username includes an
``@``, it should be changed to ``%40`` in your URI). ``@``, it should be changed to ``%40`` in your URI).
If you have an account named ``youremailaddress@gmail.com`` with a password If you are using an IMAP Mailbox, you can archive all messages before they
of ``1234`` on GMail, which uses a POP3 server of 'pop.gmail.com' and requires are deleted from the inbox. To archive emails, add the archive folder
SSL, you would enter the following as your URI:: name as a queryparemeter to the uri (for example, if your mailbox has a
folder called myarchive, add ``?myarchive`` to the uri).
pop3+ssl://youremailaddress%40gmail.com:1234@pop.gmail.com If you have an account named ``youremailaddress@gmail.com`` with a password
of ``1234`` on GMail, which uses a IMAP server of 'imap.gmail.com', requires
SSL and you want to archive all emails, you would enter the following as
your URI::
pop3+ssl://youremailaddress%40gmail.com:1234@pop.gmail.com?[GMAIL]/All Mail
Local File-based Mailboxes Local File-based Mailboxes