How to track who has read your email?
Hi friends here I am with another quick trick of web development. Recently I was working on a News letter module for one of our client and there was a special requirement, client want records of his News letter per user basic. He wants statics which user has read news letter on which date so they can plan their marketing strategy according to user specific.
My first reaction was what the hell… how can we… Is it possible?…
Then I searched on web and read few articles, after that I got it figured and then laughed at myself because it is so simple to achieve the same.
So let me explain the idea first, every web developer knows if he want to record anything from other website he need a http request to his website. Problem is how we can initiate request when user clicks on his email; we don’t have control on email clients. But there is a trick through which we can achieve this and that is “IMAGES”. When an email client opens any email he try to fetch all the images on that mail from the url of that image, so what we can do is we send a fake image request under tag and when email client try to fetch that url we process our recoding script so simple.
Here is the code sample for this just try to implement and experiment:
For Sending Mail
$to = “email@example.com, firstname.lastname@example.org”;
$subject = “HTML email”;
$message = ‘
<p>This email contains HTML Tags!</p>
<td><img src=”https://www.example.com/image.php” alt=”Email track Request” /></td>
// Always set content-type when sending HTML email
$headers = “MIME-Version: 1.0” . “\r\n”;
$headers .= “Content-type:text/html;charset=UTF-8” . “\r\n”;
// More headers
$headers .= ‘From: <email@example.com>’ . “\r\n”;
For Tracking Mail
$actual_time = time();
$actual_day = date(‘Y.m.d’, $actual_time);
$actual_day_chart = date(‘d/m/y’, $actual_time);
$actual_hour = date(‘H:i:s’, $actual_time);
$browser = $_SERVER[‘HTTP_USER_AGENT’];
$myFile = “log.txt”;
$fh = fopen($myFile, ‘a+’);
$stringData = $actual_day . ‘ ‘ . $actual_hour . ‘ ‘ . $ip . ‘ ‘ . $browser . ‘ ‘ . “\r\n”;
//Generate Image (Es. dimesion is 1×1)
$newimage = ImageCreate(1,1);
$grigio = ImageColorAllocate($newimage,255,255,255);
Or you can download the source code here. Happy Coding.