From fe24eca81aee4c75365101430940d2fbb5979bc6 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Wed, 22 Mar 2006 19:47:15 +0000 Subject: [PATCH] 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 --- AUTHORS | 3 ++- django/conf/global_settings.py | 4 ++++ django/core/mail.py | 15 +++++++++------ docs/email.txt | 22 +++++++++++++++++----- docs/settings.txt | 24 ++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 12 deletions(-) diff --git a/AUTHORS b/AUTHORS index 1e061703c6..1c8036b74a 100644 --- a/AUTHORS +++ b/AUTHORS @@ -56,7 +56,6 @@ answer newbie questions, and generally made Django that much better: gandalf@owca.info Baishampayan Ghose Espen Grindhaug - Gustavo Picon Brant Harris hipertracker@gmail.com Ian Holsman @@ -68,6 +67,7 @@ answer newbie questions, and generally made Django that much better: Russell Keith-Magee Garth Kidd Sune Kirkeby + Bruce Kroeze lakin.wecker@gmail.com Stuart Langridge Eugene Lazutkin @@ -88,6 +88,7 @@ answer newbie questions, and generally made Django that much better: oggie rob pgross@thoughtworks.com phaedo + Gustavo Picon Luke Plant plisk Daniel Poelzleithner diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py index febd821e6a..dca3118880 100644 --- a/django/conf/global_settings.py +++ b/django/conf/global_settings.py @@ -87,6 +87,10 @@ DATABASE_PORT = '' # Set to empty string for default. Not used with # Host for sending e-mail. EMAIL_HOST = 'localhost' +# Optional SMTP authentication information for EMAIL_HOST. +EMAIL_HOST_USER = '' +EMAIL_HOST_PASSWORD = '' + # List of strings representing installed apps. INSTALLED_APPS = () diff --git a/django/core/mail.py b/django/core/mail.py index 5c31c4d812..a1e491dbc7 100644 --- a/django/core/mail.py +++ b/django/core/mail.py @@ -1,6 +1,6 @@ # 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 import smtplib @@ -14,22 +14,25 @@ class SafeMIMEText(MIMEText): raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name) 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 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 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 auth_user and auth_password are set, they're used to log in. """ try: server = smtplib.SMTP(EMAIL_HOST) + if auth_user and auth_password: + server.login(auth_user, auth_password) except: if fail_silently: return @@ -49,11 +52,11 @@ def send_mass_mail(datatuple, fail_silently=False): return num_sent 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 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): - "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 send_mail(EMAIL_SUBJECT_PREFIX + subject, message, SERVER_EMAIL, [a[1] for a in MANAGERS], fail_silently) diff --git a/docs/email.txt b/docs/email.txt index c39fbbd114..8159536d1e 100644 --- a/docs/email.txt +++ b/docs/email.txt @@ -26,10 +26,12 @@ The send_mail function The simplest way to send e-mail is using the function ``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 -default. +The ``subject``, ``message``, ``from_email`` and ``recipient_list`` parameters +are required. * ``subject``: A string. * ``message``: A string. @@ -40,6 +42,13 @@ default. * ``fail_silently``: A boolean. If it's ``False``, ``send_mail`` will raise an ``smtplib.SMTPException``. See the `smtplib docs`_ for a list of 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 @@ -49,13 +58,16 @@ The send_mass_mail function ``django.core.mail.send_mass_mail`` is intended to handle mass e-mailing. 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:: (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. As in ``send_mail()``, recipients in the same ``recipient_list`` will all see diff --git a/docs/settings.txt b/docs/settings.txt index af277f4ca2..256a1b950c 100644 --- a/docs/settings.txt +++ b/docs/settings.txt @@ -334,6 +334,30 @@ Default: ``'localhost'`` 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 --------------------