Skip to main content

Django email tools.

Project description

Build Status

Mailmate is a Django app comprised of tools to make dealing with emails easier. Its main feature is a simple, class-based way to define email messages using Django templates. Here’s a quick sales pitch:

from mailmate import TemplatedEmailMessage

class MyEmail(TemplatedEmailMessage):
    to = ['some-user@some-email.com']
    from_email = 'no-reply@some-email.com'
    subject = 'Hello, {{ name }}!'
    template_name = 'emails/template.txt'

MyEmail(extra_context={'name': 'Jerry'}).send()

Installation

pip install django-mailmate

TemplatedEmailMessage

Extend TemplatedEmailMessage, and set class attributes. You can override any of those attributes by passing keyword arguments to the constructor.

from mailmate import TemplatedEmailMessage

class MyEmail(TemplatedEmailMessage):
    to = ['some-user@some-email.com']
    from_email = 'no-reply@some-email.com'
    subject = 'Hello!'
    template = 'emails/template.txt'

MyEmail(to=['somebodyelse@somewhereelse.com']).send()

You can use a template to define your email body (like in the above example), or define it as a string:

from mailmate import TemplatedEmailMessage

class MyEmail(TemplatedEmailMessage):
    to = ['some-user@some-email.com']
    subject = "The subject is parsed as a {{ what }}"
    body = "The body's also parsed as a {{ what }}."

MyEmail(extra_context={'what': 'Django template!'}).send()

TemplatedEmailMessage also makes it simple to create HTML emails. Simply add an html_template_name attribute to your class (or pass it to the constructor):

from mailmate import TemplatedEmailMessage

class MyEmail(TemplatedEmailMessage):
    to = ['some-user@some-email.com']
    subject = "The subject is parsed as a {{ what }}"
    body = "The body's also parsed as a {{ what }}."
    html_template_name = 'emails/my_email.html'

The TemplatedEmailMessage class extends django.core.mail.EmailMultiAlternatives, so you don’t have to do anything special to use it with your favorite backend.

If you install markdownify, you can omit the plaintext version of your message; mailmate will generate one automatically from the HTML version. Otherwise, omitting both body and template_name will cause a MissingBody exception to be raised. If you want to send an email without a plaintext body, you must set body to an empty string explicitly.

CleanEmailBackend

Mailmate also includes a special backend to help you debug your emails. It’s like Django’s django.core.mail.backends.filebased.EmailBackend, but in addition to the *.log file, it will also save files containing the message body for each version of the message. For example, if you send an email that has both a plaintext and HTML version, it will save a *.log file (with the entire message), a *.txt file (with the plaintext body) and a *.html file (with the body of the HTML alternative).

To use it, set your EMAIL_BACKEND and EMAIL_FILE_PATH settings in settings.py:

EMAIL_BACKEND = 'mailmate.backends.CleanEmailBackend'
EMAIL_FILE_PATH = '/path/to/messages/'

Project details


Download files

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

Source Distribution

django-mailmate-1.2.0.tar.gz (19.9 kB view details)

Uploaded Source

File details

Details for the file django-mailmate-1.2.0.tar.gz.

File metadata

File hashes

Hashes for django-mailmate-1.2.0.tar.gz
Algorithm Hash digest
SHA256 65cc93c9b007dbe3e97414f761b901615ef708fa094ae454974780012dbd5605
MD5 3c7d914e73064c85fcf4ef98e83eb08a
BLAKE2b-256 ca35d31027c0e04b58f800422110fbc23ff28c4aaddfe5b807305b7bf30e4a78

See more details on using hashes here.

Supported by

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