Skip to content

Commit

Permalink
Fixed #3985 -- Added support for custom email headers.
Browse files Browse the repository at this point in the history
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5550 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
malcolmt committed Jun 27, 2007
1 parent bcb088b commit 987f8aa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
5 changes: 4 additions & 1 deletion django/core/mail.py
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
39 changes: 29 additions & 10 deletions docs/email.txt
Expand Up @@ -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:

Expand Down

0 comments on commit 987f8aa

Please sign in to comment.