Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 14, 2023 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/64bd86d095bbe1646653ee97a1e3c80d to your computer and use it in GitHub Desktop.
Save code-boxx/64bd86d095bbe1646653ee97a1e3c80d to your computer and use it in GitHub Desktop.
PHP Email Attachment

SEND EMAIL ATTACHMENT IN PHP

https://code-boxx.com/send-attachment-php-mail/

IMAGES

attach-a attach-b

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?php
// (A) EMAIL SETTINGS
$mailTo = "jon@doe.com";
$mailSubject = "Test Attachment";
$mailMessage = "<strong>Test Message</strong>";
$mailAttach = "attach-a.png";
// (B) GENERATE RANDOM BOUNDARY TO SEPARATE MESSAGE & ATTACHMENTS
// https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
$mailBoundary = md5(time());
$mailHead = implode("\r\n", [
"MIME-Version: 1.0",
"Content-Type: multipart/mixed; boundary=\"$mailBoundary\""
]);
// (C) DEFINE THE EMAIL MESSAGE
$mailBody = implode("\r\n", [
"--$mailBoundary",
"Content-type: text/html; charset=utf-8",
"",
$mailMessage
]);
// (D) MANUALLY ENCODE & ATTACH THE FILE
$mailBody .= implode("\r\n", [
"",
"--$mailBoundary",
"Content-Type: application/octet-stream; name=\"". basename($mailAttach) . "\"",
"Content-Transfer-Encoding: base64",
"Content-Disposition: attachment",
"",
chunk_split(base64_encode(file_get_contents($mailAttach))),
"--$mailBoundary--"
]);
// (E) SEND
echo mail($mailTo, $mailSubject, $mailBody, $mailHead)
? "OK" : "ERROR" ;
<?php
// (A) EMAIL SETTINGS
$mailTo = "jon@doe.com";
$mailSubject = "Test Attachment";
$mailMessage = "<strong>Test Message</strong>";
$mailAttach = ["attach-a.png", "attach-b.png"];
// (B) GENERATE RANDOM BOUNDARY TO SEPARATE MESSAGE & ATTACHMENTS
// https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
$mailBoundary = md5(time());
$mailHead = implode("\r\n", [
"MIME-Version: 1.0",
"Content-Type: multipart/mixed; boundary=\"$mailBoundary\""
]);
// (C) DEFINE THE EMAIL MESSAGE
$mailBody = implode("\r\n", [
"--$mailBoundary",
"Content-type: text/html; charset=utf-8",
"",
$mailMessage
]);
// (D) MANUALLY ENCODE & ATTACH THE FILES
foreach ($mailAttach as $attach) {
$mailBody .= implode("\r\n", [
"",
"--$mailBoundary",
"Content-Type: application/octet-stream; name=\"". basename($attach) . "\"",
"Content-Transfer-Encoding: base64",
"Content-Disposition: attachment",
"",
chunk_split(base64_encode(file_get_contents($attach))),
"--$mailBoundary"
]);
}
$mailBody .= "--";
// (E) SEND
echo mail($mailTo, $mailSubject, $mailBody, $mailHead)
? "OK" : "ERROR" ;
<?php
// (A) LOAD PHPMAILER
// https://github.com/PHPMailer/PHPMailer
// composer require phpmailer/phpmailer
require "vendor/autoload.php";
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
// (B) SET EMAIL
$mail->From = "you@site.com";
$mail->addAddress("jon@doe.com");
$mail->isHTML(true);
$mail->Subject = "Test Attachment";
$mail->Body = "<strong>Test Message</strong>";
$mail->addAttachment("attach-a.png");
$mail->addAttachment("attach-b.ppg");
// (C) SEND MAIL
try {
$mail->send();
echo "OK";
} catch (Exception $ex) { echo $mail->ErrorInfo; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment