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

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5550 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2007-06-27 12:41:37 +00:00
parent bcb088b558
commit 987f8aa257
2 changed files with 33 additions and 11 deletions

View File

@ -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 @@ class EmailMessage(object):
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):

View File

@ -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: