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!
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
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.
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/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
<?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("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.
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
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!