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
  • 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

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()

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.0rc3.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

emark-1.0rc3-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file emark-1.0rc3.tar.gz.

File metadata

  • Download URL: emark-1.0rc3.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for emark-1.0rc3.tar.gz
Algorithm Hash digest
SHA256 335a22363f87f56be8b85d6bc0ea1891a64ed69a269b553c32fe516d037a8b26
MD5 2936d31b3ca921bfc327d07d6017d5e5
BLAKE2b-256 2ab5329c09d4e413cc439b22d69f33c4544cebd952ec29985c07eeb005c1995c

See more details on using hashes here.

File details

Details for the file emark-1.0rc3-py3-none-any.whl.

File metadata

  • Download URL: emark-1.0rc3-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for emark-1.0rc3-py3-none-any.whl
Algorithm Hash digest
SHA256 f1005d1d0b0762109372763cb1aedcc8d2f8b9e7cb4dd01da8e9e7493d5e4bc3
MD5 0df327868ae8d504167a5c3680b3cfdb
BLAKE2b-256 8be8645a54b463d7803b9fafa56ce8d18e6b913a8a368f6d7960c2d0ff0460ba

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