Django

Code

Changeset 2548

Show
Ignore:
Timestamp:
03/22/06 13:47:15 (3 years ago)
Author:
adrian
Message:

Fixed #1529 -- Added support for authenticated SMTP to django.core.mail. Thanks, Bruce Kroeze

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r2476 r2548  
    5757    Baishampayan Ghose 
    5858    Espen Grindhaug <http://grindhaug.org/> 
    59     Gustavo Picon 
    6059    Brant Harris 
    6160    hipertracker@gmail.com 
     
    6968    Garth Kidd <http://www.deadlybloodyserious.com/> 
    7069    Sune Kirkeby <http://ibofobi.dk/> 
     70    Bruce Kroeze <http://coderseye.com/> 
    7171    lakin.wecker@gmail.com 
    7272    Stuart Langridge <http://www.kryogenix.org/> 
     
    8989    pgross@thoughtworks.com 
    9090    phaedo <http://phaedo.cx/> 
     91    Gustavo Picon 
    9192    Luke Plant <http://lukeplant.me.uk/> 
    9293    plisk 
  • django/trunk/django/conf/global_settings.py

    r2536 r2548  
    8888EMAIL_HOST = 'localhost' 
    8989 
     90# Optional SMTP authentication information for EMAIL_HOST. 
     91EMAIL_HOST_USER = '' 
     92EMAIL_HOST_PASSWORD = '' 
     93 
    9094# List of strings representing installed apps. 
    9195INSTALLED_APPS = () 
  • django/trunk/django/core/mail.py

    r1798 r2548  
    11# Use this module for e-mailing. 
    22 
    3 from django.conf.settings import DEFAULT_FROM_EMAIL, EMAIL_HOST, EMAIL_SUBJECT_PREFIX 
     3from django.conf.settings import DEFAULT_FROM_EMAIL, EMAIL_HOST, EMAIL_SUBJECT_PREFIX, EMAIL_HOST_USER, EMAIL_HOST_PASSWORD 
    44from email.MIMEText import MIMEText 
    55import smtplib 
     
    1515        MIMEText.__setitem__(self, name, val) 
    1616 
    17 def send_mail(subject, message, from_email, recipient_list, fail_silently=False): 
     17def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=EMAIL_HOST_USER, auth_password=EMAIL_HOST_PASSWORD): 
    1818    """ 
    1919    Easy wrapper for sending a single message to a recipient list. All members 
    2020    of the recipient list will see the other recipients in the 'To' field. 
    2121    """ 
    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
    2323 
    24 def send_mass_mail(datatuple, fail_silently=False): 
     24def send_mass_mail(datatuple, fail_silently=False, auth_user=EMAIL_HOST_USER, auth_password=EMAIL_HOST_PASSWORD): 
    2525    """ 
    2626    Given a datatuple of (subject, message, from_email, recipient_list), sends 
     
    2828 
    2929    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. 
    3031    """ 
    3132    try: 
    3233        server = smtplib.SMTP(EMAIL_HOST) 
     34        if auth_user and auth_password: 
     35            server.login(auth_user, auth_password) 
    3336    except: 
    3437        if fail_silently: 
     
    5053 
    5154def 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." 
    5356    from django.conf.settings import ADMINS, SERVER_EMAIL 
    5457    send_mail(EMAIL_SUBJECT_PREFIX + subject, message, SERVER_EMAIL, [a[1] for a in ADMINS], fail_silently) 
    5558 
    5659def 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.
    5861    from django.conf.settings import MANAGERS, SERVER_EMAIL 
    5962    send_mail(EMAIL_SUBJECT_PREFIX + subject, message, SERVER_EMAIL, [a[1] for a in MANAGERS], fail_silently) 
  • django/trunk/docs/email.txt

    r2122 r2548  
    2727``django.core.mail.send_mail``. Here's its definition:: 
    2828 
    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) 
    3032 
    31 All parameters are required except for ``fail_silently``, which is ``False`` by 
    32 default
     33The ``subject``, ``message``, ``from_email`` and ``recipient_list`` parameters 
     34are required
    3335 
    3436    * ``subject``: A string. 
     
    4143      an ``smtplib.SMTPException``. See the `smtplib docs`_ for a list of 
    4244      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. 
    4352 
    4453.. _smtplib docs: http://www.python.org/doc/current/lib/module-smtplib.html 
     
    5059Here's the definition:: 
    5160 
    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): 
    5363 
    5464``datatuple`` is a tuple in which each element is in this format:: 
     
    5666    (subject, message, from_email, recipient_list) 
    5767 
    58 ``fail_silently`` has the same function as in ``send_mail()``. 
     68``fail_silently``, ``auth_user`` and ``auth_password`` have the same functions 
     69as in ``send_mail()``. Note that ``auth_user`` and ``auth_password`` are only 
     70available in the Django development version. 
    5971 
    6072Each separate element of ``datatuple`` results in a separate e-mail message. 
  • django/trunk/docs/settings.txt

    r2509 r2548  
    334334 
    335335The host to use for sending e-mail. 
     336 
     337EMAIL_HOST_PASSWORD 
     338------------------- 
     339 
     340Default: ``''`` (Empty string) 
     341 
     342**New in Django development version.** 
     343 
     344Username to use for the SMTP server defined in ``EMAIL_HOST``. If empty, 
     345Django won't attempt authentication. 
     346 
     347See also ``EMAIL_HOST_USER``. 
     348 
     349EMAIL_HOST_USER 
     350--------------- 
     351 
     352Default: ``''`` (Empty string) 
     353 
     354**New in Django development version.** 
     355 
     356Username to use for the SMTP server defined in ``EMAIL_HOST``. If empty, 
     357Django won't attempt authentication. 
     358 
     359See also ``EMAIL_HOST_PASSWORD``. 
    336360 
    337361EMAIL_SUBJECT_PREFIX