In Django EmailMultiAlternatives documentation there is nothing about how to add headers like "format" or "Reply-To" in EmailMultiAlternatives
. It took a while for me to figure it out and I am sending this post to help others with saving their time.
As you can see in django's source code, EmailMultiAlternatives
inherits from EmailMessage
, so they take the same parameters in the init constructor. This way, we can add headers like:
msg = EmailMultiAlternatives(
subject, message, from_email, to_list,
headers={'Reply-To': "[email protected]", 'format': 'flowed'}
)
Back in 2015 OP complained, that there were no instructions in documentation, how to add headers such as "Format" and "Reply-To" in Django mail (django.core.mail) module. However today, while using same documentation link. We can find description and examples in 2018 easily:
class
EmailMessage
[source]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.
- subject: The subject line of the email.
- body: The body text. This should be a plain text message.
- from_email: The sender’s address. Both
[email protected]
andFred <[email protected]>
forms are legal. If omitted, theDEFAULT_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 email.
- connection: An email backend instance. Use this parameter if you want to use the same connection 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’s up to the caller to ensure header names and values are in the correct format for an email message. The corresponding attribute is
extra_headers
.- cc: A list or tuple of recipient addresses used in the “Cc” header when sending the email.
For example:
email = EmailMessage('Hello', 'Body goes here', '[email protected]', ['[email protected]', '[email protected]'], ['[email protected]'], headers = {'Reply-To': '[email protected]', 'format': 'flowed'})
As we see from examples, EmailMessage
has headers
argument (dictionary) too, EmailMultiAlternatives
according to docstring in source code is:
A version of EmailMessage that makes it easy to send multipart/alternative
messages. For example, including text and HTML versions of the text is
made easier.
So if you don't need something specific, EmailMessage
is fine, because currently EmailMultiAlternatives
is for easy inclusion of text and HTML versions of text.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With