It’s a little rude to ask someone to confirm they read an email, isn’t it? Well, there’s a way to get a confirmation without them knowing it using PHP and htaccess.
First, create a directory in your images folder called ‘email’ (/images/email/ for this tutorial). Inside that folder, create an .htaccess file. Be sure that your Apache directive for AllowOverride is set to All.
In this .htaccess file, add the following:
View Code HTACCESS
RewriteEngine On ReWriteRule ^image(.*?)\-\d{4}\.png$ track.php?x=$1 [L]
Next, create a 1x1px PNG image in the same folder and name it track.png. This will be the image loaded in the email.
Finally, in the same folder create a file named track.php and add the following:
View Code PHP
<?php $trackingNumber = $_REQUEST['x']; // logic here for updating your database tables header('Content-Type:image/png'); echo file_get_contents('track.png'); ?>
Inside the email you are sending, include the following line. This will allow the system to track the ID ’1234′ when that image is loaded. Additionally, as we are using htaccess to mask the script being used, the image will appear to be a regular PNG without any extra parameters.
View Code HTML
<img src="http://www.yoursite.com/images/email/image1234.png" />
In part 2 of this tutorial, we’ll discuss where the ID is generated and how it is used.
Hi Cody,
Nice article. Just what I needed.
Can you tell me what’s the purpose of using the .htaccess file ?
I would simplify the whole thing by using this kind of img tag :
When looking at different emailng that I receive, both techniques are used, with or without URL rewriting. I’m wondering if rewriting is necessary.
Thanks !
Louis
Thanks for the feedback, Louis! Unfortunately, the code snippet you pasted didn’t come through. I’ll assume we’re talking about an image source attribute like this:
The htaccess changes are there as more of a “just in case” method for a situation where the email client or server-side filter is taking URL arguments off an image or flagging an email as spam when it has these (it may even attribute a very small spam “score” to a message with this type of image in it that, by itself, won’t get the message filtered). It would take a painfully thorough filter to detect tracking through the htaccess usage.
If you’re using a shared host that won’t allow htaccess rewrites, using the arguments in the URL should work just fine with most (if not all) email clients. This gives me an idea for a supplemental blog using both methods and testing the results across a variety of email services.
Terrific work! This is the type of information that should be shared around the web. Shame on the search engines for not positioning this post higher!
Thanks, man. Hopefully it’ll gain momentum in the search engines before the final steps to the complete system are published. Cheers to the free exchange of information.
How much more impolite is it to track message use silently ?
Techniques like this are easily disabled by standard email client features.
If it’s rude to ask someone if they read the email that you sent, maybe the conversation should be happening face to face, unless this is really an address harvesting technique. It sure looks like it.
interesting, thanks
Been using this method for years on one of my sites but there are problems.
One is Outlook and now webmail systems will block images and other content being loaded from email, warning the user about information leakage. Outlook 2003 onwards can do this and mailers such as Squirrel too.
Instead of the URL appending for the image loading you could use .htaccess to convert an image load request for say : http://www.domain.com/images/10920391.jpg to break it down to:
track.php?mailcode=10920391
The PHP script would do the db work and then display either a space or an actual image that is used in the layout. By not being a 1x1px spacer and having a genuine looking file path it should thwart more defences.
Just my 2p
An address harvesting technique? I don’t see anything in the code remotely relevant to this. As it stands, this code serves a single purpose: To track the loading of an image in an email. Should it be used in a larger application for any other purpose, that purpose is not a function of what is posted here.
Many email clients do have ways of blocking systems like this (not loading images at all is common). However, using this technique will provide the best opportunity at seeing the view data, if nothing more than a fraction of it.
Finally, asking a user or tracking the view of an email silently are both rude. This method is not perceived as rude due to the user being unaware and that’s what is important.
Most email apps will tell the user that the email is trying to load remote content. The user will be aware.
If delivery must be acknowledged, email is probably not the communication channel to use.
Still, it’s good to educate people about how spammers can see that your email address is valid, and how setting your email client to block remote content can help you avoid spam.
Hi,
I have the following for .htaccess (1and1 hosting)
Options FollowSymLinks
AllowOverride All
RewriteEngine On
ReWriteRule ^image(.*?)\-\d{4}\.png$ track.php?x=$1 [L]
And I get a 500 Error
Any ideas if I need to add something extra.
Thanks.