Most people know about php’s mail function. Even if it’s not the only way to send emails it is very used because of its simplicity. One thing that is not very often found in tutorials but is very useful might be the way to add attachments to the message using only this function and not a heavyweight classes.
This is in fact very easy. All you need to do is to provide the full message body (not only the text/html) and that’s where you will add the attachment in a block like this one:
--PHP-mixed-$random_hash Content-Type: application/zip; name="$filename" Content-Transfer-Encoding: base64 Content-Disposition: attachment $attachment
Where $filename is the name you want to show to the email receiver and $attachment is the encoded file that you get like this:
$attachment = chunk_split(base64_encode(file_get_contents($value)));
That’s all you need to do. You can download here a full working example, that supports multiple attachments (the archive also contains two test files for this) and all you need to do to use it is to change the sender email address and the one for the receiver.
Make sure that as sender you are using and email address that exists on your server.
That’s all. Feel free to post your comments/questions using the form below.