4

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'>";
?>
Bing
  • 3,071
  • 6
  • 42
  • 81

2 Answers2

6

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.

-1

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.

Raekye
  • 5,081
  • 8
  • 49
  • 74
  • I tried Google... what phrase are you using? Also, where does this go? – Bing Dec 22 '12 at 05:22
  • 1
    "Php output image". header goes before you send any data to the user. readfile basically outputs the file to the browser (again with, `` that source link should be to an image file. readfile "prints" the file contents to the browser). It doesn't really matter where readfile goes, though technically you should send data first so the client can load it (like how script tags preferably go after the body if possible) – Raekye Dec 22 '12 at 05:23