How to send html email with django with dynamic content in it?

Django includes the django.core.mail.send_mail method these days (2018), no need to use EmailMultiAlternatives class directly. Do this instead:

from django.core import mail
from django.template.loader import render_to_string
from django.utils.html import strip_tags

subject="Subject"
html_message = render_to_string('mail_template.html', {'context': 'values'})
plain_message = strip_tags(html_message)
from_email="From <from@example.com>"
to = 'to@example.com'

mail.send_mail(subject, plain_message, from_email, [to], html_message=html_message)

This will send an email which is visible in both html-capable browsers and will show plain text in crippled email viewers.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)