Package: django-email-extras
Created byStephen McDonaldIntroductiondjango-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.Dependenciespython-gnupgis
required for sending PGP encrypted email.InstallationThe easiest way to install django-email-extras is directly from PyPi
usingpipby running the command
below:$ pip install -U django-email-extrasOtherwise you can download django-email-extras and install it directly
from source:$ python setup.py installUsageOnce installed, first addemail_extrasto yourINSTALLED_APPSsetting and run the migrations. Then there are two functions for sending email
in theemail_extras.utilsmodule:send_mailsend_mail_templateThe former mimics the signature ofdjango.core.mail.send_mailwhile 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 EmailPGP explanationUsingpython-gnupg, two
models are defined inemail_extras.models-KeyandAddresswhich 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 theKeymodel is not saved. The
email address for the key is also extracted and saved as anAddressinstance.TheAddressmodel is then used when sending email to check for
an existing key to determine whether an email should be encrypted.
When anAddressis deleted via the Django Admin, the key is
removed from the key ring on the server.Sending Multipart Email with Django TemplatesAs mentioned above, the following function is provided in
theemail_extras.utilsmodule:send_mail_template(subject, template, addr_from, addr_to,
    fail_silently=False, attachments=None, context=None,
    headers=None)The arguments that differ fromdjango.core.mail.send_mailaretemplateandcontext. Thetemplateargument 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
theemail_extrasdirectory where your templates are stored,
therefore if the namecontact_formwas given for thetemplateargument, the two template files for the email would be:templates/email_extras/contact_form.htmltemplates/email_extras/contact_form.txtTheattachmentsargument 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.Thecontextargument 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.Theheadersargument is a dictionary of extra headers to put on
the message. The keys are the header name and values are the header
values.ConfigurationThere are two settings you can configure in your project’ssettings.pymodule:EMAIL_EXTRAS_USE_GNUPG- Boolean that controls whether the PGP
encryption features are used. Defaults toTrueifEMAIL_EXTRAS_GNUPG_HOMEis specified, otherwiseFalse.EMAIL_EXTRAS_GNUPG_HOME- String representing a custom location
for the GNUPG keyring.EMAIL_EXTRAS_GNUPG_ENCODING- String representing a gnupg encoding.
Defaults to GNUPGlatin-1and could be changed to e.g.utf-8if needed.  Check outpython-gnupg docsfor more info.EMAIL_EXTRAS_ALWAYS_TRUST_KEYS- Skip key validation and assume
that used keys are always fully trusted.Local Browser TestingWhen 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 developmentsettings.pymodule: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!