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 email

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:

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

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.

Create a view image_load  in views.py  file.

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.

image hit url - email tracking

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

image hit url - email tracking

 

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.

 

 

Sending Emails Using Python and Gmail

We already know how to send email using office 365 or how to send bulk emails using mailgun api.

Here we will see how to send email from your python script or Django App using Gmail account.

Gmail have some daily limits on the number of emails you can send in a day. If you need to send bulk emails, use mailgun api.

Code:

Import dependencies:

Create message:

Create message body and attach to message:

Send the message:

 

Complete code with comments:
 

Available on Gitlab as well. Please comment your views.

Host your Django Application for free on PythonAnyWhere Servers.

 

How to send bulk emails for free using Mailgun and Python Django

Consider these scenario in your newly developed Django Application:

  • You need to send promotional emails to all the registered users.
  • You need to send email confirmation link or password reset link.
  • You need to send weekly newsletter to you readers.

What do you choose? Free Gmail Account to send email using python code? That is a nice idea provided you want to look unprofessional and you need not to send more than 500 emails per day.

But if you want professional look in your emails [from address something like admin@yourwebsite.com  instead of something@gmail.com  and do not want the limit on per day email count, then this article is for you.

Continue reading “How to send bulk emails for free using Mailgun and Python Django”