Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3985 -- Added support for custom email headers.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5550 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 987f8aa257540aa310cd029f9b6c914700095ede 1 parent bcb088b
Malcolm Tredinnick authored June 27, 2007
5  django/core/mail.py
@@ -173,13 +173,14 @@ class EmailMessage(object):
173 173
     multipart_subtype = 'mixed'
174 174
 
175 175
     def __init__(self, subject='', body='', from_email=None, to=None, bcc=None,
176  
-            connection=None, attachments=None):
  176
+            connection=None, attachments=None, headers=None):
177 177
         self.to = to or []
178 178
         self.bcc = bcc or []
179 179
         self.from_email = from_email or settings.DEFAULT_FROM_EMAIL
180 180
         self.subject = subject
181 181
         self.body = body
182 182
         self.attachments = attachments or []
  183
+        self.extra_headers = headers or {}
183 184
         self.connection = connection
184 185
 
185 186
     def get_connection(self, fail_silently=False):
@@ -206,6 +207,8 @@ def message(self):
206 207
         msg['Message-ID'] = make_msgid()
207 208
         if self.bcc:
208 209
             msg['Bcc'] = ', '.join(self.bcc)
  210
+        for name, value in self.extra_headers.items():
  211
+            msg[name] = value
209 212
         return msg
210 213
 
211 214
     def recipients(self):
39  docs/email.txt
@@ -218,22 +218,41 @@ the operation. This means you can reuse the same connection (an
218 218
 E-mail messages
219 219
 ----------------
220 220
 
221  
-The ``EmailMessage`` class is initialized as follows::
  221
+The ``EmailMessage`` class is initialized with the following parameters (in
  222
+the given order, if positional arguments are used). All parameters are
  223
+optional and can be set at any time prior to calling the ``send()`` method.
222 224
 
223  
-    email = EmailMessage(subject, body, from_email, to,
224  
-                         bcc, connection, attachments)
  225
+    * ``subject``: The subject line of the e-mail.
225 226
 
226  
-All of these parameters are optional. If ``from_email`` is omitted, the value
227  
-from ``settings.DEFAULT_FROM_EMAIL`` is used. Both the ``to`` and ``bcc``
228  
-parameters are lists of addresses, as strings. The ``attachments`` parameter is
229  
-a list containing either ``(filename, content, mimetype)`` triples of
230  
-``email.MIMEBase.MIMEBase`` instances.
  227
+    * ``body``: The body text. This should be a plain text message.
  228
+
  229
+    * ``from_email``: The sender's address. Both ``fred@example.com`` and
  230
+      ``Fred <fred@example.com>`` forms are legal. If omitted, the
  231
+      ``DEFAULT_FROM_EMAIL`` setting is used.
  232
+
  233
+    * ``to``: A list or tuple of recipient addresses.
  234
+
  235
+    * ``bcc``: A list or tuple of addresses used in the "Bcc" header when
  236
+      sending the e-mail.
  237
+
  238
+    * ``connection``: An ``SMTPConnection`` instance. Use this parameter if
  239
+      you want to use the same conneciton for multiple messages. If omitted, a
  240
+      new connection is created when ``send()`` is called.
  241
+
  242
+    * ``attachments``: A list of attachments to put on the message. These can
  243
+      be either ``email.MIMEBase.MIMEBase`` instances, or ``(filename,
  244
+      content, mimetype)`` triples.
  245
+
  246
+    * ``headers``: A dictionary of extra headers to put on the message. The
  247
+      keys are the header name, values are the header values. It is up to the
  248
+      caller to ensure header names and values are in the correct format for
  249
+      an e-mail message.
231 250
 
232 251
 For example::
233 252
 
234 253
     email = EmailMessage('Hello', 'Body goes here', 'from@example.com',
235  
-                ['to1@example.com', 'to2@example.com'],
236  
-                ['bcc@example.com'])
  254
+                ['to1@example.com', 'to2@example.com'], ['bcc@example.com'],
  255
+                headers = {'Reply-To: 'another@example.com'})
237 256
 
238 257
 The class has the following methods:
239 258
 

0 notes on commit 987f8aa

Please sign in to comment.
Something went wrong with that request. Please try again.