Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #1529 -- Added support for authenticated SMTP to django.core.ma…

…il. Thanks, Bruce Kroeze

git-svn-id: http://code.djangoproject.com/svn/django/trunk@2548 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit fe24eca81aee4c75365101430940d2fbb5979bc6 1 parent e67f1a6
Adrian Holovaty authored March 22, 2006
3  AUTHORS
@@ -56,7 +56,6 @@ answer newbie questions, and generally made Django that much better:
56 56
     gandalf@owca.info
57 57
     Baishampayan Ghose
58 58
     Espen Grindhaug <http://grindhaug.org/>
59  
-    Gustavo Picon
60 59
     Brant Harris
61 60
     hipertracker@gmail.com
62 61
     Ian Holsman <http://feh.holsman.net/>
@@ -68,6 +67,7 @@ answer newbie questions, and generally made Django that much better:
68 67
     Russell Keith-Magee <freakboy@iinet.net.au>
69 68
     Garth Kidd <http://www.deadlybloodyserious.com/>
70 69
     Sune Kirkeby <http://ibofobi.dk/>
  70
+    Bruce Kroeze <http://coderseye.com/>
71 71
     lakin.wecker@gmail.com
72 72
     Stuart Langridge <http://www.kryogenix.org/>
73 73
     Eugene Lazutkin <http://lazutkin.com/blog/>
@@ -88,6 +88,7 @@ answer newbie questions, and generally made Django that much better:
88 88
     oggie rob <oz.robharvey@gmail.com>
89 89
     pgross@thoughtworks.com
90 90
     phaedo <http://phaedo.cx/>
  91
+    Gustavo Picon
91 92
     Luke Plant <http://lukeplant.me.uk/>
92 93
     plisk
93 94
     Daniel Poelzleithner <http://poelzi.org/>
4  django/conf/global_settings.py
@@ -87,6 +87,10 @@
87 87
 # Host for sending e-mail.
88 88
 EMAIL_HOST = 'localhost'
89 89
 
  90
+# Optional SMTP authentication information for EMAIL_HOST.
  91
+EMAIL_HOST_USER = ''
  92
+EMAIL_HOST_PASSWORD = ''
  93
+
90 94
 # List of strings representing installed apps.
91 95
 INSTALLED_APPS = ()
92 96
 
15  django/core/mail.py
... ...
@@ -1,6 +1,6 @@
1 1
 # Use this module for e-mailing.
2 2
 
3  
-from django.conf.settings import DEFAULT_FROM_EMAIL, EMAIL_HOST, EMAIL_SUBJECT_PREFIX
  3
+from django.conf.settings import DEFAULT_FROM_EMAIL, EMAIL_HOST, EMAIL_SUBJECT_PREFIX, EMAIL_HOST_USER, EMAIL_HOST_PASSWORD
4 4
 from email.MIMEText import MIMEText
5 5
 import smtplib
6 6
 
@@ -14,22 +14,25 @@ def __setitem__(self, name, val):
14 14
             raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name)
15 15
         MIMEText.__setitem__(self, name, val)
16 16
 
17  
-def send_mail(subject, message, from_email, recipient_list, fail_silently=False):
  17
+def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=EMAIL_HOST_USER, auth_password=EMAIL_HOST_PASSWORD):
18 18
     """
19 19
     Easy wrapper for sending a single message to a recipient list. All members
20 20
     of the recipient list will see the other recipients in the 'To' field.
21 21
     """
22  
-    return send_mass_mail([[subject, message, from_email, recipient_list]], fail_silently)
  22
+    return send_mass_mail([[subject, message, from_email, recipient_list]], fail_silently, auth_user, auth_password)
23 23
 
24  
-def send_mass_mail(datatuple, fail_silently=False):
  24
+def send_mass_mail(datatuple, fail_silently=False, auth_user=EMAIL_HOST_USER, auth_password=EMAIL_HOST_PASSWORD):
25 25
     """
26 26
     Given a datatuple of (subject, message, from_email, recipient_list), sends
27 27
     each message to each recipient list. Returns the number of e-mails sent.
28 28
 
29 29
     If from_email is None, the DEFAULT_FROM_EMAIL setting is used.
  30
+    If auth_user and auth_password are set, they're used to log in.
30 31
     """
31 32
     try:
32 33
         server = smtplib.SMTP(EMAIL_HOST)
  34
+        if auth_user and auth_password:
  35
+            server.login(auth_user, auth_password)
33 36
     except:
34 37
         if fail_silently:
35 38
             return
@@ -49,11 +52,11 @@ def send_mass_mail(datatuple, fail_silently=False):
49 52
     return num_sent
50 53
 
51 54
 def mail_admins(subject, message, fail_silently=False):
52  
-    "Sends a message to the admins, as defined by the ADMINS constant in settings.py."
  55
