Overview

django-templated-email is oriented towards sending templated emails
intended for use with transactional mailers (with support for MailchimpSTS,
and PostageApp), but as a default with a backend class which uses django’s
templating system, and django’s core.mail functions. The library supports
template inheritence, adding cc’d and bcc’d recipients, configurable
template naming and location, with easy switching between backends/providers.

The send_templated_email method can be thought of as the render_to_response
shortcut for email.

Getting going - installation

Installing:

pip install django-templated-email

You can add the following to your settings.py (but it works out the box):

TEMPLATED_EMAIL_BACKEND = 'templated_email.backends.vanilla_django.TemplateBackend'
# You can use a shortcut version
TEMPLATED_EMAIL_BACKEND = 'templated_email.backends.vanilla_django'
# You can also use a class directly
from templated_email.backends.vanilla_django import TemplateBackend
TEMPLATED_EMAIL_BACKEND = TemplateBackend

The plain part can also be calculated from the HTML using html2text. If you don’t specify the plain block and html2text package is installed, the plain part will be calculated from the HTML part. You can disable this behaviour in settings.py

TEMPLATED_EMAIL_AUTO_PLAIN = False

You can globally override the template dir, and file extension using the following variables in settings.py

For the vanilla_django and mailchimp_sts backends you can set a value for template_prefix and template_suffix (or use the less backend-portable template_dir / file_extension) for every time you call send_templated_mail, if you wish to store a set of templates in a different directory. Remember to include a trailing slash.

Please note / Warning about template inheritence

There is very basic support for template inheritence (using {% extends … %} in templates). You will run into issues if you use {{block.super}}, and will result in blank parts of emails.

Legacy Behaviour

The 0.2.x version of the library looked in django template directories/loaders
for templated_email/welcome.txt

Hey {{full_name}},
You just signed up for my website, using:
username: {{username}}
join date: {{signup_date}}
Thanks, you rock!

It will use templated_email/welcome.html for the html part
of the email allowing you to make it so much pretty.

Future Plans

Using django_templated_email in 3rd party applications

If you would like to use django_templated_email to handle mail in a reusable application, you should note that:

Your calls to send_templated_mail should set a value for template_dir, so you can keep copies of your app-specific templates local to your app (although the loader will find your email templates if you store them in <your app>/templates/templated_email, if TEMPLATED_EMAIL_TEMPLATE_DIR has not been overidden)

If you do (and you should) set a value for template_dir, remember to include a trailing slash, i.e. ‘my_app_email/’

The deployed app may use a different backend which doesn’t use the django templating backend, and as such make a note in your README warning developers that if they are using django_templated_email already, with a different backend, they will need to ensure their email provider can send all your templates (ideally enumerate those somewhere convenient)

Notes on specific backends

Using vanilla_django

This is the default backend, and as such requires no special configuration, and will work out of the box. By default it assumes the following settings (should you wish to override them):

Additionally you can call send_templated_mail and optionally override the following parameters:

template_prefix='your_template_dir/' # Override where the method looks for email templates (alternatively, use template_dir)
template_suffix='email' # Override the file extension of the email templates (alternatively, use file_extension)
cc=['fubar@example.com'] # Set a CC on the mail
bcc=['fubar@example.com'] # Set a BCC on the mail
template_dir='your_template_dir/' # Override where the method looks for email templates
connection=your_connection # Takes a django mail backend connection, created using **django.core.mail.get_connection**
auth_user='username' # Override the user that the django mail backend uses, per **django.core.mail.send_mail**
auth_password='password' # Override the password that the django mail backend uses, per **django.core.mail.send_mail**