From 987f8aa257540aa310cd029f9b6c914700095ede Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Wed, 27 Jun 2007 12:41:37 +0000 Subject: [PATCH] Fixed #3985 -- Added support for custom email headers. git-svn-id: http://code.djangoproject.com/svn/django/trunk@5550 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/mail.py | 5 ++++- docs/email.txt | 39 +++++++++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/django/core/mail.py b/django/core/mail.py index cf8b5fec13..2d9a400db5 100644 --- a/django/core/mail.py +++ b/django/core/mail.py @@ -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): diff --git a/docs/email.txt b/docs/email.txt index 16cf44014c..d81a49c2e2 100644 --- a/docs/email.txt +++ b/docs/email.txt @@ -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 `` 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: