Skip to main content

Markdown template based HTML and text emails for Django.

Project description

Django eMark↓

emark logo: envelope with markdown stamp

Markdown template based HTML and text emails for Django.

  • simple email templates with markdown
  • support for HTML and text emails
  • i18n support
  • built-in UTM tracking
  • built-in sent, open and click tracking
  • automatic CSS inliner via premailer

PyPi Version Test Coverage GitHub License

Setup

python3 -m pip install emark
# settings.py
INSTALLED_APPS = [
    'emark',
    # ...
]
python3 manage.py migrate

Usage

<!-- myapp/my_message.md -->
# Hello World

Hi {{ user.short_name }}!
# myapp/emails.py
from emark.message import MarkdownEmail

class MyMessage(MarkdownEmail):
    subject = "Hello World"
    template_name = "myapp/my_message.md"
# myapp/views.py
from . import emails

def my_view(request):
    message = emails.MyMessage.to_user(request.user)
    message.send()

Templates

You can use Django's template engine, just like you usually would. You can use translations, template tags, filters, blocks, etc.

You may also have a base template, that you inherit form in your individual emails to provide a consistent salutation and farewell.

<!-- base.md -->
{% load static i18n %}
{% block salutation %}Hi {{ user.short_name }}!{% endblock %}

{% block content %}{% endblock %}

{% block farewell %}
{% blocktrans trimmed %}
Best regards,
{{ site_admin }}
{% endblocktrans %}
{% endblock %}

{% block footer %}
Legal footer.
{% endblock %}
<!-- myapp/email.md -->
{% extends "base.md" %}

{% block content %}
This is the content of the email.
{% endblock %}

Context

The context is passed to the template as a dictionary. Furthermore, you may override the get_context_data method to add additional context variables.

# myapp/emails.py
from emark.message import MarkdownEmail

class MyMessage(MarkdownEmail):
    subject = "Hello World"
    template_name = "myapp/email.md"

    def get_context_data(self):
        context = super().get_context_data()
        context["my_variable"] = "Hello World"
        return context

Tracking

Sent, Open & Click Tracking

Django eMark comes with built-in tracking for sent, open and click events. The tracking is done via a tracking pixel and a redirect view.

As an added bonus, this feature also comes with an open-in-browser link that allows the user to view the email in their browser if their email client does not support HTML emails.

This feature is disabled by default. To enable it, you need to use a separate email backend. This backend will send the email via SMTP and also add the tracking pixel and redirect view. However, it will send a separate email for each recipient, which may not be desirable in all cases.

# settings.py
EMAIL_BACKEND = "emark.backends.TrackingSMTPEmailBackend"

Furthermore, you need to add the tracking view to your urls.py:

# urls.py
from django.urls import include, path

urlpatterns = [
    # … other urls
    path("emark/", include("emark.urls")),
]

You will need to provide a domain name for the tracking pixel and redirect view. This can be done via the DOMAIN setting:

# settings.py
EMARK = {
    "DOMAIN": "example.com"
}

If the site framework is installed and no settings are provided, the domain will be automatically set to the current site's domain.

The tracking data is stored in the database. You need to run migrations to create the necessary tables:

python3 manage.py migrate

You can analyze the tracking data via the tables emark_sent, emark_open and emark_click.

UTM Tracking

Every MarkdownEmail subclass comes with automatic UTM tracking. UTM parameters are added to all links in the email. Existing UTM params on link that have been explicitly set, are not overridden. The default parameters are:

  • utm_source: website
  • utm_medium: email
  • utm_campaign: {{ EMAIL_CLASS_NAME }}

The global UTM parameters can be overridden via the EMARK_UTM_PARAMS setting, which is a dictionary of parameters:

# settings.py
EMARK = {
  "UTM_PARAMS": {
      "utm_source": "website",  # default
      "utm_medium": "email",  # default
  }
}

You may also change the UTM parameters by overriding the get_utm_params or passing a utm_params dictionary to class constructor.

# myapp/emails.py
from emark.message import MarkdownEmail


class MyMessage(MarkdownEmail):
  subject = "Hello World"
  template_name = "myapp/email.md"

  # override the parameters for this email class
  def get_utm_params(self):
    return {
      "utm_source": "myapp",
      "utm_medium": "email",
      "utm_campaign": "my-campaign",
    }


# or alternatively during instantiation
MyMessage(utm_params={"utm_campaign": "my-other-campaign"}).send()

Development

Pretty HTML emails are great, unless they spam your console during development. To prevent this, you can use the ConsoleEmailBackend:

# settings.py
EMAIL_BACKEND = "emark.backends.ConsoleEmailBackend"

The ConsoleEmailBackend will only print the plain text version of the email.

Credits

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

emark-1.1rc1.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

emark-1.1rc1-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

Details for the file emark-1.1rc1.tar.gz.

File metadata

  • Download URL: emark-1.1rc1.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for emark-1.1rc1.tar.gz
Algorithm Hash digest
SHA256 8e30027a62a8d27e52f5dc4392642043bb08fc0f612694a9b10a1a7017eeac53
MD5 d4805bf1de5ef087de9e67e67a5cd76b
BLAKE2b-256 2f6e42782014442300192c3afdb6f1e93e63fd418bc66adcd3f75f94ec45dc6c

See more details on using hashes here.

File details

Details for the file emark-1.1rc1-py3-none-any.whl.

File metadata

  • Download URL: emark-1.1rc1-py3-none-any.whl
  • Upload date:
  • Size: 19.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for emark-1.1rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 2c05167772680e2c450aee8e0aeff1fd4b4f864fdc448eaf33ff1ef8edd2c1f9
MD5 3d514a0716ff3285b64804ee88dfacf4
BLAKE2b-256 0f08a7f54e69109ab8c6395ab9c76acbd679219f73aed397e74bd2de4de7d0ae

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