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.3.0.tar.gz (20.0 kB view details)

Uploaded Source

File details

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

File metadata

File hashes

Hashes for django-mailmate-1.3.0.tar.gz
Algorithm Hash digest
SHA256 efd90549c6fc094ed00a3f75383571f8ad1c01c6c19c34809cf4194aa18aa2f3
MD5 637fc8dbee7f56ae4a7c3b09359114e5
BLAKE2b-256 89e64dfe014afd44a4aa2da553ad3219396b4d53da431a88d277ddf2d5f51bbe

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