Prior to Django 1.7 (currently in dev), we don’t have any option to pass html version of an email to “send_mail()” from django.core.mail module. Even in the newer version, you can pass html template as an alternative but you have to pass a plain text version as well. What if you want to send html version, only? This is what I did –
1 2 3 4 5 6 |
from django.core.mail import EmailMessage def send_email(to_list, subject, message, sender="Aircourts <noreply@aircourts.com>"): msg = EmailMessage(subject, message, sender, to_list) msg.content_subtype = "html" # Main content is now text/html return msg.send() |
Now you would pass html as the message parameter. I use django templates with “render_to_string()” function from django.template.loader module.
Do you know any other, perhaps smarter way to accomplish this? Please share in the comments! 🙂
5 replies on “Django: Sending HTML Only Email”
pretty awesome worked right out of the box
That was very helpful, thanks!
Perfect. Thanks!
[…] it. As-is Django wants you to send a plain text version of your email along with the HTML version. This post shows how to get around that requirement. I should also mention that the email had a few images in […]
Thanks, it is not nessesary complicated ourselves, this works so fine, thanks.