Django

Code

Ticket #1529: auth_mail.diff

File auth_mail.diff, 2.1 kB (added by bruce@coderseye.com, 4 years ago)
  • mail.py

    old new  
    44from email.MIMEText import MIMEText 
    55import smtplib 
    66 
     7# try to get the authentication settings from the master settings file, fail silently 
     8# and disable if not found 
     9try: 
     10    from django.conf.settings import EMAIL_AUTH_USER,EMAIL_AUTH_PASSWORD 
     11except ImportError: 
     12    EMAIL_AUTH_USER = "" 
     13    EMAIL_AUTH_PASSWORD = "" 
     14 
    715class BadHeaderError(ValueError): 
    816    pass 
    917 
     
    1422            raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name) 
    1523        MIMEText.__setitem__(self, name, val) 
    1624 
    17 def send_mail(subject, message, from_email, recipient_list, fail_silently=False): 
     25def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None): 
    1826    """ 
    1927    Easy wrapper for sending a single message to a recipient list. All members 
    2028    of the recipient list will see the other recipients in the 'To' field. 
    2129    """ 
    22     return send_mass_mail([[subject, message, from_email, recipient_list]], fail_silently
     30    return send_mass_mail([[subject, message, from_email, recipient_list]], fail_silently, auth_user, auth_password
    2331 
    24 def send_mass_mail(datatuple, fail_silently=False): 
     32def send_mass_mail(datatuple, fail_silently=False, auth_user=None, auth_password=None): 
    2533    """ 
    2634    Given a datatuple of (subject, message, from_email, recipient_list), sends 
    2735    each message to each recipient list. Returns the number of e-mails sent. 
    2836 
    2937    If from_email is None, the DEFAULT_FROM_EMAIL setting is used. 
     38    If auth_user and auth_password are set, use them to log in to an authenticating 
     39    SMTP server. 
    3040    """ 
    3141    try: 
    3242        server = smtplib.SMTP(EMAIL_HOST) 
     43        if auth_user != None and auth_password != None: 
     44            server.login(auth_user, auth_password) 
     45 
     46        elif EMAIL_AUTH_USER: 
     47            server.login(EMAIL_AUTH_USER, EMAIL_AUTH_PASSWORD) 
    3348    except: 
    3449        if fail_silently: 
    3550            return