Files
django-anymail/docs/tips/django_templates.rst
medmunds 75730e8219 Add ESP templates, batch send and merge
* message.template_id to use ESP stored templates
* message.merge_data and merge_global_data
  to supply per-recipient/global merge variables
  (with or without an ESP stored template)
* When using per-recipient merge_data, tell ESP to use
  batch send: individual message per "to" address.
  (Mailgun does this automatically; SendGrid requires
  using a different "to" field; Mandrill requires
  `preserve_recipients=False`; Postmark doesn't
  support *this type* of batch sending with merge data.)
* Allow message.from_email=None (must be set after
  init) and message.subject=None to suppress those
  fields in API calls (for ESPs that allow "From" and
  "Subject" in their template definitions).

Mailgun:
* Emulate merge_global_data by copying to
  recipient-variables for each recipient.

SendGrid:
* Add delimiters to merge field names via
  esp_extra['merge_field_format'] or
  ANYMAIL_SENDGRID_MERGE_FIELD_FORMAT setting.

Mandrill:
* Remove Djrill versions of these features;
  update migration notes.

Closes #5.
2016-05-06 12:27:11 -07:00

56 lines
2.0 KiB
ReStructuredText

.. _django-templates:
Using Django templates for email
================================
ESP's templating languages and merge capabilities are generally not compatible
with each other, which can make it hard to move email templates between them.
But since you're working in Django, you already have access to the
extremely-full-featured :mod:`Django templating system <django.template>`.
You don't even have to use Django's template syntax: it supports other
template languages (like Jinja2).
You're probably already using Django's templating system for your HTML pages,
so it can be an easy decision to use it for your email, too.
To compose email using *Django* templates, you can use Django's
:func:`~django.template.loaders.django.template.loader.render_to_string`
template shortcut to build the body and html.
Example that builds an email from the templates ``message_subject.txt``,
``message_body.txt`` and ``message_body.html``:
.. code-block:: python
from django.core.mail import EmailMultiAlternatives
from django.template import Context
from django.template.loader import render_to_string
merge_data = {
'ORDERNO': "12345", 'TRACKINGNO': "1Z987"
}
plaintext_context = Context(autoescape=False) # HTML escaping not appropriate in plaintext
subject = render_to_string("message_subject.txt", merge_data, plaintext_context)
text_body = render_to_string("message_body.txt", merge_data, plaintext_context)
html_body = render_to_string("message_body.html", merge_data)
msg = EmailMultiAlternatives(subject=subject, from_email="store@example.com",
to=["customer@example.com"], body=text_body)
msg.attach_alternative(html_body, "text/html")
msg.send()
Helpful add-ons
---------------
These (third-party) packages can be helpful for building your email
in Django:
.. TODO: flesh this out
* django-templated-mail
* Premailer, for inlining css
* BeautifulSoup, lxml, or html2text, for auto-generating plaintext from your html