Embed Images In PHP Mail (Simple Examples)

Welcome to a tutorial on how to embed images in PHP mail. Trying to add images to the email message with the default PHP mail function? Yes, it is possible with a couple of small touches.

There are 2 common ways to add images in PHP mail:

  1. Host the image on your server, then set PHP to send out an HTML mail – <img src="HTTP://SITE.COM/IMAGE.JPG">
  2. Or use base64 encode to directly embed the image into the email – <img src="data:image/x-icon;base64,ENCODED-IMAGE-DATA-HERE">

Let us walk through the examples of each method – Read on!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

PHP EMAIL WITH IMAGE

All right, let us now get into the examples of embedding images with PHP mail.

 

METHOD 1) HOST IMAGE ON SERVER

1-mail-image.php
<?php
// (A) MAIL SETTINGS
$mailTo = "jon@doe.com";
$mailSubject = "Test Mail With Image";

// (B) MAIL MESSAGE
// HOST THE IMAGE ON YOUR OWN SERVER!
// ALSO REMEMBER TO PROVIDE THE DIRECT LINK JUST-IN-CASE
$img = "http://localhost/bottle.png";
$mailBody = "<img src='$img'><br>";
$mailBody .= "<a href='$img'>Can't see the image? Click Here.</a>";

// (C) HEADER - HTML MAIL
$mailHead = implode("\r\n", [
  "MIME-Version: 1.0",
  "Content-type: text/html; charset=utf-8"
]);

// (D) SEND
echo mail($mailTo, $mailSubject, $mailBody, $mailHead)
  ? "OK" : "ERROR" ;

This should be pretty self-explanatory – We are just specifying an HTML mail Content-type: text/html, then using the “usual” <img> tag to point to an image hosted on a server.

 

 

METHOD 2) EMBED IMAGE IN EMAIL

2-image-embed.php
<?php
// (A) MAIL SETTINGS
$mailTo = "jon@doe.com";
$mailSubject = "Test Mail With Embedded Image";

// (B) MAIL MESSAGE
// DIRECTLY EMBED IMAGE INTO EMAIL WITH BASE64 ENCODE
$img = file_get_contents("bottle.png");
$imgdata = base64_encode($img);
$mailBody = "<img src='data:image/x-icon;base64,$imgdata'>";

// (C) HEADER - HTML MAIL
$mailHead = implode("\r\n", [
  "MIME-Version: 1.0",
  "Content-type: text/html; charset=utf-8"
]);

// (D) SEND
echo mail($mailTo, $mailSubject, $mailBody, $mailHead)
  ? "OK" : "ERROR" ;

As in the introduction, we directly use base64_encode() to encode and embed the image into the email itself. But please take note, some email clients or webmail are not capable of displaying such embedded images. Some mail hosts, firewalls, or security will outright strip off all embedded data. The traditional “host image on a server” is the safer option.

 

 

EXTRAS

That’s all for the main tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

2 thoughts on “Embed Images In PHP Mail (Simple Examples)”

  1. Hi –
    I need to send emails with *embedded* pictures & was excited to see your really simple & straightforward solution. Unfortunately it doesn’t work on the server I use which is hosted by UK2. I get the “OK” at the end of your code and the email arrives, but the picture is empty. I presume this is because of the base64_encode() problem you mention.
    Is there any chance of a more universal solution?
    Best wishes, Tony Reynolds

Comments are closed.