Email Tracking Starts with a Real Image

I play with the idea of ​​adding email tracking to the web service that I created for a small client business. I planned to make an embedded image solution (with a link to an image on my server) - if someone else does not have a better method, but when I use an image tag that links to a PHP page on my server, it uploads a "broken" image. How to make it valid?

Here is the code for the PHP post page:

<?php $body = "<html>Hello there!". "<img src='http://mysite.com/track.php?name=bob' />". "</html>"; $subject = "Tracking on ".date('Ymd H:i:s'); $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: webmaster@mysite.com ' . "\r\n"; mail(' my_email@gmail.com ',$subject,$body,$headers); ?> 

And here is the tracking code:

 <?php include('database_connection.php'); $query = "INSERT INTO tracking SET name='".$_GET['name']."', date=NOW()"; mysql_query($query); // Tried this, but it doesn't work: echo "<img src='http://mysite.com/photos/image.jpg'>"; ?> 
+4
source share
2 answers

If you intend to use a PHP script, then it needs to return image data, not just an HTML image tag. The easiest way to do this would be something like this:

 <?php header("Content-Type: image/jpeg"); readfile("image.jpeg"); do_all_your_tracking_stuff(); 

Note that this first returns the image data so that the email client can immediately display it, rather than waiting for your SQL queries to complete.

+5
source

A simple google search would work ...

 header('Content-Type: image/jpeg'); readfile('path'); 

You just need to use your intuition: what are you referring to in the image tag? You are linking to an image source. Actual image file. What are you doing in the code? Repeating HTML code with a different image source. But the client browser expects an image, not more HTML code.

-1
source

All Articles