This way you can use the same script for multiple emails and have them each generate unique return-receipts, so you'll know which particular emails were already opened and which were not. This is particularly useful if you have PHP code that generates mass-emails; simply replace the query string with a variable, so that each recipient will generate a unique return-receipt. If you don't know how to pick up query data in PHP, don't worry. Examples are coming.
You now have a PHP script that will be executed when the recipient opens your email, so you can handle this any way you like. Have the script add an entry to a database, or append an entry to a log file, or anything else you can dream up. The example I'll give is the one which I find most useful, which is instant email notification (that is, an automatic email to you, the sender, notifying you that your email has been opened by the recipient). You can even include environment variables in the email, to get things like the recipient's IP address and browser version. I won't get into how that could be useful, but use your imagination.
Put the following snippet into your PHP page. It will auto-generate an email notification and dispatch it to you. In this example I've included the query data from the example above:
$from_email = 'firstname.lastname@example.org';
$to = 'email@example.com';
$subject = "Email opened by ".$_GET['you'];
$body = $_SERVER['REMOTE_ADDR'].' '.$_SERVER['HTTP_USER_AGENT'];
$headers = "From: ".$from_email."\r\nX-Mailer: php";
mail($to, $subject, $body, $headers);
You can edit the subject and body however you like. The $to variable will be the email address where the return-receipt gets sent, so in most cases this should be set to your own email address. The $from_email variable is the address that the notification will appear to come from, and you can set this to whatever you want (even a non-existing email address, in most cases). I've included the IP address and user-agent variables in the body, which you can leave in or remove as you see fit.
The rest is up to you. Here are a few things to keep in mind:
- All PHP code within the referenced page will be executed when the recipient opens your email. The sleep() function is interesting here in that it causes a delay in the recipient's email client. I myself don't see any use for it, but I do find it interesting.
- Code that would normally cause HTML output for viewing by a client browser, such as "echo" statements, will not actually output anything.
- Depending on the recipient's email client, the image reference may show up as a "broken image" icon, which the recipient may see as suspicious. To prevent this, you can have your PHP script output an actual image after it's done executing your code. Simply add this line to the end of your script:
Replace "image.gif" with an actual image path/filename on your server. All the code that comes before that line will be executed, and then the image will be loaded by the recipient's email client. It will look to the recipient just as it would if the IMG reference pointed to an image file in the first place. I recommend always including this statement, even if it only points to a tiny white (or transparent) placeholder image.
Sending email with URL-based images in Mozilla Thunderbird
Sending the actual email is fairly simple, however sending the email manually in Mozilla Thunderbird presents a unique problem to this return-receipt gizmo.
Thunderbird automatically detects URL-based IMG tags in your outgoing messages and replaces them. It downloads the referenced image, attaches it to the email, and converts your IMG source to reference the attached file instead of the remote URL. Since the point of the IMG tag is to execute PHP code on your web server, this would completely break the return-receipt functionality. The good news is that this behavior can be prevented, although you have to do it for every image in every email individually as there is no global setting (at least none that I've been able to find).
Open Thunderbird and start a new email. Click in the body of the message and click the Insert menu, then choose Image. Type in your URL, then click "Advanced Edit." In the Attribute field, type "moz-do-not-send" (without quotes), and for Value type "true" (again without quotes). Click OK a couple of times. That's it. You just told Thunderbird not to convert your image references to attachments. Your IMG tag will now call your remote PHP script when the recipient opens your email.
That's about it. I hope you find this as useful as I have.