Skip to main content

Django Template Email

django-template-email provides a set of tools that allows you to easily build plain-text or HTML emails using templates.

Usage

After installing django-templatee-email, add template_email to your INSTALLED_APPS in settings.py.

Templates

An email template is like any other django template. To use the template as an email, however, you must load the “email” templatetag library and use its tags to define the different parts of the email. The email templatetag library gives you three different tags to use: subject, body, and bodyhtml, each with their respective endsubject, endbody, and endbodyhtml.

For example

{% load email %}
{% subject %}Thank you for signing up!{% endsubject %}
{% body %}
Hello, {{ first_name }}.

Thank you for signing up. To find out more information, please visit
http://www.example.com/foo/.

Sincerely,
The Team
{% endbody %}
{% bodyhtml %}
Hello, <em>{{ first_name }}</em>.

Thank you for signing up.  To find out more information, click
<a href="http://www.example.com/foo/">here</a>.
{% endbodyhtml %}

Each tag is entirely optional. You can set any part of the email as you normally would with Djanog’s EmailMessage class.

Sending Email

The TemplateEmail class is a subclass of django.core.mail.EmailMultiAlternatives, which itself is a subclass of django.core.mail.EmailMessage.

To send your email template as an email, simply instantiate the TemplateEmail class while passing it your template and (optionally) a context dict:

from template_email import TemplateEmail

context = {'first_name': user.first_name}
email = TemplateEmail(template='email/confirmation_message.html', context)
email.send()

TemplateEmail Class

Of course, you may also extend the TemplateEmail class to suit your needs. The TemplateEmail class is initialized with optional keyword arguments of template and context. However, template and context variables may be overridden as a property as well. The TemplateEmail class has the following properties:

  • template: The template used to render the email

  • context: The context provided to the template

  • subject: The subject of the email

  • body: The plan-text body of the email

  • html: The html to attach as an alternative type

The subject, body, and html properties are intended as defaults, and will be overridden by whatever is given in the template.

When you call the send() method, the TemplateEmail class first renders the given template into the different parts of the email. The templatetags simply dump their contents into temporary context variables for the render() method use. The render method then renders the contents of each tag separately into the class’s subject, body, and bodyhtml properties.

As a convienience, the send() method will automatically convert User model instances to email recipients, formatting them as “first_name last_name <email>”.

Metadata

Release files for django-template-email 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-template-email 0.1
File Size Uploaded Provenance
django-template-email-0.1.tar.gz 3.7 kB Without provenance Details

Release files / django-template-email-0.1.tar.gz

Download URL django-template-email-0.1.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
9d819fb32146a4d40b4926010a48d42d7058722e243075627c5754df88f5fe79
BLAKE2b-256 checksum
How to use checksums
2bdf238037cc0d83b0155414612c3cb7d3a557a97ec314a7107c7996c4bc2163
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for django-template-email 0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page