summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2013-07-11 20:58:06 +0200
committerClaude Paroz <claude@2xlibre.net>2013-07-11 22:00:08 +0200
commit59ebe39812858ba37e83ab0ee886f676980d472d (patch)
tree321863def25ef1148b27881adff1836b6c23ad1e /django
parent684a606a4ea21de6d1cc59b69f43b3a133672d59 (diff)
Fixed #17471 -- Added smtplib.SMTP_SSL connection option for SMTP backend
Thanks dj.facebook at gmail.com for the report and initial patch and Areski Belaid and senko for improvements.
Diffstat (limited to 'django')
-rw-r--r--django/conf/global_settings.py1
-rw-r--r--django/core/mail/backends/smtp.py38
2 files changed, 21 insertions, 18 deletions
diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py
index bf0dea558a..19258fbcd4 100644
--- a/django/conf/global_settings.py
+++ b/django/conf/global_settings.py
@@ -184,6 +184,7 @@ EMAIL_PORT = 25
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False
+EMAIL_USE_SSL = False
# List of strings representing installed apps.
INSTALLED_APPS = ()
diff --git a/django/core/mail/backends/smtp.py b/django/core/mail/backends/smtp.py
index e456b7864e..c7f4a1a801 100644
--- a/django/core/mail/backends/smtp.py
+++ b/django/core/mail/backends/smtp.py
@@ -15,22 +15,18 @@ class EmailBackend(BaseEmailBackend):
A wrapper that manages the SMTP network connection.
"""
def __init__(self, host=None, port=None, username=None, password=None,
- use_tls=None, fail_silently=False, **kwargs):
+ use_tls=None, fail_silently=False, use_ssl=None, **kwargs):
super(EmailBackend, self).__init__(fail_silently=fail_silently)
self.host = host or settings.EMAIL_HOST
self.port = port or settings.EMAIL_PORT
- if username is None:
- self.username = settings.EMAIL_HOST_USER
- else:
- self.username = username
- if password is None:
- self.password = settings.EMAIL_HOST_PASSWORD
- else:
- self.password = password
- if use_tls is None:
- self.use_tls = settings.EMAIL_USE_TLS
- else:
- self.use_tls = use_tls
+ self.username = settings.EMAIL_HOST_USER if username is None else username
+ self.password = settings.EMAIL_HOST_PASSWORD if password is None else password
+ self.use_tls = settings.EMAIL_USE_TLS if use_tls is None else use_tls
+ self.use_ssl = settings.EMAIL_USE_SSL if use_ssl is None else use_ssl
+ if self.use_ssl and self.use_tls:
+ raise ValueError(
+ "EMAIL_USE_TLS/EMAIL_USE_SSL are mutually exclusive, so only set "
+ "one of those settings to True.")
self.connection = None
self._lock = threading.RLock()
@@ -45,12 +41,18 @@ class EmailBackend(BaseEmailBackend):
try:
# If local_hostname is not specified, socket.getfqdn() gets used.
# For performance, we use the cached FQDN for local_hostname.
- self.connection = smtplib.SMTP(self.host, self.port,
+ if self.use_ssl:
+ self.connection = smtplib.SMTP_SSL(self.host, self.port,
local_hostname=DNS_NAME.get_fqdn())
- if self.use_tls:
- self.connection.ehlo()
- self.connection.starttls()
- self.connection.ehlo()
+ else:
+ self.connection = smtplib.SMTP(self.host, self.port,
+ local_hostname=DNS_NAME.get_fqdn())
+ # TLS/SSL are mutually exclusive, so only attempt TLS over
+ # non-secure connections.
+ if self.use_tls:
+ self.connection.ehlo()
+ self.connection.starttls()
+ self.connection.ehlo()
if self.username and self.password:
self.connection.login(self.username, self.password)
return True