Sunday, March 27, 2011

Check if email has been read

Hi,

If you are working with newsletters, it is a part of the job to maintain a statistics, of the emails to which the newsletters are send. The statistics may contain, number of bounced mails, number of opened/unopened email and so on. In this post I will explain a method for checking whether an email has been read or not. For checking bounced emails, I hope I will add it as a seperate post.

The idea is very simple. When we are talking of whether an email has been read or not, we basically mean to say  whether the email has been opened or not. For achieving this, following is the technique I have been using in most of my projects.

Assuming we have the following table structure (this is the minimum columns needed, you can change according to your need):



First add a image tag in your email along with the other email contents, and do not mention any size of the image as:

<img src="....." />

The main trick is with the "src" attribute. In the src, instead of using an actual image, enter the path of a PHP file in your server with the full URL of the files as:

<img src="http://localhost/php/ismailopened/opened.php?id=1">

In "?id=1", the value 1 needs to be dynamic. It is the newsletter ID. Now in opened.php I have added the following code:


<?php
if(isset($_GET['id'])){

mysql_pconnect("localhost","root");
mysql_select_db("test");
mysql_query("update newsletter set isopened='1' where id=".$_GET['id']);


$im=imagecreatetruecolor(1,1);
$white=imagecolorallocate($im,255,255,255);
imagefilledrectangle($im,0,0,1,1,$white);
header("content-type: image/jpeg");
imagejpeg($im);

}

Explanation:

In the above code, I am updating the database and changing the email opened status as 1, for the newsletter whose ID is passed in the GET parameter in the image src earlier, "<img src="http://localhost/php/ismailopened/opened.php?id=1">". But this does not finish our job.


Now we have to make sure that the <img src="..."> is not showing a broken image e.g.  . For avoiding that I am creating a small white 1x1 image using GD library by the following code:


$im=imagecreatetruecolor(1,1);       // initialising the image object and asigning its simensions
$white=imagecolorallocate($im,255,255,255); // creating the color object for white color
imagefilledrectangle($im,0,0,1,1,$white); // creating a white rectangle to the image object
header("content-type: image/jpeg"); // creating an image header for returning an image to the src
imagejpeg($im); // image is being created

Finally here is the change in database:


Best of luck!

13 comments:

  1. Exactly what I was looking for... Thanks!

    ReplyDelete
  2. Hi Thanks for nice script, but its working only when user add you in to "Safe sender list" in e-mal program (like windows mail or outlook).

    ReplyDelete
    Replies
    1. That is true. But this is a solution being used for years now. Other solution will be to use API's of third party mailing vendors such as the MailChimp.

      Delete
  3. It only works when the user allows for the images to be loaded, otherwise the image is blocked and the view is not detected.

    ReplyDelete
  4. Wont this result in your email being flagged as spam?

    ReplyDelete
  5. Replies
    1. Please share if you have a better solution. There are many request for an alternate solution.

      Delete
  6. Very useful as usual. Keep it up Hasan.

    ReplyDelete
  7. Thanks for a big bunch!!

    ReplyDelete
  8. You can also use inline property style="display: none" for that img tag then also it won't show any broken image.

    ReplyDelete