+    "Sends a message to the admins, as defined by the ADMINS setting."
53 56
     from django.conf.settings import ADMINS, SERVER_EMAIL
54 57
     send_mail(EMAIL_SUBJECT_PREFIX + subject, message, SERVER_EMAIL, [a[1] for a in ADMINS], fail_silently)
55 58
 
56 59
 def mail_managers(subject, message, fail_silently=False):
57  
-    "Sends a message to the managers, as defined by the MANAGERS constant in settings.py"
  60
+    "Sends a message to the managers, as defined by the MANAGERS setting."
58 61
     from django.conf.settings import MANAGERS, SERVER_EMAIL
59 62
     send_mail(EMAIL_SUBJECT_PREFIX + subject, message, SERVER_EMAIL, [a[1] for a in MANAGERS], fail_silently)
22  docs/email.txt
@@ -26,10 +26,12 @@ The send_mail function
26 26
 The simplest way to send e-mail is using the function
27 27
 ``django.core.mail.send_mail``. Here's its definition::
28 28
 
29  
-    send_mail(subject, message, from_email, recipient_list, fail_silently=False)
  29
+    send_mail(subject, message, from_email, recipient_list,
  30
+        fail_silently=False, auth_user=EMAIL_HOST_USER,
  31
+        auth_password=EMAIL_HOST_PASSWORD)
30 32
 
31  
-All parameters are required except for ``fail_silently``, which is ``False`` by
32  
-default.
  33
+The ``subject``, ``message``, ``from_email`` and ``recipient_list`` parameters
  34
+are required.
33 35
 
34 36
     * ``subject``: A string.
35 37
     * ``message``: A string.
@@ -40,6 +42,13 @@ default.
40 42
     * ``fail_silently``: A boolean. If it's ``False``, ``send_mail`` will raise
41 43
       an ``smtplib.SMTPException``. See the `smtplib docs`_ for a list of
42 44
       possible exceptions, all of which are subclasses of ``SMTPException``.
  45
+    * ``auth_user``: **New in Django development version.** The optional
  46
+      username to use to authenticate to the SMTP server. If this isn't
  47
+      provided, Django will use the value of the ``EMAIL_HOST_USER`` setting.
  48
+    * ``auth_password``: **New in Django development version.** The optional
  49
+      password to use to authenticate to the SMTP server. If this isn't
  50
+      provided, Django will use the value of the ``EMAIL_HOST_PASSWORD``
  51
+      setting.
43 52
 
44 53
 .. _smtplib docs: http://www.python.org/doc/current/lib/module-smtplib.html
45 54
 
@@ -49,13 +58,16 @@ The send_mass_mail function
49 58
 ``django.core.mail.send_mass_mail`` is intended to handle mass e-mailing.
50 59
 Here's the definition::
51 60
 
52  
-    send_mass_mail(datatuple, fail_silently=False):
  61
+    send_mass_mail(datatuple, fail_silently=False,
  62
+        auth_user=EMAIL_HOST_USER, auth_password=EMAIL_HOST_PASSWORD):
53 63
 
54 64
 ``datatuple`` is a tuple in which each element is in this format::
55 65
 
56 66
     (subject, message, from_email, recipient_list)
57 67
 
58  
-``fail_silently`` has the same function as in ``send_mail()``.
  68
+``fail_silently``, ``auth_user`` and ``auth_password`` have the same functions
  69
+as in ``send_mail()``. Note that ``auth_user`` and ``auth_password`` are only
  70
+available in the Django development version.
59 71
 
60 72
 Each separate element of ``datatuple`` results in a separate e-mail message.
61 73
 As in ``send_mail()``, recipients in the same ``recipient_list`` will all see
24  docs/settings.txt
@@ -334,6 +334,30 @@ Default: ``'localhost'``
334 334
 
335 335
 The host to use for sending e-mail.
336 336
 
  337
+EMAIL_HOST_PASSWORD
  338
+-------------------
  339
+
  340
+Default: ``''`` (Empty string)
  341
+
  342
+**New in Django development version.**
  343
+
  344
+Username to use for the SMTP server defined in ``EMAIL_HOST``. If empty,
  345
+Django won't attempt authentication.
  346
+
  347
+See also ``EMAIL_HOST_USER``.
  348
+
  349
+EMAIL_HOST_USER
  350
+---------------
  351
+
  352
+Default: ``''`` (Empty string)
  353
+
  354
+**New in Django development version.**
  355
+
  356
+Username to use for the SMTP server defined in ``EMAIL_HOST``. If empty,
  357
+Django won't attempt authentication.
  358
+
  359
+See also ``EMAIL_HOST_PASSWORD``.
  360
+
337 361
 EMAIL_SUBJECT_PREFIX
338 362
 --------------------
339 363
 

0 notes on commit fe24eca

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