Code

Ticket #3985: mail-additional_headers.3.diff

File mail-additional_headers.3.diff, 4.3 KB (added by SmileyChris, 7 years ago)

backwards compatibility and documentation

Line 
1Index: django/core/mail.py
2===================================================================
3--- django/core/mail.py (revision 5023)
4+++ django/core/mail.py (working copy)
5@@ -34,7 +34,7 @@
6             val = Header(val, settings.DEFAULT_CHARSET)
7         MIMEText.__setitem__(self, name, val)
8 
9-def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None):
10+def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, additional_headers_dict={}):
11     """
12     Easy wrapper for sending a single message to a recipient list. All members
13     of the recipient list will see the other recipients in the 'To' field.
14@@ -46,12 +46,14 @@
15         auth_user = settings.EMAIL_HOST_USER
16     if auth_password is None:
17         auth_password = settings.EMAIL_HOST_PASSWORD
18-    return send_mass_mail([[subject, message, from_email, recipient_list]], fail_silently, auth_user, auth_password)
19+    return send_mass_mail([[subject, message, from_email, recipient_list, additional_headers_dict]], fail_silently, auth_user, auth_password)
20 
21 def send_mass_mail(datatuple, fail_silently=False, auth_user=None, auth_password=None):
22     """
23-    Given a datatuple of (subject, message, from_email, recipient_list), sends
24-    each message to each recipient list. Returns the number of e-mails sent.
25+    Given a datatuple of (subject, message, from_email, recipient_list) or
26+    (subject, message, from_email, recipient_list, additional_headers_dict),
27+    sends each message to each recipient list. Returns the number of e-mails
28+    sent.
29 
30     If from_email is None, the DEFAULT_FROM_EMAIL setting is used.
31     If auth_user and auth_password are set, they're used to log in.
32@@ -71,7 +73,13 @@
33             return
34         raise
35     num_sent = 0
36-    for subject, message, from_email, recipient_list in datatuple:
37+    for data in datatuple:
38+        if len(data) == 5:
39+            subject, message, from_email, recipient_list, additional_headers_dict = data
40+        else:
41+            # Backwards compatibility (no additional_headers_dict passed)
42+            subject, message, from_email, recipient_list = data
43+            additional_headers_dict = {}
44         if not recipient_list:
45             continue
46         from_email = from_email or settings.DEFAULT_FROM_EMAIL
47@@ -85,6 +93,10 @@
48         except AttributeError: # Python 2.3 doesn't have random.getrandbits().
49             random_bits = ''.join([random.choice('1234567890') for i in range(19)])
50         msg['Message-ID'] = "<%d.%s@%s>" % (time.time(), random_bits, DNS_NAME)
51+        core_headers = ['Subject', 'From', 'To', 'Date', 'Message-ID']
52+        for header in additional_headers_dict:
53+            if header not in core_headers:
54+                msg[header] = additional_headers_dict[header]
55         try:
56             server.sendmail(from_email, recipient_list, msg.as_string())
57             num_sent += 1
58Index: docs/email.txt
59===================================================================
60--- docs/email.txt      (revision 5023)
61+++ docs/email.txt      (working copy)
62@@ -35,7 +35,7 @@
63 
64     send_mail(subject, message, from_email, recipient_list,
65         fail_silently=False, auth_user=None,
66-        auth_password=None)
67+        auth_password=None, additional_headers_dict={})
68 
69 The ``subject``, ``message``, ``from_email`` and ``recipient_list`` parameters
70 are required.
71@@ -55,6 +55,8 @@
72     * ``auth_password``: The optional password to use to authenticate to the
73       SMTP server. If this isn't provided, Django will use the value of the
74       ``EMAIL_HOST_PASSWORD`` setting.
75+    * ``additional_headers_dict``: An optional dictionary of additional headers
76+      to use in the outgoing e-mail.
77 
78 .. _smtplib docs: http://www.python.org/doc/current/lib/module-smtplib.html
79 
80@@ -67,9 +69,11 @@
81     send_mass_mail(datatuple, fail_silently=False,
82         auth_user=None, auth_password=None):
83 
84-``datatuple`` is a tuple in which each element is in this format::
85+``datatuple`` is a tuple in which each element is either of the following
86+formats::
87 
88     (subject, message, from_email, recipient_list)
89+    (subject, message, from_email, recipient_list, additional_headers_dict)
90 
91 ``fail_silently``, ``auth_user`` and ``auth_password`` have the same functions
92 as in ``send_mail()``.