Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #2897 -- Added support for TLS connections to email handling. T…

…his 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
  • Loading branch information...
commit 7a84ad93e69faf7a14342ebb4da1f6be265f1fe0 1 parent 21a2ca6
Malcolm Tredinnick authored May 03, 2007
1  django/conf/global_settings.py
@@ -119,6 +119,7 @@
119 119
 # Optional SMTP authentication information for EMAIL_HOST.
120 120
 EMAIL_HOST_USER = ''
121 121
 EMAIL_HOST_PASSWORD = ''
  122
+EMAIL_USE_TLS = False
122 123
 
123 124
 # List of strings representing installed apps.
124 125
 INSTALLED_APPS = ()
23  django/core/mail.py
@@ -70,15 +70,12 @@ class SMTPConnection(object):
70 70
     """
71 71
 
72 72
     def __init__(self, host=None, port=None, username=None, password=None,
73  
-                 fail_silently=False):
74  
-        if host is None:
75  
-            self.host = settings.EMAIL_HOST
76  
-        if port is None:
77  
-            self.port = settings.EMAIL_PORT
78  
-        if username is None:
79  
-        	self.username = settings.EMAIL_HOST_USER
80  
-        if password is None:
81  
-	        self.password = settings.EMAIL_HOST_PASSWORD
  73
+            use_tls=None, fail_silently=False):
  74
+        self.host = host or settings.EMAIL_HOST
  75
+        self.port = (port is not None) and port or settings.EMAIL_PORT
  76
+        self.username = username or settings.EMAIL_HOST_USER
  77
+        self.password = password or settings.EMAIL_HOST_PASSWORD
  78
+        self.use_tls = (use_tls is not None) and use_tls or settings.EMAIL_USE_TLS
82 79
         self.fail_silently = fail_silently
83 80
         self.connection = None
84 81
 
@@ -92,6 +89,10 @@ def open(self):
92 89
             return False
93 90
         try:
94 91
             self.connection = smtplib.SMTP(self.host, self.port)
  92
+            if self.use_tls:
  93
+                self.connection.ehlo()
  94
+                self.connection.starttls()
  95
+                self.connection.ehlo()
95 96
             if self.username and self.password:
96 97
                 self.connection.login(self.username, self.password)
97 98
             return True
@@ -104,6 +105,10 @@ def close(self):
104 105
         try:
105 106
             try:
106 107
                 self.connection.quit()
  108
+            except socket.sslerror:
  109
+                # This happens when calling quit() on a TLS connection
  110
+                # sometimes.
  111
+                self.connection.close()
107 112
             except:
108 113
                 if self.fail_silently:
109 114
                     return
4  docs/email.txt
@@ -22,7 +22,8 @@ In two lines::
22 22
 
23 23
 Mail will be sent using the SMTP host and port specified in the `EMAIL_HOST`_
24 24
 and `EMAIL_PORT`_ settings. The `EMAIL_HOST_USER`_ and `EMAIL_HOST_PASSWORD`_
25  
-settings, if set, will be used to authenticate to the SMTP server.
  25
+settings, if set, will be used to authenticate to the SMTP server and the
  26
+`EMAIL_USE_TLS`_ settings will control whether a secure connection is used.
26 27
 
27 28
 .. note::
28 29
 
@@ -34,6 +35,7 @@ settings, if set, will be used to authenticate to the SMTP server.
34 35
 .. _EMAIL_PORT: ../settings/#email-port
35 36
 .. _EMAIL_HOST_USER: ../settings/#email-host-user
36 37
 .. _EMAIL_HOST_PASSWORD: ../settings/#email-host-password
  38
+.. _EMAIL_USE_TLS: ../settings/#email-use-tls
37 39
 
38 40
 
39 41
 send_mail()
7  docs/settings.txt
@@ -428,6 +428,13 @@ Subject-line prefix for e-mail messages sent with ``django.core.mail.mail_admins
428 428
 or ``django.core.mail.mail_managers``. You'll probably want to include the
429 429
 trailing space.
430 430
 
  431
+EMAIL_USE_TLS
  432
+-------------
  433
+
  434
+Default: ``False``
  435
+
  436
+Whether to use a TLS (secure) connection when talking to the SMTP server.
  437
+
431 438
 FIXTURE_DIRS
432 439
 -------------
433 440
 

0 notes on commit 7a84ad9

Please sign in to comment.
Something went wrong with that request. Please try again.