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!
ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
REAL QUICK SLIDES
TABLE OF CONTENTS
|Download & Notes||Mail Image||Useful Bits & Links|
DOWNLOAD & NOTES
Firstly, here is the download link to the example code as promised.
EXAMPLE CODE DOWNLOAD
Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.
PHP MAIL WITH IMAGE
All right, let us now get into the examples of embedding images with PHP mail.
1) HOST IMAGE ON SERVER
<?php // (A) MAIL SETTINGS $mailTo = "email@example.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/penguin.jpg"; $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.
2) EMBED IMAGE IN EMAIL
<?php // (A) MAIL SETTINGS $mailTo = "firstname.lastname@example.org"; $mailSubject = "Test Mail With Image"; // (B) MAIL MESSAGE // DIRECTLY EMBED IMAGE INTO EMAIL WITH BASE64 ENCODE $img = file_get_contents('penguin.jpg'); $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 may not be capable of displaying such embedded images. The traditional “host image on a server” is the safer option.
USEFUL 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.
INFOGRAPHIC CHEAT SHEET
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!