Fixed #2897 -- Added support for TLS connections to email handling. This means
servers like Google's SMTP server can now be used for admin emails. git-svn-id: http://code.djangoproject.com/svn/django/trunk@5144 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
21a2ca6a21
commit
7a84ad93e6
|
@ -119,6 +119,7 @@ EMAIL_PORT = 25
|
||||||
# Optional SMTP authentication information for EMAIL_HOST.
|
# Optional SMTP authentication information for EMAIL_HOST.
|
||||||
EMAIL_HOST_USER = ''
|
EMAIL_HOST_USER = ''
|
||||||
EMAIL_HOST_PASSWORD = ''
|
EMAIL_HOST_PASSWORD = ''
|
||||||
|
EMAIL_USE_TLS = False
|
||||||
|
|
||||||
# List of strings representing installed apps.
|
# List of strings representing installed apps.
|
||||||
INSTALLED_APPS = ()
|
INSTALLED_APPS = ()
|
||||||
|
|
|
@ -70,15 +70,12 @@ class SMTPConnection(object):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, host=None, port=None, username=None, password=None,
|
def __init__(self, host=None, port=None, username=None, password=None,
|
||||||
fail_silently=False):
|
use_tls=None, fail_silently=False):
|
||||||
if host is None:
|
self.host = host or settings.EMAIL_HOST
|
||||||
self.host = settings.EMAIL_HOST
|
self.port = (port is not None) and port or settings.EMAIL_PORT
|
||||||
if port is None:
|
self.username = username or settings.EMAIL_HOST_USER
|
||||||
self.port = settings.EMAIL_PORT
|
self.password = password or settings.EMAIL_HOST_PASSWORD
|
||||||
if username is None:
|
self.use_tls = (use_tls is not None) and use_tls or settings.EMAIL_USE_TLS
|
||||||
self.username = settings.EMAIL_HOST_USER
|
|
||||||
if password is None:
|
|
||||||
self.password = settings.EMAIL_HOST_PASSWORD
|
|
||||||
self.fail_silently = fail_silently
|
self.fail_silently = fail_silently
|
||||||
self.connection = None
|
self.connection = None
|
||||||
|
|
||||||
|
@ -92,6 +89,10 @@ class SMTPConnection(object):
|
||||||
return False
|
return False
|
||||||
try:
|
try:
|
||||||
self.connection = smtplib.SMTP(self.host, self.port)
|
self.connection = smtplib.SMTP(self.host, self.port)
|
||||||
|
if self.use_tls:
|
||||||
|
self.connection.ehlo()
|
||||||
|
self.connection.starttls()
|
||||||
|
self.connection.ehlo()
|
||||||
if self.username and self.password:
|
if self.username and self.password:
|
||||||
self.connection.login(self.username, self.password)
|
self.connection.login(self.username, self.password)
|
||||||
return True
|
return True
|
||||||
|
@ -104,6 +105,10 @@ class SMTPConnection(object):
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
self.connection.quit()
|
self.connection.quit()
|
||||||
|
except socket.sslerror:
|
||||||
|
# This happens when calling quit() on a TLS connection
|
||||||
|
# sometimes.
|
||||||
|
self.connection.close()
|
||||||
except:
|
except:
|
||||||
if self.fail_silently:
|
if self.fail_silently:
|
||||||
return
|
return
|
||||||
|
|
|
@ -22,7 +22,8 @@ In two lines::
|
||||||
|
|
||||||
Mail will be sent using the SMTP host and port specified in the `EMAIL_HOST`_
|
Mail will be sent using the SMTP host and port specified in the `EMAIL_HOST`_
|
||||||
and `EMAIL_PORT`_ settings. The `EMAIL_HOST_USER`_ and `EMAIL_HOST_PASSWORD`_
|
and `EMAIL_PORT`_ settings. The `EMAIL_HOST_USER`_ and `EMAIL_HOST_PASSWORD`_
|
||||||
settings, if set, will be used to authenticate to the SMTP server.
|
settings, if set, will be used to authenticate to the SMTP server and the
|
||||||
|
`EMAIL_USE_TLS`_ settings will control whether a secure connection is used.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
|
@ -34,6 +35,7 @@ settings, if set, will be used to authenticate to the SMTP server.
|
||||||
.. _EMAIL_PORT: ../settings/#email-port
|
.. _EMAIL_PORT: ../settings/#email-port
|
||||||
.. _EMAIL_HOST_USER: ../settings/#email-host-user
|
.. _EMAIL_HOST_USER: ../settings/#email-host-user
|
||||||
.. _EMAIL_HOST_PASSWORD: ../settings/#email-host-password
|
.. _EMAIL_HOST_PASSWORD: ../settings/#email-host-password
|
||||||
|
.. _EMAIL_USE_TLS: ../settings/#email-use-tls
|
||||||
|
|
||||||
|
|
||||||
send_mail()
|
send_mail()
|
||||||
|
|
|
@ -428,6 +428,13 @@ Subject-line prefix for e-mail messages sent with ``django.core.mail.mail_admins
|
||||||
or ``django.core.mail.mail_managers``. You'll probably want to include the
|
or ``django.core.mail.mail_managers``. You'll probably want to include the
|
||||||
trailing space.
|
trailing space.
|
||||||
|
|
||||||
|
EMAIL_USE_TLS
|
||||||
|
-------------
|
||||||
|
|
||||||
|
Default: ``False``
|
||||||
|
|
||||||
|
Whether to use a TLS (secure) connection when talking to the SMTP server.
|
||||||
|
|
||||||
FIXTURE_DIRS
|
FIXTURE_DIRS
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue