Skip to main content

A Django reusable app providing the ability to send PGP encrypted and multipart emails using the Django templating system.

Project description

Created by Stephen McDonald

Introduction

django-email-extras is a Django reusable app providing the ability to send PGP encrypted and multipart emails using Django templates. These features can be used together or separately. When configured to send PGP encrypted email, the ability for Admin users to manage PGP keys is also provided.

A tool for automatically opening multipart emails in a local web browser during development is also provided.

Dependencies

Installation

The easiest way to install django-email-extras is directly from PyPi using pip by running the command below:

$ pip install -U django-email-extras

Otherwise you can download django-email-extras and install it directly from source:

$ python setup.py install

Usage

Once installed, first add email_extras to your INSTALLED_APPS setting and run the migrations. Then there are two functions for sending email in the email_extras.utils module:

  • send_mail

  • send_mail_template

The former mimics the signature of django.core.mail.send_mail while the latter provides the ability to send multipart emails using the Django templating system. If configured correctly, both these functions will PGP encrypt emails as described below.

Sending PGP Encrypted Email

PGP explanation

Using python-gnupg, two models are defined in email_extras.models - Key and Address which represent a PGP key and an email address for a successfully imported key. These models exist purely for the sake of importing keys and removing keys for a particular address via the Django Admin.

When adding a key, the key is imported into the key ring on the server and the instance of the Key model is not saved. The email address for the key is also extracted and saved as an Address instance.

The Address model is then used when sending email to check for an existing key to determine whether an email should be encrypted. When an Address is deleted via the Django Admin, the key is removed from the key ring on the server.

Sending Multipart Email with Django Templates

As mentioned above, the following function is provided in the email_extras.utils module:

send_mail_template(subject, template, addr_from, addr_to,
    fail_silently=False, attachments=None, context=None,
    headers=None)

The arguments that differ from django.core.mail.send_mail are template and context. The template argument is simply the name of the template to be used for rendering the email contents.

A template consists of both a HTML file and a TXT file each responsible for their respective versions of the email and should be stored in the email_extras directory where your templates are stored, therefore if the name contact_form was given for the template argument, the two template files for the email would be:

  • templates/email_extras/contact_form.html

  • templates/email_extras/contact_form.txt

The attachments argument is a list of files to attach to the email. Each attachment can be the full filesystem path to the file, or a file name / file data pair.

The context argument is simply a dictionary that is used to populate the email templates, much like a normal request context would be used for a regular Django template.

The headers argument is a dictionary of extra headers to put on the message. The keys are the header name and values are the header values.

Configuration

There are two settings you can configure in your project’s settings.py module:

  • EMAIL_EXTRAS_USE_GNUPG - Boolean that controls whether the PGP encryption features are used. Defaults to True if EMAIL_EXTRAS_GNUPG_HOME is specified, otherwise False.

  • EMAIL_EXTRAS_GNUPG_HOME - String representing a custom location for the GNUPG keyring.

  • EMAIL_EXTRAS_GNUPG_ENCODING - String representing a gnupg encoding. Defaults to GNUPG latin-1 and could be changed to e.g. utf-8 if needed. Check out python-gnupg docs for more info.

  • EMAIL_EXTRAS_ALWAYS_TRUST_KEYS - Skip key validation and assume that used keys are always fully trusted.

Local Browser Testing

When sending multipart emails during development, it can be useful to view the HTML part of the email in a web browser, without having to actually send emails and open them in a mail client. To use this feature during development, simply set your email backend as follows in your development settings.py module:

EMAIL_BACKEND = 'email_extras.backends.BrowsableEmailBackend'

With this configured, each time a multipart email is sent, it will be written to a temporary file, which is then automatically opened in a local web browser. Suffice to say, this should only be enabled during development!

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-email-extras-0.3.4.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

django_email_extras-0.3.4-py2.py3-none-any.whl (16.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-email-extras-0.3.4.tar.gz.

File metadata

  • Download URL: django-email-extras-0.3.4.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/2.7

File hashes

Hashes for django-email-extras-0.3.4.tar.gz
Algorithm Hash digest
SHA256 eb1a1ec4edc20776010e09087c8516474abd4e7d88a91d64350dddf6e917e537
MD5 1e02312bbac85ec1bba85654743e152d
BLAKE2b-256 01ea49e81122884ec1dd1b2e3c7574b7417b280fd27ea8898a240c7bd7c4df87

See more details on using hashes here.

File details

Details for the file django_email_extras-0.3.4-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_email_extras-0.3.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 208aec64d5e3a5c5afb8bab007a22c24e1566b44d4dd3f98137296726d76ce14
MD5 6aeee62dea1713913c6281f73ab99b32
BLAKE2b-256 3d9e213021fa55c416af204b2a2736e8986feb6cae638c4d2a9ca5e860202daf

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