Ticket #1529: auth_mail.diff
File auth_mail.diff, 2.1 KB (added by , 19 years ago) |
---|
-
mail.py
4 4 from email.MIMEText import MIMEText 5 5 import smtplib 6 6 7 # try to get the authentication settings from the master settings file, fail silently 8 # and disable if not found 9 try: 10 from django.conf.settings import EMAIL_AUTH_USER,EMAIL_AUTH_PASSWORD 11 except ImportError: 12 EMAIL_AUTH_USER = "" 13 EMAIL_AUTH_PASSWORD = "" 14 7 15 class BadHeaderError(ValueError): 8 16 pass 9 17 … … 14 22 raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name) 15 23 MIMEText.__setitem__(self, name, val) 16 24 17 def send_mail(subject, message, from_email, recipient_list, fail_silently=False ):25 def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None): 18 26 """ 19 27 Easy wrapper for sending a single message to a recipient list. All members 20 28 of the recipient list will see the other recipients in the 'To' field. 21 29 """ 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) 23 31 24 def send_mass_mail(datatuple, fail_silently=False ):32 def send_mass_mail(datatuple, fail_silently=False, auth_user=None, auth_password=None): 25 33 """ 26 34 Given a datatuple of (subject, message, from_email, recipient_list), sends 27 35 each message to each recipient list. Returns the number of e-mails sent. 28 36 29 37 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. 30 40 """ 31 41 try: 32 42 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) 33 48 except: 34 49 if fail_silently: 35 50 return