Fixed #1529 -- Added support for authenticated SMTP to django.core.mail. Thanks, Bruce Kroeze
git-svn-id: http://code.djangoproject.com/svn/django/trunk@2548 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
e67f1a680d
commit
fe24eca81a
3
AUTHORS
3
AUTHORS
|
@ -56,7 +56,6 @@ answer newbie questions, and generally made Django that much better:
|
||||||
gandalf@owca.info
|
gandalf@owca.info
|
||||||
Baishampayan Ghose
|
Baishampayan Ghose
|
||||||
Espen Grindhaug <http://grindhaug.org/>
|
Espen Grindhaug <http://grindhaug.org/>
|
||||||
Gustavo Picon
|
|
||||||
Brant Harris
|
Brant Harris
|
||||||
hipertracker@gmail.com
|
hipertracker@gmail.com
|
||||||
Ian Holsman <http://feh.holsman.net/>
|
Ian Holsman <http://feh.holsman.net/>
|
||||||
|
@ -68,6 +67,7 @@ answer newbie questions, and generally made Django that much better:
|
||||||
Russell Keith-Magee <freakboy@iinet.net.au>
|
Russell Keith-Magee <freakboy@iinet.net.au>
|
||||||
Garth Kidd <http://www.deadlybloodyserious.com/>
|
Garth Kidd <http://www.deadlybloodyserious.com/>
|
||||||
Sune Kirkeby <http://ibofobi.dk/>
|
Sune Kirkeby <http://ibofobi.dk/>
|
||||||
|
Bruce Kroeze <http://coderseye.com/>
|
||||||
lakin.wecker@gmail.com
|
lakin.wecker@gmail.com
|
||||||
Stuart Langridge <http://www.kryogenix.org/>
|
Stuart Langridge <http://www.kryogenix.org/>
|
||||||
Eugene Lazutkin <http://lazutkin.com/blog/>
|
Eugene Lazutkin <http://lazutkin.com/blog/>
|
||||||
|
@ -88,6 +88,7 @@ answer newbie questions, and generally made Django that much better:
|
||||||
oggie rob <oz.robharvey@gmail.com>
|
oggie rob <oz.robharvey@gmail.com>
|
||||||
pgross@thoughtworks.com
|
pgross@thoughtworks.com
|
||||||
phaedo <http://phaedo.cx/>
|
phaedo <http://phaedo.cx/>
|
||||||
|
Gustavo Picon
|
||||||
Luke Plant <http://lukeplant.me.uk/>
|
Luke Plant <http://lukeplant.me.uk/>
|
||||||
plisk
|
plisk
|
||||||
Daniel Poelzleithner <http://poelzi.org/>
|
Daniel Poelzleithner <http://poelzi.org/>
|
||||||
|
|
|
@ -87,6 +87,10 @@ DATABASE_PORT = '' # Set to empty string for default. Not used with
|
||||||
# Host for sending e-mail.
|
# Host for sending e-mail.
|
||||||
EMAIL_HOST = 'localhost'
|
EMAIL_HOST = 'localhost'
|
||||||
|
|
||||||
|
# Optional SMTP authentication information for EMAIL_HOST.
|
||||||
|
EMAIL_HOST_USER = ''
|
||||||
|
EMAIL_HOST_PASSWORD = ''
|
||||||
|
|
||||||
# List of strings representing installed apps.
|
# List of strings representing installed apps.
|
||||||
INSTALLED_APPS = ()
|
INSTALLED_APPS = ()
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# Use this module for e-mailing.
|
# Use this module for e-mailing.
|
||||||
|
|
||||||
from django.conf.settings import DEFAULT_FROM_EMAIL, EMAIL_HOST, EMAIL_SUBJECT_PREFIX
|
from django.conf.settings import DEFAULT_FROM_EMAIL, EMAIL_HOST, EMAIL_SUBJECT_PREFIX, EMAIL_HOST_USER, EMAIL_HOST_PASSWORD
|
||||||
from email.MIMEText import MIMEText
|
from email.MIMEText import MIMEText
|
||||||
import smtplib
|
import smtplib
|
||||||
|
|
||||||
|
@ -14,22 +14,25 @@ class SafeMIMEText(MIMEText):
|
||||||
raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name)
|
raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name)
|
||||||
MIMEText.__setitem__(self, name, val)
|
MIMEText.__setitem__(self, name, val)
|
||||||
|
|
||||||
def send_mail(subject, message, from_email, recipient_list, fail_silently=False):
|
def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=EMAIL_HOST_USER, auth_password=EMAIL_HOST_PASSWORD):
|
||||||
"""
|
"""
|
||||||
Easy wrapper for sending a single message to a recipient list. All members
|
Easy wrapper for sending a single message to a recipient list. All members
|
||||||
of the recipient list will see the other recipients in the 'To' field.
|
of the recipient list will see the other recipients in the 'To' field.
|
||||||
"""
|
"""
|
||||||
return send_mass_mail([[subject, message, from_email, recipient_list]], fail_silently)
|
return send_mass_mail([[subject, message, from_email, recipient_list]], fail_silently, auth_user, auth_password)
|
||||||
|
|
||||||
def send_mass_mail(datatuple, fail_silently=False):
|
def send_mass_mail(datatuple, fail_silently=False, auth_user=EMAIL_HOST_USER, auth_password=EMAIL_HOST_PASSWORD):
|
||||||
"""
|
"""
|
||||||
Given a datatuple of (subject, message, from_email, recipient_list), sends
|
Given a datatuple of (subject, message, from_email, recipient_list), sends
|
||||||
each message to each recipient list. Returns the number of e-mails sent.
|
each message to each recipient list. Returns the number of e-mails sent.
|
||||||
|
|
||||||
If from_email is None, the DEFAULT_FROM_EMAIL setting is used.
|
If from_email is None, the DEFAULT_FROM_EMAIL setting is used.
|
||||||
|
If auth_user and auth_password are set, they're used to log in.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
server = smtplib.SMTP(EMAIL_HOST)
|
server = smtplib.SMTP(EMAIL_HOST)
|
||||||
|
if auth_user and auth_password:
|
||||||
|
server.login(auth_user, auth_password)
|
||||||
except:
|
except:
|
||||||
if fail_silently:
|
if fail_silently:
|
||||||
return
|
return
|
||||||
|
@ -49,11 +52,11 @@ def send_mass_mail(datatuple, fail_silently=False):
|
||||||
return num_sent
|
return num_sent
|
||||||
|
|
||||||
def mail_admins(subject, message, fail_silently=False):
|
def mail_admins(subject, message, fail_silently=False):
|
||||||
"Sends a message to the admins, as defined by the ADMINS constant in settings.py."
|
"Sends a message to the admins, as defined by the ADMINS setting."
|
||||||
from django.conf.settings import ADMINS, SERVER_EMAIL
|
from django.conf.settings import ADMINS, SERVER_EMAIL
|
||||||
send_mail(EMAIL_SUBJECT_PREFIX + subject, message, SERVER_EMAIL, [a[1] for a in ADMINS], fail_silently)
|
send_mail(EMAIL_SUBJECT_PREFIX + subject, message, SERVER_EMAIL, [a[1] for a in ADMINS], fail_silently)
|
||||||
|
|
||||||
def mail_managers(subject, message, fail_silently=False):
|
def mail_managers(subject, message, fail_silently=False):
|
||||||
"Sends a message to the managers, as defined by the MANAGERS constant in settings.py"
|
"Sends a message to the managers, as defined by the MANAGERS setting."
|
||||||
from django.conf.settings import MANAGERS, SERVER_EMAIL
|
from django.conf.settings import MANAGERS, SERVER_EMAIL
|
||||||
send_mail(EMAIL_SUBJECT_PREFIX + subject, message, SERVER_EMAIL, [a[1] for a in MANAGERS], fail_silently)
|
send_mail(EMAIL_SUBJECT_PREFIX + subject, message, SERVER_EMAIL, [a[1] for a in MANAGERS], fail_silently)
|
||||||
|
|
|
@ -26,10 +26,12 @@ The send_mail function
|
||||||
The simplest way to send e-mail is using the function
|
The simplest way to send e-mail is using the function
|
||||||
``django.core.mail.send_mail``. Here's its definition::
|
``django.core.mail.send_mail``. Here's its definition::
|
||||||
|
|
||||||
send_mail(subject, message, from_email, recipient_list, fail_silently=False)
|
send_mail(subject, message, from_email, recipient_list,
|
||||||
|
fail_silently=False, auth_user=EMAIL_HOST_USER,
|
||||||
|
auth_password=EMAIL_HOST_PASSWORD)
|
||||||
|
|
||||||
All parameters are required except for ``fail_silently``, which is ``False`` by
|
The ``subject``, ``message``, ``from_email`` and ``recipient_list`` parameters
|
||||||
default.
|
are required.
|
||||||
|
|
||||||
* ``subject``: A string.
|
* ``subject``: A string.
|
||||||
* ``message``: A string.
|
* ``message``: A string.
|
||||||
|
@ -40,6 +42,13 @@ default.
|
||||||
* ``fail_silently``: A boolean. If it's ``False``, ``send_mail`` will raise
|
* ``fail_silently``: A boolean. If it's ``False``, ``send_mail`` will raise
|
||||||
an ``smtplib.SMTPException``. See the `smtplib docs`_ for a list of
|
an ``smtplib.SMTPException``. See the `smtplib docs`_ for a list of
|
||||||
possible exceptions, all of which are subclasses of ``SMTPException``.
|
possible exceptions, all of which are subclasses of ``SMTPException``.
|
||||||
|
* ``auth_user``: **New in Django development version.** The optional
|
||||||
|
username to use to authenticate to the SMTP server. If this isn't
|
||||||
|
provided, Django will use the value of the ``EMAIL_HOST_USER`` setting.
|
||||||
|
* ``auth_password``: **New in Django development version.** The optional
|
||||||
|
password to use to authenticate to the SMTP server. If this isn't
|
||||||
|
provided, Django will use the value of the ``EMAIL_HOST_PASSWORD``
|
||||||
|
setting.
|
||||||
|
|
||||||
.. _smtplib docs: http://www.python.org/doc/current/lib/module-smtplib.html
|
.. _smtplib docs: http://www.python.org/doc/current/lib/module-smtplib.html
|
||||||
|
|
||||||
|
@ -49,13 +58,16 @@ The send_mass_mail function
|
||||||
``django.core.mail.send_mass_mail`` is intended to handle mass e-mailing.
|
``django.core.mail.send_mass_mail`` is intended to handle mass e-mailing.
|
||||||
Here's the definition::
|
Here's the definition::
|
||||||
|
|
||||||
send_mass_mail(datatuple, fail_silently=False):
|
send_mass_mail(datatuple, fail_silently=False,
|
||||||
|
auth_user=EMAIL_HOST_USER, auth_password=EMAIL_HOST_PASSWORD):
|
||||||
|
|
||||||
``datatuple`` is a tuple in which each element is in this format::
|
``datatuple`` is a tuple in which each element is in this format::
|
||||||
|
|
||||||
(subject, message, from_email, recipient_list)
|
(subject, message, from_email, recipient_list)
|
||||||
|
|
||||||
``fail_silently`` has the same function as in ``send_mail()``.
|
``fail_silently``, ``auth_user`` and ``auth_password`` have the same functions
|
||||||
|
as in ``send_mail()``. Note that ``auth_user`` and ``auth_password`` are only
|
||||||
|
available in the Django development version.
|
||||||
|
|
||||||
Each separate element of ``datatuple`` results in a separate e-mail message.
|
Each separate element of ``datatuple`` results in a separate e-mail message.
|
||||||
As in ``send_mail()``, recipients in the same ``recipient_list`` will all see
|
As in ``send_mail()``, recipients in the same ``recipient_list`` will all see
|
||||||
|
|
|
@ -334,6 +334,30 @@ Default: ``'localhost'``
|
||||||
|
|
||||||
The host to use for sending e-mail.
|
The host to use for sending e-mail.
|
||||||
|
|
||||||
|
EMAIL_HOST_PASSWORD
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
Default: ``''`` (Empty string)
|
||||||
|
|
||||||
|
**New in Django development version.**
|
||||||
|
|
||||||
|
Username to use for the SMTP server defined in ``EMAIL_HOST``. If empty,
|
||||||
|
Django won't attempt authentication.
|
||||||
|
|
||||||
|
See also ``EMAIL_HOST_USER``.
|
||||||
|
|
||||||
|
EMAIL_HOST_USER
|
||||||
|
---------------
|
||||||
|
|
||||||
|
Default: ``''`` (Empty string)
|
||||||
|
|
||||||
|
**New in Django development version.**
|
||||||
|
|
||||||
|
Username to use for the SMTP server defined in ``EMAIL_HOST``. If empty,
|
||||||
|
Django won't attempt authentication.
|
||||||
|
|
||||||
|
See also ``EMAIL_HOST_PASSWORD``.
|
||||||
|
|
||||||
EMAIL_SUBJECT_PREFIX
|
EMAIL_SUBJECT_PREFIX
|
||||||
--------------------
|
--------------------
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue