2

I have a PHP mail script which is basically the following:

$result = mail($to, $subject, $message, $headers);
if(!$result) {   
  echo "Error";   
} else {
  echo "Success";
}

The $message is a HTML email that mostly renders fine in my email client except the images seem to only load sporadically.

The images are all like so:

<img src='http://www.mywebsite.com/media/twitter.png' />

I don't understand why some would load and some wouldn't, when they are all set up the same way.

I've read that it's better to embed images into the email as attachments but I'm unsure how to do this. It seems that you add a line like so:

<img src='cid:123456789'>

But what does this reference? How would I encode an image like this?

Any help would be appreciated!! Thanks

Lee
  • 1,485
  • 2
  • 24
  • 44

1 Answers1

1

You would have to base64 encode the file.

I found a code example on github. I have not tested it myself but should give you a good nudge in the right direction...

   $picture = file_get_contents($file);
   $size = getimagesize($file);

   // base64 encode the binary data, then break it into chunks according to RFC 2045 semantics
   $base64 = chunk_split(base64_encode($picture));
   echo '<img src="data:' . $size['mime'] . ';base64,' . "\n" . $base64 . '" ' . $size[3] . ' />', "\n";

Source : https://gist.github.com/jasny/3938108

Just as a side note. Are the images that you are using web optimised? Large images might be blocked by email clients, or just not downloaded by email clients.

  • 1
    Thanks! After finding out about base64 I found this excellent post - http://stackoverflow.com/a/1607263/4120885. I fed my `$message` through his function and it embedded the images – Lee May 13 '15 at 10:36