Ticket #17471: ssl_setting_patch.diff

File ssl_setting_patch.diff, 6.8 KB (added by anonymous, 13 years ago)

same as the same-named attachment with .txt extension, only with .diff as extension to allow for easy trac viewing

  • docs/topics/email.txt

     
    2727:setting:`EMAIL_HOST` and :setting:`EMAIL_PORT` settings. The
    2828:setting:`EMAIL_HOST_USER` and :setting:`EMAIL_HOST_PASSWORD` settings, if
    2929set, are used to authenticate to the SMTP server, and the
    30 :setting:`EMAIL_USE_TLS` setting controls whether a secure connection is used.
     30:setting:`EMAIL_USE_TLS` and :setting:`EMAIL_USE_SSL` settings control whether
     31a secure connection is used.
    3132
    3233.. note::
    3334
     
    415416This is the default backend. Email will be sent through a SMTP server.
    416417The server address and authentication credentials are set in the
    417418:setting:`EMAIL_HOST`, :setting:`EMAIL_PORT`, :setting:`EMAIL_HOST_USER`,
    418 :setting:`EMAIL_HOST_PASSWORD` and :setting:`EMAIL_USE_TLS` settings in your
    419 settings file.
     419:setting:`EMAIL_HOST_PASSWORD`, :setting:`EMAIL_USE_TLS` and
     420:setting:`EMAIL_USE_SSL` settings in your settings file.
    420421
    421422The SMTP backend is the default configuration inherited by Django. If you
    422423want to specify it explicitly, put the following in your settings::
  • docs/ref/settings.txt

     
    976976Default: ``False``
    977977
    978978Whether to use a TLS (secure) connection when talking to the SMTP server.
     979This is used for explicit TLS connections, generally on port 587. If you are
     980experiencing hanging connections, see the implicit TLS setting
     981:setting:`EMAIL_USE_SSL`.
    979982
     983.. setting:: EMAIL_USE_SSL
     984
     985EMAIL_USE_SSL
     986-------------
     987
     988Default: ``False``
     989
     990Whether to use an implicit TLS (secure) connection when talking to the SMTP
     991server. In most email documentation this type of TLS connection is referred
     992to as SSL. It is generally used on port 465. If you are experiencing problems,
     993see the explicit TLS setting :setting:`EMAIL_USE_TLS`.
     994
    980995.. setting:: FILE_CHARSET
    981996
    982997FILE_CHARSET
  • tests/regressiontests/mail/tests.py

     
    66import smtpd
    77import sys
    88from StringIO import StringIO
     9from smtplib import SMTPException
     10from ssl import SSLError
    911import tempfile
    1012import threading
    1113
     
    674676        backend = smtp.EmailBackend(username='', password='')
    675677        self.assertEqual(backend.username, '')
    676678        self.assertEqual(backend.password, '')
     679
     680    @override_settings(EMAIL_USE_TLS=True)
     681    def test_email_tls_use_settings(self):
     682        backend = smtp.EmailBackend()
     683        self.assertTrue(backend.use_tls)
     684
     685    @override_settings(EMAIL_USE_TLS=True)
     686    def test_email_tls_override_settings(self):
     687        backend = smtp.EmailBackend(use_tls=False)
     688        self.assertFalse(backend.use_tls)
     689
     690    def test_email_tls_default_disabled(self):
     691        backend = smtp.EmailBackend()
     692        self.assertFalse(backend.use_tls)
     693
     694    @override_settings(EMAIL_USE_SSL=True)
     695    def test_email_ssl_use_settings(self):
     696        backend = smtp.EmailBackend()
     697        self.assertTrue(backend.use_ssl)
     698
     699    @override_settings(EMAIL_USE_SSL=True)
     700    def test_email_ssl_override_settings(self):
     701        backend = smtp.EmailBackend(use_ssl=False)
     702        self.assertFalse(backend.use_ssl)
     703
     704    def test_email_ssl_default_disabled(self):
     705        backend = smtp.EmailBackend()
     706        self.assertFalse(backend.use_ssl)
     707
     708    @override_settings(EMAIL_USE_TLS=True)
     709    def test_email_tls_attempts_starttls(self):
     710        backend = smtp.EmailBackend()
     711        self.assertTrue(backend.use_tls)
     712        try:
     713            backend.open()
     714            self.fail('SMTPException STARTTLS not raised.')
     715        except SMTPException, e:
     716            self.assertNotEqual(-1, str(e).find('STARTTLS'), "SMTPException wasn't for STARTTLS")
     717
     718    @override_settings(EMAIL_USE_SSL=True)
     719    def test_email_ssl_attempts_ssl_connection(self):
     720        backend = smtp.EmailBackend()
     721        self.assertTrue(backend.use_ssl)
     722        try:
     723            backend.open()
     724            self.fail('SSLError not raised.')
     725        except SSLError, e:
     726            pass
  • django/conf/global_settings.py

     
    171171EMAIL_HOST_USER = ''
    172172EMAIL_HOST_PASSWORD = ''
    173173EMAIL_USE_TLS = False
     174EMAIL_USE_SSL = False
    174175
    175176# List of strings representing installed apps.
    176177INSTALLED_APPS = ()
  • django/core/mail/backends/smtp.py

     
    1414    A wrapper that manages the SMTP network connection.
    1515    """
    1616    def __init__(self, host=None, port=None, username=None, password=None,
    17                  use_tls=None, fail_silently=False, **kwargs):
     17                 use_tls=None, fail_silently=False, use_ssl=None, **kwargs):
    1818        super(EmailBackend, self).__init__(fail_silently=fail_silently)
    1919        self.host = host or settings.EMAIL_HOST
    2020        self.port = port or settings.EMAIL_PORT
     
    3030            self.use_tls = settings.EMAIL_USE_TLS
    3131        else:
    3232            self.use_tls = use_tls
     33        if use_ssl is None:
     34            self.use_ssl = settings.EMAIL_USE_SSL
     35        else:
     36            self.use_ssl = use_ssl
    3337        self.connection = None
    3438        self._lock = threading.RLock()
    3539
     
    4448        try:
    4549            # If local_hostname is not specified, socket.getfqdn() gets used.
    4650            # For performance, we use the cached FQDN for local_hostname.
    47             self.connection = smtplib.SMTP(self.host, self.port,
     51            if self.use_ssl:
     52                self.connection = smtplib.SMTP_SSL(self.host, self.port,
    4853                                           local_hostname=DNS_NAME.get_fqdn())
    49             if self.use_tls:
    50                 self.connection.ehlo()
    51                 self.connection.starttls()
    52                 self.connection.ehlo()
     54            else:
     55                self.connection = smtplib.SMTP(self.host, self.port,
     56                                           local_hostname=DNS_NAME.get_fqdn())
     57                # TLS/SSL are mutually exclusive, so only attempt TLS over
     58                # non-secure connections.
     59                if self.use_tls:
     60                    self.connection.ehlo()
     61                    self.connection.starttls()
     62                    self.connection.ehlo()
    5363            if self.username and self.password:
    5464                self.connection.login(self.username, self.password)
    5565            return True
Back to Top