0

I developed a Django application to send emails to researchers in different universities and help them with their research. I tested the emails using Litmus and all emails passed all the tests and showed up completely in different platforms. Also I wanted to let those who have old browsers be able to see the contents of the emails. That's why I used EmailMultiAlternatives to send both text content and the alternative html content. However, in one of the universities that I was testing the emails, everybody received incomplete emails. Also in another university, they received emails with broken lines. It was very problematic in case they saw broken hyperlinks.

The problem was related to 78 character limit in email lines explained in this Stackoverflow page.

However, 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@example.com", 'format': 'flowed'})
Community
  • 1
  • 1
1man
  • 5,216
  • 7
  • 42
  • 56
  • Consider sumarising that link. Even StackOverflow links can break with time. Thanks –  Mar 31 '15 at 04:02
  • 1
    Cleaned up formatting – James A Mohler Mar 31 '15 at 04:06
  • 1
    You can rephrase your post into a question, answer it yourself and accept your answer. This way, it is clear that the problem is resolved rather than having a forever open question. – Burhan Khalid Mar 31 '15 at 04:14
  • Thank you so much for your help with formatting, correction and nice suggestions. I actually did not answer it, because I though I might be wrong and someone else may have a better answer. As you can see, you have provided us with one. – 1man Mar 31 '15 at 04:54
  • 1
    Note that adding `{'format': 'flowed'}` to the `headers` won't do what you want. That adds a header of `format: flowed` to the email headers (which has no effect), when what you want is to add `format=flowed` to the `Content-Type` of the `text/plain` version of your email. – dhobbs Sep 08 '16 at 20:10

1 Answers1

1

However, in Django EmailMultiAlternatives documentation, there is nothing about how to add headers like "format" or "Reply-To" in EmailMultiAlternatives.

Actually, the documentation is very clear on this. If you look at the EmailMessage class documentation, it has the following:

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.

Then further down on the same page it states:

Sending alternative content types

It can be useful to include multiple versions of the content in an email; the classic example is to send both text and HTML versions of a message. With Django’s email library, you can do this using the EmailMultiAlternatives class. This subclass of EmailMessage has an attach_alternative() method for including extra versions of the message body in the email. All the other methods (including the class initialization) are inherited directly from EmailMessage.

So, from here its clear that you can use headers in EmailMultiAlternatives, as its just a thin abstraction from the main EmailMessage class.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • You are right. It was my mistake that I did not read the whole document. You know, when I am searching for a solution, I usually don't read from the beginning to the end of the document. Sometimes it helps me to solve it faster, and sometimes it is problematic. I thought someone else may face such a problem. That's why I posted it. If you think it is not necessary, we can delete the post. – 1man Mar 31 '15 at 04:51