Package: django-emailmessagetemplates
© 2019 Michael O’Connor,http://www.mcoconnor.nethttps://github.com/mcoconnor/django-emailmessagetemplatesIn many cases, users will want to be able to edit the emails sent by
your application without having to go to developers to change hard-coded
email content. This package provides a Django app that allows users to
edit email content with an easy-to-integrate developer API.RequirementsDjango Email Templates supports versions 1.9-1.11 of Django under Python 2.7.django-appconfis the only external dependency.html2textis required to use the text autogeneration
functionality.InstallationDjango Email Templates is a standard Django app.To add it to a project, just include'emailmessagetemplates'in theINSTALLED_APPSsection of yoursettings.pyfile.If you would like to use text version autogeneration for HTML templates,
include thetext_autogenextras in your install (e.g.pip installdjango-emailmessagetemplates[text_autogen]) or ensure
you’ve installedhtml2textseparately.UsageThe central piece of functionality in this app is the
EmailMessageTemplate class, which is a Django model that also inherits
from Django’sEmailMultiAlternativesclass. Usage is derived from
its parents: to select a template to send, query for it as a model. To
send an email, first populate the message with the template context,
recipients, and other data, and then call thesendmethod. For
example:::from emailmessagetemplates.models import EmailMessageTemplatet = EmailMessageTemplate.objects.get(name=’Hello World’)
t.context = {‘a’:’hello’,’b’:’world’}
t.to = [‘michael@mcoconnor.net’,]
t.attach_file(‘/docs/Hello.pdf’)
t.send()Email templates support the same attributes thatEmailMultiAlternativess do, includingto,cc,bcc,from_email,headers, andattachments.HTML/Multipart MessagesDjango Email Templates can either send plain text emails or HTML
formatted messages with plain-text alternative content. To enable HTML
emails, theEMAILMESSAGETEMPLATES_ALLOW_HTML_MESSAGESsetting must
be set toTrue, and thetypefield on theEmailMessageTemplateinstance must be set to ‘HTML’. Plain text
alternative can either be auto-generated from the rendered HTML body
content (via the HTML2Text library, which converts the message to
Markdown) or by manually maintaining a separate plain text body
template.Convenience FunctionsThe email convenience functions provided by Django replicated for
message templates. These includesend_mail,send_mass_mail,mail_admins,mail_managersand are used similarly:::from emailmessagetemplates.utils import send_mail, send_mass_mail,mail_admins, mail_managerssend_mail(name, related_object=None, context={}, from_email=None,recipient_list=[], fail_silently=False, auth_user=None,
auth_password=None, connection=None)send_mass_mail(name, related_object=None, datatuple=(), fail_silently=False,auth_user=None, auth_password=None, connection=None)mail_admins(name, related_object=None, context={}, fail_silently=False,connection=None)mail_managers(name, related_object=None, context={}, fail_silently=False,connection=None)Differences fromEmailMultiAlternativesWhileEmailMessageTemplatebehaves like Django’sEmailMultiAlternativesin many ways, there are some differences:Subject and body values cannot be set directly; instead they’re
constructed from templates saved in the model rendered against the
specified contextIffrom_emailis not specified when a message is prepared, the
value defaults first to thesenderset on the template model,
then to theEMAILMESSAGETEMPLATES_DEFAULT_FROM_EMAILsettingValues required by the message (e.g the recipients) cannot be set in
theEmailMessageTemplateconstructor like they are forEmailMessage(since normally you will retrieve an existing model
instance rather than constructing one). Instead, they must be set
individually on the instance.An HTML alternative is automatically added for messages with an HTML
type (when HTML messages are permitted by application settings). A
plain text alternative is also provided, either generated from a
separate template or autogenerated from the HTML content.SettingsEMAILMESSAGETEMPLATES_DEFAULT_FROM_EMAILDefault: TheDEFAULT_FROM_EMAILvalue from your project’s settings.The default email address to use for message sent from templates. This
is can be overridden on a per-template basis by setting thesenderfield on the template model instance. It can be overridden on a
per-email basis by setting thefrom_emailattribute on an
instantiatedEmailMessageTemapleobject or using thefrom_emailargument to any of the convenience functions.EMAILMESSAGETEMPLATES_ALLOW_HTML_MESSAGESDefault: FalseIf true, templates can produce HTML-formatted messages and provide
plain-text alternative content. Enabling this option will display
additional fields in the Django admin form and will enable HTML
generation for templates that have atypeoftext/html.History0.1.3 (2019-5-11)Update for Django 1.110.1.2 (2016-5-6)Update for Django>=1.90.1.1 (2015-3-19)Fix packaging errorsAdd South initial migration0.1.0 (2015-3-18)First release on PyPI.