How to Track Email Opens Sent From Django App

In this article we will see how to track the Email opens for the emails sent from your Django App.

This will help us in accumulating the information such as:
 - who opened the email
- when was email opened
- what is the open rate, i.e. how many user opened the email
- what is the time when most of the users check their emails.

Sending Email:

We have covered this part in detail in below articles.

You may use any of these methods to send an email.
- Sending bulk emails using mailgun api
- Sending email using office 365
- Sending email using Gmail

Create the HTML body for email:

# using template to generate the email content
template = get_template("testapp/email.html")
context_data = dict()

# pass the variable image_url to template
# image_load is the URL name. see below
context_data["image_url"] = equest.build_absolute_uri(reverse("image_load"))
html_text = template.render(context_data)
plain_text = strip_tags(html_text)

In the email template email.html  , at the end of the content, insert the below code.

<img src="{{image_url}}" height="0px" width="0px"/>

This is an image of 0 by 0 pixels, which will not be visible in email but will hit the URL image_load  on our server when email is loaded.

Image src URL :

This is the URL which will be used as source of the image in email and will receive the hit whenever email will be opened.

Create a url in  file of your app.

url(r'^image_load/$', views.image_load, name='image_load'),

Create a view image_load  in  file.

from django.http import HttpResponse
from PIL import Image

def image_load(request):
    print("\nImage Loaded\n")
    red ='RGB', (1, 1))
    response = HttpResponse(content_type="image/png"), "PNG")
    return response

This view is creating and returning an image as response to url image_load.


Now send the email and open it. As soon as you open the email, you will receive a hit on the image_load  url.

This will NOT work on localhost. So I created a test app for free on pythonanywhere server (it took me just 5 minutes) and used the URL from there.

Screenshot from

When I opened the test email, I could see the output printed in server logs.

Screenshot from  

When sending emails to multiple users, append the encrypted userId or emailId to the image source URL so that you can track who opened the email.

Try this and let us know your views.      

Related Articles:
Python Script 3: Validate, format and Beautify JSON string Using Python
Validating json using python code, format and beautify json file using python, validate json file using python, how to validate, format and beautify json...
Encryption-Decryption in Python Django
How to encrypt and decrypt the content in DJango. Encrypting the critical information in Django App. Encrypting username, email and password in Django...
Adding Robots.txt file to Django Application
Adding robots.txt file in your Django application. Easiest way to add robots.txt file. Django application robots.txt file. Why should you add robots.txt file in your Django Application....
Scraping 10000 tweets in 60 seconds using celery, RabbitMQ and Docker cluster with rotating proxy
Scrapping large amount of tweets within minutes using celery, RabbitMQ and docker cluster. Scraping huge data quickly using docker cluster with TOR....

0 thoughts on 'How To Track Email Opens Sent From Django App'
Leave a comment:

*All Fields are mandatory. **Email Id will not be published publicly.

Please subscribe to get the latest articles in your mailbox.

Recent Posts:

© 2018-2019
Contact Us: code108labs [at]
Address: 3747 Smithfield Avenue, Houston, Texas