Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding headers to Django EmailMultiAlternatives

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'}
)
like image 659
1man Avatar asked Mar 30 '15 18:03

1man


1 Answers

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] and Fred <[email protected]> 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 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.

like image 107
Lycopersicum Avatar answered Sep 28 '22 10:09

Lycopersicum