Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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 malcolmt authored
Showing with 33 additions and 11 deletions.
  1. +4 −1 django/core/mail.py
  2. +29 −10 docs/email.txt
5 django/core/mail.py
View
@@ -173,13 +173,14 @@ class EmailMessage(object):
multipart_subtype = 'mixed'
def __init__(self, subject='', body='', from_email=None, to=None, bcc=None,
- connection=None, attachments=None):
+ connection=None, attachments=None, headers=None):
self.to = to or []
self.bcc = bcc or []
self.from_email = from_email or settings.DEFAULT_FROM_EMAIL
self.subject = subject
self.body = body
self.attachments = attachments or []
+ self.extra_headers = headers or {}
self.connection = connection
def get_connection(self, fail_silently=False):
@@ -206,6 +207,8 @@ def message(self):
msg['Message-ID'] = make_msgid()
if self.bcc:
msg['Bcc'] = ', '.join(self.bcc)
+ for name, value in self.extra_headers.items():
+ msg[name] = value
return msg
def recipients(self):
39 docs/email.txt
View
@@ -218,22 +218,41 @@ the operation. This means you can reuse the same connection (an
E-mail messages
----------------
-The ``EmailMessage`` class is initialized as follows::
+The ``EmailMessage`` class is initialized with the following parameters (in
+the given order, if positional arguments are used). All parameters are
+optional and can be set at any time prior to calling the ``send()`` method.
- email = EmailMessage(subject, body, from_email, to,
- bcc, connection, attachments)
+ * ``subject``: The subject line of the e-mail.
-All of these parameters are optional. If ``from_email`` is omitted, the value
-from ``settings.DEFAULT_FROM_EMAIL`` is used. Both the ``to`` and ``bcc``
-parameters are lists of addresses, as strings. The ``attachments`` parameter is
-a list containing either ``(filename, content, mimetype)`` triples of
-``email.MIMEBase.MIMEBase`` instances.
+ * ``body``: The body text. This should be a plain text message.
+
+ * ``from_email``: The sender's address. Both ``fred@example.com`` and
+ ``Fred <fred@example.com>`` forms are legal. If omitted, the
+ ``DEFAULT_FROM_EMAIL`` setting is used.
+
+ * ``to``: A list or tuple of recipient addresses.
+
+ * ``bcc``: A list or tuple of addresses used in the "Bcc" header when
+ sending the e-mail.
+
+ * ``connection``: An ``SMTPConnection`` instance. Use this parameter if
+ you want to use the same conneciton for multiple messages. If omitted, a
+ new connection is created when ``send()`` is called.
+
+ * ``attachments``: A list of attachments to put on the message. These can
+ be either ``email.MIMEBase.MIMEBase`` instances, or ``(filename,
+ content, mimetype)`` triples.
+
+ * ``headers``: A dictionary of extra headers to put on the message. The
+ keys are the header name, values are the header values. It is up to the
+ caller to ensure header names and values are in the correct format for
+ an e-mail message.
For example::
email = EmailMessage('Hello', 'Body goes here', 'from@example.com',
- ['to1@example.com', 'to2@example.com'],
- ['bcc@example.com'])
+ ['to1@example.com', 'to2@example.com'], ['bcc@example.com'],
+ headers = {'Reply-To: 'another@example.com'})
The class has the following methods:
Please sign in to comment.
Something went wrong with that request. Please try again.