Creating email templates with Django Creating email templates with Django django django

Creating email templates with Django


From the docs, to send HTML e-mail you want to use alternative content-types, like this:

from django.core.mail import EmailMultiAlternativessubject, from_email, to = 'hello', 'from@example.com', 'to@example.com'text_content = 'This is an important message.'html_content = '<p>This is an <strong>important</strong> message.</p>'msg = EmailMultiAlternatives(subject, text_content, from_email, [to])msg.attach_alternative(html_content, "text/html")msg.send()

You'll probably want two templates for your e-mail - a plain text one that looks something like this, stored in your templates directory under email.txt:

Hello {{ username }} - your account is activated.

and an HTMLy one, stored under email.html:

Hello <strong>{{ username }}</strong> - your account is activated.

You can then send an e-mail using both those templates by making use of get_template, like this:

from django.core.mail import EmailMultiAlternativesfrom django.template.loader import get_templatefrom django.template import Contextplaintext = get_template('email.txt')htmly     = get_template('email.html')d = Context({ 'username': username })subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'text_content = plaintext.render(d)html_content = htmly.render(d)msg = EmailMultiAlternatives(subject, text_content, from_email, [to])msg.attach_alternative(html_content, "text/html")msg.send()


Boys and Girls!

Since Django's 1.7 in send_email method the html_message parameter was added.

html_message: If html_message is provided, the resulting email will be a multipart/alternative email with message as the text/plain content type and html_message as the text/html content type.

So you can just:

from django.core.mail import send_mailfrom django.template.loader import render_to_stringmsg_plain = render_to_string('templates/email.txt', {'some_params': some_params})msg_html = render_to_string('templates/email.html', {'some_params': some_params})send_mail(    'email title',    msg_plain,    'some@sender.com',    ['some@receiver.com'],    html_message=msg_html,)


I have made django-templated-email in an effort to solve this problem, inspired by this solution (and the need to, at some point, switch from using django templates to using a mailchimp etc. set of templates for transactional, templated emails for my own project). It is still a work-in-progress though, but for the example above, you would do:

from templated_email import send_templated_mailsend_templated_mail(        'email',        'from@example.com',        ['to@example.com'],        { 'username':username }    )

With the addition of the following to settings.py (to complete the example):

TEMPLATED_EMAIL_DJANGO_SUBJECTS = {'email':'hello',}

This will automatically look for templates named 'templated_email/email.txt' and 'templated_email/email.html' for the plain and html parts respectively, in the normal django template dirs/loaders (complaining if it cannot find at least one of those).