Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Django app for managing transactional email templates.
This project now requires Django 4.0+ and Python 3.9+. If you require a previous version you will have to refer to the relevant branch or tag.
This project arose out of a project to integrate a large transactional Django application with Mandrill, and the lessons learned. It also owes a minor h/t to this project from 2011 (https://github.com/hugorodgerbrown/AppMail).
The core requirement is to provide an easy way to add / edit email templates to a Django project, in such a way that it doesn't require a developer to make changes. The easiest way to use templated emails in Django is to rely on the in-built template structure, but that means that the templates are held in files, under version control, which makes it very hard for non-developers to edit.
This is not a WYSIWYG HTML editor, and it doesn't do anything clever. It doesn't handle the sending of the emails - it simply provides a convenient mechanism for storing and rendering email content.
from appmail.models import EmailTemplate, AppmailMessage
def send_order_confirmation(order_id):
order = Orders.objects.get(id=order_id)
template = EmailTemplate.objects.current('order_confirmation')
context = { "order": order }
message = AppmailMessage(
template=template,
context=context,
to=[order.recipient.email]
)
message.send()
The core requirements are:
From v2 on, it is possible to log all emails that are sent via
AppmailMessage.send
. It records the template, context and the rendered output,
so that the email can be views as sent, and resent. It will attempt to record
the User to whom the email was sent, as well as the email address. This is
dependent on there being a unique 1:1 match from email to User object, but can
prove useful in tracking emails sent to users when they change their email
address.
Individual templates are stored as model objects in the database. The standard
Django admin site is used to view / filter templates. The templates are ordered
by name, language and version. This combination is unique. The language and
version properties have sensible defaults (version=settings.LANGUAGE_CODE
and
version=0
) so don't need to set if you don't require it. There is no
inheritance or relationship between different languages and versions - they are
stored as independent objects.
# get the default order_summary email (language = settings.LANGUAGE_CODE)
template = EmailTemplate.objects.current('order_summary')
# get the french version
template = EmailTemplate.objects.current('order_summary', language='fr')
# get a specific version
template = EmailTemplate.objects.version('order_summary', 1)
Template syntax
The templates themselves use standard Django template syntax, including the use of tags, filters. There is nothing special about them, however there is one caveat - template inheritance.
Template inheritance
Although the template content is not stored on disk, without re-engineering the template rendering methods any parent templates must be. This is annoying, but there is a valid assumption behind it - if you are changing your base templates you are probably involving designers and developers already, so having to rely on a developer to make the changes is acceptable.
Sending test emails
You can send test emails to an email address through the admin list view.
The custom admin action 'Send test emails' will redirect to an intermediate page where you can enter the recipient email address and send the email:
There is also a linkon individual template admin pages (top-right, next to the history link):
There is a test suite for the app, which is best run through tox
.
MIT
Usual rules apply:
Please take care to follow the coding style - and PEP8.
FAQs
Django app for managing localised email templates.
We found that django-appmail demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.