Skip to main content

A Django package for email template editing

Project description

Coverage Code Climate

Django e-mail templates!

Many projects have a well defined set of events that trigger an outgoing e-mail, and the sensible thing is to template these out.

However, doing code updates and deployments for small template tweaks are a bit cumbersome and annoying.

This project aims to solve that - define the types of templates in the code, and edit the actual templates in the front-end. Templates are validated on syntax and required/optional content.

This project also aims to solve the HTML email template problems that you can have when supporting all email clients. This was also used by foundation for email. Foundation for email is a good way to build your initial email template on development mode. It will generate a separate html file and css file.

For e-mail sending and logging, we recommend using a solution such as Django Yubin.

This is only tested on a postgres database.

Supported are:

  • python 3.10, 3.11, 3.12

  • Django 3.2, 4.2

  • PostgreSQL

Warning

This project is currently in development and not stable.

Installation

Install with pip:

pip install mail_editor

Add ‘mail_editor’ to the installed apps:

# settings.py

INSTALLED_APPS = [
    ...
    'mail_editor',
    'ckeditor',
    ...
]

Add the urls:

# urls.py

url(r'^mail-editor/', include('mail_editor.urls', namespace='mail_editor')),

Using the template

There are 2 templates that you can use.

The _base.html, this template can not be edited by the user. This will only be rendered when the email is send.

The _outer_table.html, this template can be edited by the user and will be loaded in the editor in the admin. This template will be saved in the database with the modifications.

You can use the templates in some different ways. The shortest way is:

from mail_editor.helpers import find_template

def email_stuff():
    template = find_template('activation')

    context = {
        'name': 'Test Person',
        'site_name': 'This site',
        'activation_link': 'https://github.com/maykinmedia/mail-editor',
    }

    template.send_email('test@example.com', context)

Settings

The following settings are an example:

MAIL_EDITOR_CONF = {
    'activation': {
        'name': gettext_noop('Activation Email'),
        'description': gettext_noop('This email is used when people need to activate their account.'),
        'subject_default': 'Activeer uw account voor {{site_name}}',
        'body_default': """
            <h1>Hallo {{ name }},</h1>

            <p>Welkom! Je hebt je geregistreerd voor een {{ site_name }} account.</p>

            <p>{{ activation_link }}</p>
        """,
        'subject': [{
            'name': 'site_name',
            'description': gettext_noop('This is the name of the site. From the sites'),
            'example': gettext_noop('Example site'),
        }],
        'body': [{
            'name': 'name',
            'description': gettext_noop('This is the name of the user'),
            'example': gettext_noop('Jane Doe'),
        }, {
            'name': 'site_name',
            'description': gettext_noop('This is the name of the site. From the sites'),
            'example': gettext_noop('Example site'),
        }, {
            'name': 'activation_link',
            'description': gettext_noop('This is the link to activate their account.'),
            'example': gettext_noop('/'),
        }]
    },
    ...
}

These settings are usefull to add:

# These settings make sure that CKEDITOR does not strip any html tags. like <center></center>
CKEDITOR_CONFIGS = {
    'mail_editor': {
        'allowedContent': True,
        'contentsCss': ['/static/css/email.css'], # Enter the css file used to style the email.
        'height': 600,  # This is optional
        'entities': False, # This is added because CKEDITOR escapes the ' when you do an if statement
    }
}

You can set template variables to all of the mail templates in the following fashion.

# static dictionary
MAIL_EDITOR_BASE_CONTEXT = {
    'url': 'http://www.maykinmedia.nl',
}

# import path to callable that returns a dictionary
MAIL_EDITOR_DYNAMIC_CONTEXT = "dotted.path.to.callable"

Installation

Install with pip:

pip install mail_editor

Local development

To install and develop the library locally, use:

.. code-block:: bash

pip install -e .[tests,coverage,release]

When running management commands via django-admin, make sure to add the root directory to the python path (or use python -m django <command>):

export PYTHONPATH=. DJANGO_SETTINGS_MODULE=testapp.settings
django-admin check
# or other commands like:
django-admin migrate
django-admin createsuperuser
django-admin runserver
# django-admin makemessages -l nl

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

mail_editor-0.3.6.tar.gz (29.0 kB view details)

Uploaded Source

Built Distribution

mail_editor-0.3.6-py2.py3-none-any.whl (30.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file mail_editor-0.3.6.tar.gz.

File metadata

  • Download URL: mail_editor-0.3.6.tar.gz
  • Upload date:
  • Size: 29.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for mail_editor-0.3.6.tar.gz
Algorithm Hash digest
SHA256 2ae585bfa18ad175662480005b3d2298c9df9f8b9bbab88cbad3aa61fc529984
MD5 189848bd27f4d05ff541e4472922a029
BLAKE2b-256 96c6dfc7aace69be257b4c81318dae37511715d31c93d2bc3bafc0d0ff96b98a

See more details on using hashes here.

File details

Details for the file mail_editor-0.3.6-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for mail_editor-0.3.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e142a03264403e3fde1e918ada9259aaa647e2eee11c07e34931ec937c8372c5
MD5 5dc22660cbb7f923b866a4485c194638
BLAKE2b-256 2f80141f64915b3f5905f4f6d18cb9ef6b8fe05069163e6cd1398bdce6dbea84

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