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:
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.
$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:
Finally here is the change in database:
Best of luck!
Exactly what I was looking for... Thanks!
ReplyDeleteHi 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).
ReplyDeleteThat 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.
Deletesense !!
ReplyDeleteIt only works when the user allows for the images to be loaded, otherwise the image is blocked and the view is not detected.
ReplyDeleteI agree.
DeleteWont this result in your email being flagged as spam?
ReplyDeleteNo
Deleteits not better idea
ReplyDeletePlease share if you have a better solution. There are many request for an alternate solution.
DeleteVery useful as usual. Keep it up Hasan.
ReplyDeleteThanks for a big bunch!!
ReplyDeleteYou can also use inline property style="display: none" for that img tag then also it won't show any broken image.
ReplyDelete