Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
email tracking free-emails   2   20541
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 urls.py  file of your app.

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


Create a view image_load  in views.py  file.

from django.http import HttpResponse
from PIL import Image

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


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


Testing:

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.      

email tracking free-emails   2   20541
2 comments on 'How To Track Email Opens Sent From Django App'
Login to comment

Vishnu Prasad B March 26, 2019, 4:59 a.m.
How to tack with the same technique, but sending the same email with "cc" and "bcc".In that case how to track "cc" and "bcc".
Rahul Raj July 25, 2020, 8:19 p.m.
Hello Can you share with me your all code because its bit consuion here , if possible github link or `gist`

Related Articles:
Accessing Gmail Inbox using Python imaplib module
In this article, we are accessing Gmail inbox using IMAP library of python, We covered how to generate the password of an App to access the gmail inbox, how to read inbox and different category emails, how to read promotional email, forum email and updates, How to search in email in Spam folder, how to search an email by subject line, how to get header values of an email, how to check DKIM, SPF, and DMARC headers of an email...
Sending email with attachments using Python built-in email module
Sending email with attachments using Python built-in email module, adding image as attachment in email while sending using Python, Automating email sending process using Python, Automating email attachment using python...
Adding Email Subscription Feature in Django Application
email subscription feature in django, sending email subscription confirmation mail in django, sending email unsubscribe email in Django, Add subscription feature in Django...
Sending Emails Using Python and Gmail
Python code to send free emails using Gmail credentials, Sending automated emails using python and Gmail, Using Google SMTP server to send emails using python. Python script to automate gmail sending emails, automating email sending using gmail...
DigitalOcean Referral Badge

© 2024-2025 Python Circle   Contact   Sponsor   Archive   Sitemap