django-mail-analytics
Simple python module that add email tracking features for both mail opening (via a hidden pixel) and for link clicks (via a redirecting proxy)
- This works only for email with an html body
- The changes to your code is minimal as we monkey patch the standard django.core.mail.EmailMessage object
- Three new models are added to save mail sent, mail recipients and mail recipient's action such as opening and link clicking
Installation
- Run
pip install django-mail-analytics - Add
django_mail_analyticsto your installed INSTALLED_APPS - Add
re_path(r"^m/", include("django_mail_analytics.urls"))to your urls - Add this to your settings:
MAIL_ANALYTICS = {
"DOMAIN": "localhost", #add the domain of your app to be used in the pixel/proxy url of your mail sent
"SCHEME": "http", #http or https
# "SALT": "my-salt", #comment to use global SECRET_KEY instead
"LENGTH": 6, # minimal length of encoded id
}
Usage
All email sent with an html body will be modified on the fly to incorporate a pixel tracking and to rewrite link urls:
from django.core.mail import send_mail
send_mail(
"Subject here",
"Here is the message.",
"from@example.com",
["to@example.com"],
fail_silently=False,
html_message="""
<body>
<a href="https://google.com">link</a>
</body>
""",
)
The above code will actually send an html body email that is rewritten like this:
<body>
<a href="http://localhost/m/p?q=OJe2wr&u=https%3A%2F%2Fgoogle.com">link</a>
<img src="http://localhost/m/i?q=OJe2wr" height="0px" width="0px"/>
</body>
There is no perciple change for the user that receive the email. But as soon as it opens (or reopens) the email, we create a MailRecipientAction with an empty action. If the user clicks on the link we would create another MailRecipientAction with an action equal to the link url: "https://google.com" in this example.
Every email send trigger the creation of a Mail object. In addition, a MailRecipient object is also created for every recipients of the email.
Note, that multiple emails sharing the same "key" and sending "date" are merged together into a single Mail Object. So no need to worry about sending mass emails (they will only create one single Mail object). The email "key" is either the email subject OR the first recipient with a "@DMA" email address. This recipient is ignored from the sending list, it's just an easy way to track mails of a given category.
So typical usage is like this:
send_mail(
subject="subject",
message="message",
html_message="html_message,
from_email=None,
recipient_list=[user.email, f"EMAIL_KEY@DMA"],
)
Admin
There is a django admin for the three added models that can easilly be used to monitor your email success rate and typical use behaviour.
Remarks
Email were not developped for tracking in the first place. So adding a pixel tracking and link proxy is just a workaround to add tracking capabilities to email. One of the limitation of this workaround is that sending the email to multiple recipients doesn't allow to distinguish who among the recipients is currently opening the email... just that someone did it. Similarly if the email is forwarded to somebody else, we can only know that the email was opened a second time, but not that it's from a different people.
Testing
# clone repository
git clone https://github.com/quertenmont/django-mail-analytics.git && cd django_mail_analytics
# create virtualenv and activate it
python -m venv venv && . venv/bin/activate
# upgrade pip
python -m pip install --upgrade pip
# install requirements
pip install -r requirements.txt -r requirements-test.txt
# install pre-commit to run formatters and linters
pre-commit install --install-hooks
# run tests
tox
# or
python runtests.py
# or
python -m django test --settings "tests.settings"
License
Released under MIT License.
Supporting
Release files for django-mail-analytics 1.0.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_mail_analytics-1.0.9.tar.gz | 16.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_mail_analytics-1.0.9-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 30.1 kB
Release files / django_mail_analytics-1.0.9.tar.gz
| Download URL | django_mail_analytics-1.0.9.tar.gz |
|---|---|
| Size | 16.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0a8e716d24b4c14530bdda9759cb6cf0370f2a6081ea5834bdf0fa7f1cb374d6
|
|
BLAKE2b-256 checksum How to use checksums |
da55e4b3a862262fc671441a12f22b762c20c425ca1e7e446ed337c139d0f01c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 8, 2026.
Transparency logRelease files / django_mail_analytics-1.0.9-py3-none-any.whl
| Download URL | django_mail_analytics-1.0.9-py3-none-any.whl |
|---|---|
| Size | 13.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8c86449ba1a39571dd8be6662ad6e0869660745235a5ddc3913679185cb7cebd
|
|
BLAKE2b-256 checksum How to use checksums |
027a30ee21002cb7a75809d1a25d67ccb6c11298402c27cb23a5dbc3a640ec27
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 8, 2026.
Transparency log