Do you want to know when someone has read an email you’ve sent? It’s not as difficult as you may think. In this article I’ll explain how you can track email opens with PHP.
Writing Your Email
To track when the recipient has opened your email you will need to include a snippet of HTML for loading an image.
[html][/html]
You will notice that the code above is pointing at a PHP script, and not an image file. Inside this PHP script is where the magic happens.
Breaking It Down
So emailtracker.php
is our PHP script. While ?to=recipient@example.com
lets us know who we sent the email to by submitting the email address via GET Method.
I like to put the code in my signature, so that it seems it’s a regular image and not a tracking image.
The PHP Script
Open your favorite text editor and create a PHP script called emailtracker.php including the following;
[php][/php]
Breaking It Down
I have commented the code above thoroughly, but I suspect the $getTo
variable might confuse people.
[php]$getTo = (filter_var($_GET[‘to’], FILTER_VALIDATE_EMAIL)) ? $_GET[‘to’] : null;[/php]
I am using shorthand PHP to make sure the email address is valid using PHP’s filter_var function and then setting the $getTo
variable to the email address if it’s valid or null if it’s invalid.
If you were tracking email opens in a web application and using a database you would set your database configuration at the top and change the following to your database connection and processing code;
[php]mail($email, $subject, $message);[/php]
Did you successful track email opens with PHP? If you have any questions, drop them in the comments below 🙂
Header image based on “Code” by Michael Himbeault, CC-BY-2.0.
Excellent yet another article,
Short & sweet, fine as particle!
With respect upmost,
You deserve a toast!!
<3 lol