Welcome to a tutorial on how to send an attachment with PHP mail. Looking to attach a file with the default PHP mail function? It is possible, but there is just one problem…
To send an attachment with the default PHP mail function, we have to manually define and format a multi-part email.
Yes, and that is a rather confusing not-so-straightforward process – Read on for an example!
ⓘ 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 Attachment||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 ATTACHMENT
All right, let us now get into the example of how to send attachments with PHP mail.
MULTI-PART EMAIL WITH ATTACHMENT
<?php // (A) MAIL SETTINGS $mailTo = "firstname.lastname@example.org"; $mailSubject = "Test Attachment"; $mailMessage = "Test Message"; $mailAttach = "capybara.jpg"; // (B) MAIL HEADERS $boundary = md5(time()); // Random boundary $mailHead = implode("\r\n", [ "MIME-Version: 1.0", 'Content-Type: multipart/mixed; boundary="'.$boundary.'"' ]); // (C) MAIL MESSAGE $mailBody = implode("\r\n", [ // (C1) THE MESSAGE "--$boundary", "Content-type: text/html; charset=utf-8", "", $mailMessage, // (C2) ATTACHMENT "--$boundary", 'Content-Type: application/octet-stream; name="'.basename($mailAttach).'"', "Content-Transfer-Encoding: base64", "Content-Disposition: attachment", "", chunk_split(base64_encode(file_get_contents($mailAttach))), "--$boundary--" ]); // (D) SEND echo mail($mailTo, $mailSubject, $mailBody, $mailHead) ? "OK" : "ERROR" ;
For you guys who just want to use this as a “plugin”, go ahead – Just plug in your own mail settings in
(A) and end-of-story.
For you guys who want more technical information – We are only formatting the email according to RFC1341 multipart.
- In the mail header, we have to define
Content-Type: multipart/mixed; boundary="BOUNDARY-STRING".
- Then for each section of the email:
- We start with
- Followed by whatever
- Then an empty line break, and the content itself.
- We start with
- Lastly, close the email with
That’s all – If you want to add more attachments, it is only a manner of adding more
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 – PHP
- Send Email with Attachment in PHP – Codex World
- PHP: Sending Email (Text/HTML/Attachments) – WebCheatSheet
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!