Ticket #3985: mail-additional_headers.3.diff
File mail-additional_headers.3.diff, 4.3 KB (added by , 18 years ago) |
---|
-
django/core/mail.py
34 34 val = Header(val, settings.DEFAULT_CHARSET) 35 35 MIMEText.__setitem__(self, name, val) 36 36 37 def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None ):37 def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, additional_headers_dict={}): 38 38 """ 39 39 Easy wrapper for sending a single message to a recipient list. All members 40 40 of the recipient list will see the other recipients in the 'To' field. … … 46 46 auth_user = settings.EMAIL_HOST_USER 47 47 if auth_password is None: 48 48 auth_password = settings.EMAIL_HOST_PASSWORD 49 return send_mass_mail([[subject, message, from_email, recipient_list ]], fail_silently, auth_user, auth_password)49 return send_mass_mail([[subject, message, from_email, recipient_list, additional_headers_dict]], fail_silently, auth_user, auth_password) 50 50 51 51 def send_mass_mail(datatuple, fail_silently=False, auth_user=None, auth_password=None): 52 52 """ 53 Given a datatuple of (subject, message, from_email, recipient_list), sends 54 each message to each recipient list. Returns the number of e-mails sent. 53 Given a datatuple of (subject, message, from_email, recipient_list) or 54 (subject, message, from_email, recipient_list, additional_headers_dict), 55 sends each message to each recipient list. Returns the number of e-mails 56 sent. 55 57 56 58 If from_email is None, the DEFAULT_FROM_EMAIL setting is used. 57 59 If auth_user and auth_password are set, they're used to log in. … … 71 73 return 72 74 raise 73 75 num_sent = 0 74 for subject, message, from_email, recipient_list in datatuple: 76 for data in datatuple: 77 if len(data) == 5: 78 subject, message, from_email, recipient_list, additional_headers_dict = data 79 else: 80 # Backwards compatibility (no additional_headers_dict passed) 81 subject, message, from_email, recipient_list = data 82 additional_headers_dict = {} 75 83 if not recipient_list: 76 84 continue 77 85 from_email = from_email or settings.DEFAULT_FROM_EMAIL … … 85 93 except AttributeError: # Python 2.3 doesn't have random.getrandbits(). 86 94 random_bits = ''.join([random.choice('1234567890') for i in range(19)]) 87 95 msg['Message-ID'] = "<%d.%s@%s>" % (time.time(), random_bits, DNS_NAME) 96 core_headers = ['Subject', 'From', 'To', 'Date', 'Message-ID'] 97 for header in additional_headers_dict: 98 if header not in core_headers: 99 msg[header] = additional_headers_dict[header] 88 100 try: 89 101 server.sendmail(from_email, recipient_list, msg.as_string()) 90 102 num_sent += 1 -
docs/email.txt
35 35 36 36 send_mail(subject, message, from_email, recipient_list, 37 37 fail_silently=False, auth_user=None, 38 auth_password=None )38 auth_password=None, additional_headers_dict={}) 39 39 40 40 The ``subject``, ``message``, ``from_email`` and ``recipient_list`` parameters 41 41 are required. … … 55 55 * ``auth_password``: The optional password to use to authenticate to the 56 56 SMTP server. If this isn't provided, Django will use the value of the 57 57 ``EMAIL_HOST_PASSWORD`` setting. 58 * ``additional_headers_dict``: An optional dictionary of additional headers 59 to use in the outgoing e-mail. 58 60 59 61 .. _smtplib docs: http://www.python.org/doc/current/lib/module-smtplib.html 60 62 … … 67 69 send_mass_mail(datatuple, fail_silently=False, 68 70 auth_user=None, auth_password=None): 69 71 70 ``datatuple`` is a tuple in which each element is in this format:: 72 ``datatuple`` is a tuple in which each element is either of the following 73 formats:: 71 74 72 75 (subject, message, from_email, recipient_list) 76 (subject, message, from_email, recipient_list, additional_headers_dict) 73 77 74 78 ``fail_silently``, ``auth_user`` and ``auth_password`` have the same functions 75 79 as in ``send_mail()``.