Getting started

Installation

Run:

pip install django-mail-templated

And register the app in your settings file:

INSTALLED_APPS = (
    ...
    'mail_templated'
)

It is also a good practice to ensure that the app is installed successfully and is fully compatible with your environment:

python manage.py test mail_templated

Creating templates

Each email template should extend mail_templated/base.tpl or its clone either directly or via descendants.

Note: newlines at the begin and end of message part blocks will be removed.

Plain text message:

{% extends "mail_templated/base.tpl" %}

{% block subject %}
Hello {{ user.name }}
{% endblock %}

{% block body %}
This is a plain text message.
{% endblock %}

HTML message:

{% extends "mail_templated/base.tpl" %}

{% block subject %}
Hello {{ user.name }}
{% endblock %}

{% block html %}
This is an <strong>html</strong> message.
{% endblock %}

Multipart message:

{% extends "mail_templated/base.tpl" %}

{% block subject %}
Hello {{ user.name }}
{% endblock %}

{% block body %}
This is a plain text part.
{% endblock %}

{% block html %}
This is an <strong>html</strong> part.
{% endblock %}

Sending messages

Fast method using send_mail() function:

from mail_templated import send_mail

send_mail('email/hello.tpl', {'user': user}, from_email, [user.email])

More control with EmailMessage class:

from mail_templated import EmailMessage

message = EmailMessage('email/hello.tpl', {'user': user}, from_email,
                       to=[user.email])
# TODO: Add more useful commands here.
message.send()

Proceed to the Advanced usage section to find more details.