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:
- Host the image on your server, then set PHP to send out an HTML mail –
- Or use base64 encode to directly embed the image into the email –
Let us walk through the examples of each method – Read on!
TLDR – QUICK SLIDES
Fullscreen Mode – Click Here
TABLE OF CONTENTS
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
<?php // (A) MAIL SETTINGS $mailTo = "firstname.lastname@example.org"; $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/mail-girl.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 basically 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
<?php // (A) MAIL SETTINGS $mailTo = "email@example.com"; $mailSubject = "Test Mail With Embedded Image"; // (B) MAIL MESSAGE // DIRECTLY EMBED IMAGE INTO EMAIL WITH BASE64 ENCODE $img = file_get_contents("mail-girl.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.
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
600+ free tutorials & projects on Code Boxx and still growing. I insist on not turning Code Boxx into a "paid scripts and courses" business, so every little bit of support helps.
Buy Me A Meal Code Boxx eBooks
EXAMPLE CODE DOWNLOAD
Click here for the 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.
EXTRA BITS & LINKS
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
- Mail Function – PHP
- Send the image as an attachment – Code Boxx
- Base64 Encoding Image – Stack Overflow
INFOGRAPHIC CHEAT SHEET
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 “How To Embed Images In PHP Mail (Simple Examples)”
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
Yes, the universal solution is called “host your images on a server“. Good luck!