问题
I'm playing with the idea of adding email tracking to a web service I built for a small client business. I was planning on doing the embedded image solution (with reference to an image on my server) - unless someone else has a better method - but when I use the image tag referencing a PHP page on my server it loads the "broken image" icon. How can I make this a valid image?
Here is the code for the mailing PHP page:
<?php
$body = "<html>Hello there!".
"<img src='http://mysite.com/track.php?name=bob' />".
"</html>";
$subject = "Tracking on ".date('Y-m-d 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'>";
?>
回答1:
If you're going to use a PHP script like that, it needs to return image data, not just a HTML image tag. Easiest way to do that will be something like:
<?php
header("Content-Type: image/jpeg");
readfile("image.jpeg");
do_all_your_tracking_stuff();
Note that this is returning the image data first, so that a mail client can start displaying it immediately, rather than waiting for your SQL queries to complete.
回答2:
Simple google search would have worked...
header('Content-Type: image/jpeg');
readfile('path');
You really just need to use your intuition: what do you link to in an image tag? You link to the image source. The actual image file. What are you doing in the code? Echoing HTML code with another image source. But the client browser is expecting an image, not more HTML code.
来源:https://stackoverflow.com/questions/14000277/tracking-email-opens-with-a-real-image