How to track who has read your email?

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

<?php

$to = “someemail@gmail.com, another@gmail.com”;

$subject = “HTML email”;

$message = ‘

<html>

<head>

<title>HTML email</title>

</head>

<body>

<p>This email contains HTML Tags!</p>

<table>

<tr>

<th>Firstname</th>

<th>Lastname</th>

</tr>

<tr>

<td><img src=”http://www.example.com/image.php” alt=”Email track Request” /></td>

<td>Doe</td>

</tr>

</table>

</body>

</html>’;

// 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: <youremail@gmail.com>’ . “\r\n”;

mail($to,$subject,$message,$headers);

?>

 

For Tracking Mail

<?php

error_reporting(0);

Header(“Content-Type: image/jpeg”);

//Get IP
if (!empty($_SERVER[‘HTTP_CLIENT_IP’]))
{

$ip=$_SERVER[‘HTTP_CLIENT_IP’];

}

elseif (!empty($_SERVER[‘HTTP_X_FORWARDED_FOR’]))
{

$ip=$_SERVER[‘HTTP_X_FORWARDED_FOR’];

}

else
{

$ip=$_SERVER[‘REMOTE_ADDR’];

}

//Time
$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);

//GET Browser

$browser = $_SERVER[‘HTTP_USER_AGENT’];

//LOG

$myFile = “log.txt”;

$fh = fopen($myFile, ‘a+’);

$stringData = $actual_day . ‘ ‘ . $actual_hour . ‘ ‘ . $ip . ‘ ‘ . $browser . ‘ ‘ . “\r\n”;

fwrite($fh, $stringData);

fclose($fh);

//Generate Image (Es. dimesion is 1×1)

$newimage = ImageCreate(1,1);

$grigio = ImageColorAllocate($newimage,255,255,255);

ImageJPEG($newimage);

ImageDestroy($newimage);

?>

Or you can download the source code here. Happy Coding.

Latest Blogs