Thanks to my friend Tobie Kruger who, as usual, guided me towards the light on this one.
Here is a small and clean way to use PHP to check whether someone opened an email sent to them.
First of all, include a link to the (I call it) spike.php script in the body of your email as an image and pass the GET variables along in the URL which will be called when the email is opened. For example:
<img src=”http://path.to.your.spike.script.org.za/spike.php?emailaddress=someone@somewhere.org”>
Below is an example of the spike.php script:
<?php
Header("Content-type: image/jpg");
// Create a new blank 1 pixel image
$im = ImageCreate(1, 1);
// Set the color of the 1 pixel image to white
// Change this to match the background color of
// your email body to make it invisible
$bck = ImageColorAllocate($im, 255,255,255);
// Bucket fill the image with the color specified above
ImageFill($im, 0, 0, $bck);
// Create a jpeg image from the above settings
Imagejpeg($im);
$emailAddress = $_GET['emailaddress'];
$ipaddress = $_SERVER['REMOTE_ADDR'];
$result = mysql_query("UPDATE sentemails SET emailopened = 1, ip = 'ipaddress ' WHERE emailAddress = '$emailAddress '",$MyResource);
?>
Note: You should remember to sanitize the GET variables etc, all that code has been trimmed from the example above.