Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Fixed #13142 -- Added support for SSL connections in core.mail.backends.... #347

Closed
wants to merge 1 commit into from

3 participants

Jason Mayfield Florian Apolloner Tim Graham
Jason Mayfield

...smtp

Note: SVN-based patch originally provided by serg.partizan / partizan
and Wojciech Banaś in Trac (refer to https://code.djangoproject.com/ticket/13142).

This commit includes the work done by those previous, with the following
changes:

  • If use_ssl is True, or settings.EMAIL_USE_SSL is True, but the current Python
    version is less than 2.6, use_ssl is not internally changed to False. Nor
    does the SMTP backend code raise an exception. We don't need to, because Django 1.5
    is dropping support for Python prior to 2.6.

  • This update provides the ability to configure the keyfile and certfile
    to use with SMTP_SSL.

  • Documentation is updated to specify that this feature is new in 1.5.

Fixed #13142 -- Added support for SSL connections in core.mail.backen…
…ds.smtp

Note: SVN-based patch originally provided by serg.partizan / partizan
and Wojciech Banaś in Trac (refer to https://code.djangoproject.com/ticket/13142).

This commit includes the work done by those previous, with the following
changes:

- If use_ssl is True, or settings.EMAIL_USE_SSL is True, but the current Python
  version is less than 2.6, use_ssl is *not* internally changed to False.  Nor
  does the SMTP backend code raise an exception.  We don't need to, because Django 1.5
  is dropping support for Python prior to 2.6.

- This update provides the ability to configure the keyfile and certfile
  to use with SMTP_SSL.

- Documentation is updated to specify that this feature is new in 1.5.
7d25bb5
Florian Apolloner
Owner

Three things:

Thx!

Tim Graham timgraham closed this July 17, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Sep 07, 2012
Fixed #13142 -- Added support for SSL connections in core.mail.backen…
…ds.smtp

Note: SVN-based patch originally provided by serg.partizan / partizan
and Wojciech Banaś in Trac (refer to https://code.djangoproject.com/ticket/13142).

This commit includes the work done by those previous, with the following
changes:

- If use_ssl is True, or settings.EMAIL_USE_SSL is True, but the current Python
  version is less than 2.6, use_ssl is *not* internally changed to False.  Nor
  does the SMTP backend code raise an exception.  We don't need to, because Django 1.5
  is dropping support for Python prior to 2.6.

- This update provides the ability to configure the keyfile and certfile
  to use with SMTP_SSL.

- Documentation is updated to specify that this feature is new in 1.5.
7d25bb5
This page is out of date. Refresh to see the latest.
3  django/conf/global_settings.py
@@ -176,6 +176,9 @@
176 176
 EMAIL_HOST_USER = ''
177 177
 EMAIL_HOST_PASSWORD = ''
178 178
 EMAIL_USE_TLS = False
  179
+EMAIL_USE_SSL = False
  180
+EMAIL_SSL_CERTFILE = None
  181
+EMAIL_SSL_KEYFILE = None
179 182
 
180 183
 # List of strings representing installed apps.
181 184
 INSTALLED_APPS = ()
22  django/core/mail/backends/smtp.py
@@ -14,6 +14,7 @@ class EmailBackend(BaseEmailBackend):
14 14
     A wrapper that manages the SMTP network connection.
15 15
     """
16 16
     def __init__(self, host=None, port=None, username=None, password=None,
  17
+                 use_ssl=None, keyfile=None, certfile=None,
17 18
                  use_tls=None, fail_silently=False, **kwargs):
18 19
         super(EmailBackend, self).__init__(fail_silently=fail_silently)
19 20
         self.host = host or settings.EMAIL_HOST
@@ -30,6 +31,18 @@ def __init__(self, host=None, port=None, username=None, password=None,
30 31
             self.use_tls = settings.EMAIL_USE_TLS
31 32
         else:
32 33
             self.use_tls = use_tls
  34
+        if use_ssl is None:
  35
+            self.use_ssl = settings.EMAIL_USE_SSL
  36
+        else:
  37
+            self.use_ssl = use_ssl
  38
+        if keyfile is None:
  39
+            self.keyfile = settings.EMAIL_SSL_KEYFILE
  40
+        else:
  41
+            self.keyfile = keyfile
  42
+        if certfile is None:
  43
+            self.certfile = settings.EMAIL_SSL_CERTFILE
  44
+        else:
  45
+            self.certfile = certfile
33 46
         self.connection = None
34 47
         self._lock = threading.RLock()
35 48
 
@@ -44,8 +57,13 @@ def open(self):
44 57
         try:
45 58
             # If local_hostname is not specified, socket.getfqdn() gets used.
46 59
             # For performance, we use the cached FQDN for local_hostname.
47  
-            self.connection = smtplib.SMTP(self.host, self.port,
48  
-                                           local_hostname=DNS_NAME.get_fqdn())
  60
+            if self.use_ssl:
  61
+                self.connection = smtplib.SMTP_SSL(self.host, self.port,
  62
+                        keyfile=self.keyfile, certfile=self.certfile,
  63
+                        local_hostname=DNS_NAME.get_fqdn())
  64
+            else:
  65
+                self.connection = smtplib.SMTP(self.host, self.port,
  66
+                        local_hostname=DNS_NAME.get_fqdn())
49 67
             if self.use_tls:
50 68
                 self.connection.ehlo()
51 69
                 self.connection.starttls()
43  docs/ref/settings.txt
@@ -979,6 +979,49 @@ Default: ``False``
979 979
 
980 980
 Whether to use a TLS (secure) connection when talking to the SMTP server.
981 981
 
  982
+.. setting:: EMAIL_USE_SSL 
  983
+
  984
+EMAIL_USE_SSL
  985
+-------------
  986
+ 
  987
+.. versionadded:: 1.5
  988
+
  989
+Default: ``False`` 
  990
+ 
  991
+Whether to use a SSL (secure) connection when talking to the SMTP server.  
  992
+ 
  993
+.. warning:: 
  994
+ 
  995
+        Requirements: Python 2.6 or higher 
  996
+
  997
+EMAIL_SSL_CERTFILE
  998
+------------------
  999
+ 
  1000
+.. versionadded:: 1.5
  1001
+
  1002
+Default: ``None`` 
  1003
+ 
  1004
+If :setting:`EMAIL_USE_SSL` is ``True``, optionally specify the path to a
  1005
+PEM-formatted certificate chain file to use for the SSL connection.
  1006
+ 
  1007
+.. warning:: 
  1008
+ 
  1009
+        Requirements: Python 2.6 or higher 
  1010
+
  1011
+EMAIL_SSL_KEYFILE
  1012
+-----------------
  1013
+ 
  1014
+.. versionadded:: 1.5
  1015
+
  1016
+Default: ``None`` 
  1017
+ 
  1018
+If :setting:`EMAIL_USE_SSL` is ``True``, optionally specify the path to a
  1019
+PEM-formatted private key file to use for the SSL connection.
  1020
+ 
  1021
+.. warning:: 
  1022
+ 
  1023
+        Requirements: Python 2.6 or higher 
  1024
+
982 1025
 .. setting:: FILE_CHARSET
983 1026
 
984 1027
 FILE_CHARSET
5  docs/topics/email.txt
@@ -27,7 +27,7 @@ Mail is sent using the SMTP host and port specified in the
27 27
 :setting:`EMAIL_HOST` and :setting:`EMAIL_PORT` settings. The
28 28
 :setting:`EMAIL_HOST_USER` and :setting:`EMAIL_HOST_PASSWORD` settings, if
29 29
 set, 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` or :setting:`EMAIL_USE_SSL` setting controls whether a secure connection is used. 
31 31
 
32 32
 .. note::
33 33
 
@@ -413,7 +413,8 @@ SMTP backend
413 413
 This is the default backend. Email will be sent through a SMTP server.
414 414
 The server address and authentication credentials are set in the
415 415
 :setting:`EMAIL_HOST`, :setting:`EMAIL_PORT`, :setting:`EMAIL_HOST_USER`,
416  
-:setting:`EMAIL_HOST_PASSWORD` and :setting:`EMAIL_USE_TLS` settings in your
  416
+:setting:`EMAIL_HOST_PASSWORD`, :setting:`EMAIL_USE_TLS`, :setting:`EMAIL_USE_SSL`,
  417
+:setting:`EMAIL_SSL_CERTFILE`, and :setting:`EMAIL_SSL_KEYFILE` settings in your
417 418
 settings file.
418 419
 
419 420
 The SMTP backend is the default configuration inherited by Django. If you
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.