I have seen the service like spypig.com placing a small image in the email and tracking when it is opened and from where. They track city, country, IP address etc. How is this done?
- How do we know when the mail is opened? And how is the image
generated?
- How is the IP address detected and how is it possible to know location from
it?
Basically, in the HTML body of your email, there will be an <img>
tag that would look like this :
<img src="http://www.yoursite.com/tracker.php?id=123456" alt="" />
When someone reads his mail, with images enabled, the email-client will send a request to tracker.php
, to load the image, passing it id=123456
as a parameter.
This tracker.php
script will be on your server, and, when called, it will :
- Check the
id
parameter,
- Use it to find to which email address it corresponds -- when generating the email for each one of your subscribers, you'll have generated an
id
different for each e-mail.
- Do some stuff -- like log "email 123456 has been opened", and some additional informations
- return the content of a small image ; like a 1x1 transparent gif.
The tracker.php
script knows from which IP address it's been called -- like any other PHP script :
$ipAddress = $_SERVER['REMOTE_ADDR'];
And, starting from this IP address, you can use a geolocation service to find out from where in the world the email has been opened.
As a couple of examples, you could take a look at MaxMind, or IPInfoDB
As you know that id=123456
corresponds to one specific email address, this allows to find out where each one of your subscribers are.
1. Place the tracker iamge at the E-mail
<img src="http://www.yoursite.com/tracker.php?eid=123456&uid=123" alt="" width="1px" height="1px">
Its working is very simple., Once your mail open, the tracker image sends the request to tracker.php for an image for download, We get data of user data from URL and consider as the mail is read.
Note: Don't use display: none; property for hiding your images, it may filter by spam algorithm. And don't place any javascript codes, it also blocks the spam filter
2. On the tracker.php
<?php
header("Content-Type: image/jpeg"); // it will return image
readfile("img.jpg");
dbfunction(); // place your db code
?>
3. The ip address is get by the following function.
function get_client_ip() {
$ipaddress = '';
if (isset($_SERVER['HTTP_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_X_FORWARDED']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_FORWARDED']))
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if(isset($_SERVER['REMOTE_ADDR']))
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
$PublicIP = get_client_ip();
4. Location:
The location is getting by any geolocation services, you can use open-source GeoLocation finder like nekudo,freegeoip.
for examle
<?php
$json = file_get_contents("https://freegeoip.net/json/$PublicIP");
$json = json_decode($json ,true);
$country = $json['country_name'];
$region= $json['region_name'];
$city = $json['city'];
?>
About the first part of the question, what I did was return the image from a php file. Aside from returning an image (it can be 1x1 pixel transparent png) is log all the info in to the database. This way, when the php file is called, you know that the image was loaded i.e. the email was read. The problem is that alot of modern clients dont load images automaticlly. This is to not allow just the kind of thing youre trying to do, for privacy reasons.
About the second part, there are several geolocation web services, where you submit an IP and get the geolocation. You can do that in the php file that returns the 1x1 pixel image.
Here is a good thread about this on this site:
Geolocation web service recommendations
I was looking for a tip to hide the image, the easiest way seems to do :
<img src="http://www.yoursite.com/tracker.php?id=123456" alt="" width="1" height="1" border="0">
to complete the brilliant explanation of Pascal.