mirror of
https://github.com/coddingtonbear/django-mailbox.git
synced 2026-07-09 22:38:19 +02:00
Added support for SSL mailboxes.
This commit is contained in:
parent
050b972c68
commit
636b144e85
2 changed files with 35 additions and 10 deletions
|
|
@ -12,7 +12,7 @@ class Mailbox(models.Model):
|
||||||
uri = models.CharField(
|
uri = models.CharField(
|
||||||
max_length=255,
|
max_length=255,
|
||||||
help_text="""
|
help_text="""
|
||||||
Start your URI with imap:// or pop3://.
|
Start your URI with imap://, imap+ssl://, pop3://, or pop3+ssl://.
|
||||||
<br />
|
<br />
|
||||||
<br />
|
<br />
|
||||||
Example: imap://myusername:mypassword@someserver
|
Example: imap://myusername:mypassword@someserver
|
||||||
|
|
@ -49,18 +49,27 @@ class Mailbox(models.Model):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def type(self):
|
def type(self):
|
||||||
return self._protocol_info.scheme.lower()
|
scheme = self._protocol_info.scheme.lower()
|
||||||
|
if '+' in scheme:
|
||||||
|
return scheme.split('+')[0]
|
||||||
|
return scheme
|
||||||
|
|
||||||
|
@property
|
||||||
|
def use_ssl(self):
|
||||||
|
return '+ssl' in self._protocol_info.scheme.lower()
|
||||||
|
|
||||||
def get_connection(self):
|
def get_connection(self):
|
||||||
if self.type == 'imap':
|
if self.type == 'imap':
|
||||||
conn = ImapMailEnumerator(
|
conn = ImapMailEnumerator(
|
||||||
self.location,
|
self.location,
|
||||||
self.port if self.port else 143
|
port=self.port if self.port else None,
|
||||||
|
ssl=self.use_ssl
|
||||||
)
|
)
|
||||||
elif self.type == 'pop3':
|
elif self.type == 'pop3':
|
||||||
conn = PopMailEnumerator(
|
conn = PopMailEnumerator(
|
||||||
self.location,
|
self.location,
|
||||||
self.port if self.port else 110
|
port=self.port if self.port else None,
|
||||||
|
ssl=self.use_ssl
|
||||||
)
|
)
|
||||||
conn.connect(self.username, self.password)
|
conn.connect(self.username, self.password)
|
||||||
return conn
|
return conn
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,22 @@
|
||||||
import email
|
import email
|
||||||
from imaplib import IMAP4
|
from imaplib import IMAP4, IMAP4_SSL
|
||||||
from poplib import POP3
|
from poplib import POP3, POP3_SSL
|
||||||
|
|
||||||
class PopMailEnumerator(object):
|
class PopMailEnumerator(object):
|
||||||
def __init__(self, hostname, port):
|
def __init__(self, hostname, port=None, ssl=False):
|
||||||
self.hostname = hostname
|
self.hostname = hostname
|
||||||
self.port = port
|
self.port = port
|
||||||
|
if ssl:
|
||||||
|
self.transport = POP3_SSL
|
||||||
|
if not self.port:
|
||||||
|
self.port = 995
|
||||||
|
else:
|
||||||
|
self.transport = POP3
|
||||||
|
if not self.port:
|
||||||
|
self.port = 110
|
||||||
|
|
||||||
def connect(self, username, password):
|
def connect(self, username, password):
|
||||||
self.server = POP3(self.hostname, self.port)
|
self.server = self.transport(self.hostname, self.port)
|
||||||
self.server.user(username)
|
self.server.user(username)
|
||||||
self.server.pass_(password)
|
self.server.pass_(password)
|
||||||
|
|
||||||
|
|
@ -26,12 +34,20 @@ class PopMailEnumerator(object):
|
||||||
return
|
return
|
||||||
|
|
||||||
class ImapMailEnumerator(object):
|
class ImapMailEnumerator(object):
|
||||||
def __init__(self, hostname, port):
|
def __init__(self, hostname, port=None, ssl=False):
|
||||||
self.hostname = hostname
|
self.hostname = hostname
|
||||||
self.port = port
|
self.port = port
|
||||||
|
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):
|
def connect(self, username, password):
|
||||||
self.server = IMAP4(self.hostname, self.port)
|
self.server = self.transport(self.hostname, self.port)
|
||||||
typ, msg = self.server.login(username, password)
|
typ, msg = self.server.login(username, password)
|
||||||
self.server.select()
|
self.server.select()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue