Send Email with Attachment

 
<?php

$to = 'recipient@example.com';
$subject = 'This is the subject';
$headers = "From: sender@example.com\r\nReply-To: sender@example.com\r\n"
    . "Content-Type: multipart/alternative; boundary=\"1234567890\"";

$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));

$message = 
"--1234567890
Content-Type: multipart/alternative; boundary=\"abcdefghij\"

--abcdefghij
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

This is the text version of the message. 

--abcdefghij
Content-Type: text/html; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

<div>This is the html version of the message.</div>

--abcdefghij--

--1234567890
Content-Type: application/zip; name=\"attachment.zip\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

" . $attachment . "
--1234567890--";

mail($to, $subject, $message, $headers);

?>

 

Related Snippets

•  Send Email with utf-8 characters on the subject
•  HTTP Headers to Download a File
•  Set time limit indefinitely
•  Send Email in Html and Plain Text formats
•  Send Email in Html format
•  Send Email with CC and BCC