django-templated-email

raw JSON →
3.1.1 verified Mon Apr 27 auth: no python

A Django-oriented templated/transactional email abstraction library that supports multiple backends (e.g., Mandrill, SendGrid, SES) and allows you to send emails using Django templates. Current version 3.1.1 supports Django 4.2, 5.0, 5.1 and Python 3.10-3.13. Release cadence is sporadic.

pip install django-templated-email
error ModuleNotFoundError: No module named 'django_templated_email'
cause The import path uses the wrong module name (common mistake).
fix
Use from templated_email import send_templated_email instead.
error KeyError: 'TEMPLATED_EMAIL_BACKEND'
cause TEMPLATED_EMAIL_BACKEND setting is missing or not configured correctly.
fix
Add TEMPLATED_EMAIL_BACKEND = 'templated_email.backends.vanilla_mail.TemplateBackend' to your Django settings (or another backend).
error django.core.exceptions.ImproperlyConfigured: Requested setting EMAIL_BACKEND, but settings are not configured.
cause Django settings are not configured before using the library (e.g., in standalone script).
fix
Set up Django settings with os.environ['DJANGO_SETTINGS_MODULE'] and call django.setup() before sending emails.
breaking In version 3.0.0, HTML parts in templates are now escaped by default. If you relied on unescaped HTML, you need to use the 'autoescape' block.
fix Wrap your HTML content in {% autoescape off %}...{% endautoescape %} if you need raw HTML.
deprecated Python 3.6, 3.7, 3.8 and Django 2.2, 3.1, 3.2 are no longer supported as of version 3.1.0.
fix Upgrade to Python >=3.10 and Django >=4.2.
gotcha The library expects template files to be in a 'templated_email/' directory under your templates. If templates are missing, it silently falls back to plain text without error.
fix Ensure your email templates are located at: <app>/templates/templated_email/<template_name>.email (or .txt for plain text).
gotcha Calling send_templated_email without an explicit 'from_email' parameter will raise an error if settings.DEFAULT_FROM_EMAIL is not set.
fix Set DEFAULT_FROM_EMAIL in settings or always pass from_email.

Sends an email using a Django template named 'welcome'. Requires Django settings for email backend (e.g., SMTP).

import os
from templated_email import send_templated_email

send_templated_email(
    template_name='welcome',
    from_email='from@example.com',
    recipient_list=['to@example.com'],
    context={'username': 'John', 'site_url': 'http://example.com'},
    # For authentication, set EMAIL_HOST_USER and EMAIL_HOST_PASSWORD in Django settings.
)