Skip to main content

Utility for sending HTML emails from Django.

Project description

Django HTML Emailer

A utility app for sending HTML emails in Django 1.7+:

Installation

Install this module:

pip install django-html-emailer

Add htmlemailer to your Django settings's INSTALLED_APPS.

Basic Usage

Here's a quick example for how to send a message:

from htmlemailer import send_mail

send_mail(
	"htmlemailer/example",
	"My Site <mysite@example.org>",
	["you@recipient.com"],
	{
		"my_message": "Hello & good day to you!"
	})

Replace the recipient address with your email address. This should send you a message using the example template.

Your Templates

htmlemailer composes your actual email from a series of templates. Usually you have:

  1. A template storing the actual content of your email (either a Markdown template or a pair of templates, one for the HTML part and one for the plain text part), which extends...
  2. A template that has the general design of all of your emails (CSS, header, footer), akin to your base.html for your site (a pair of templates, one for HTML and one for text), which extends...
  3. The HTML Email Boilerplate, which we've already converted into a template.
  4. A ..._subject.txt template which generates the subject line of the email (it's also a template so you can use variables etc. in it).

First copy the example "general design" template files into your project's templates path, naming them as you like. Copy them from:

Then copy the example "actual content" template files into your project:

and either

if you want to use a single Markdown file or

if you want to explicitly set the text and HTML parts of the message separately.

You can change the path and file names, except the set of files must have the same path name up to .md, .txt, .html, and _subject.txt. That's how the module knows they go together (note how you don't include the file extension in the call to send_mail).

If you changed the path of the general design templates, you'll have to update the {% extends ... %} template tags in example.md or example.txt and example.html to point to the new path. You can of course have more than one email by creating a new set of .md, .txt, .html, and _subject.txt files at a different path.

Lastly, in your call to send_mail, update the first argument to specify the location of your email templates. Just specify the common part of the path name of the three files. In this case, it's just htmlemailer/example. The .md, .txt, .html, and _subject.txt will be added by the library.

Advanced Usage

send_mail also takes an optional fail_silently boolean argument (default is False), and it passes other keyword arguments on to Django's EmailMessage constructor, so you can also pass headers and connection.

If DEFAULT_TEMPLATE_CONTEXT is set in your settings, then it should be a dictionary with default template context variables passed into your email templates.

Notes on Markdown

Markdown messages are rendered into HTML using CommonMark (specification, library). The text part of the message is rendered using a special Markdown-to-text renderer, because raw Markdown doesn't always look professional (especially links and images).

The Markdown is rendered first prior to running the Django template engine. So you cannot cause Markdown to be inserted into the email through template context variables. This is by design.

Also note that the {% extends ... %} tag at the top of the Markdown message body template does not contain the .txt or .html file extension. The library inserts the right file extension for the general design template prior to rendering into HTML and text.

Note: The CommonMark library is monkey-patched to turn off escaping of {'s and }'s in URLs (to allow for template tags to appear within links). If you are using CommonMark elsewhere in your application, that might affect you if you are creating Markdown documents with these characters in URLs (which is probably bad anyway).

Testing (Library Developers)

A test Django project is included. To use:

cd test_project
pip3 install inlinestyler commonmark commonmarkextensions
python3 manage.py test_html_email example
python3 manage.py test_html_email example2

This will output test emails (a MIME message) to the console. example uses separate text and HTML parts. example2 uses a single Markdown body file.

License

This project and the (upstream) boilerplate code are available under the MIT license.

For Project Maintainers

To publish a universal wheel to pypi, update the version number in setup.py, then:

pip3 install twine
rm -rf dist
python3 setup.py bdist_wheel --universal
twine upload dist/*
git tag v1.0.XXX
git push --tags

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_html_emailer-0.1.0-py2.py3-none-any.whl (12.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django_html_emailer-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: django_html_emailer-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

File hashes

Hashes for django_html_emailer-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1c0db5026d59ebecb3b24504b849830b379302f667d29c02d8ced8ad82920ffd
MD5 39e4607aed060c5125fa6ae2b8d3cf71
BLAKE2b-256 9f259cb0d273496d6c04f6274551cc172847604f4296bc7971b18e111f2b9acd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page