note 114954 deleted from function.mail by tularis
| From: | tularis@php.net | Date: | Mon, 05 May 2014 11:56:48 +0000 |
| Subject: | note 114954 deleted from function.mail by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-199417@lists.php.net to get a copy of this message | ||
Note Submitter: Manu Manjunath
----
To send mails with attachments in a procedural fashion, you may write a function like this:
<?php
function mail_with_attachments($to, $subject, $message, Array $filepaths, $from = null, $replyto =
null) {
$uid = md5(uniqid(time()));
$from = str_replace(array("\r", "\n"), '', $from);
$headers = (empty($from) ? "" : "From: $from\r\n")
. (empty($replyto) ? "" : "Reply-To: $replyto\r\n")
. "MIME-Version: 1.0\r\n"
. "Content-Type: multipart/mixed; boundary=\"$uid\"\r\n\r\n"
. "This is a multi-part message in MIME format.\r\n"
. "--$uid\r\n"
. "Content-type: text/plain; charset=iso-8859-1\r\n"
. "Content-Transfer-Encoding: 7bit\r\n\r\n"
. "$message\r\n\r\n";
foreach ($filepaths as $filepath) {
if(!file_exists($filepath))
continue;
$filename = basename($filepath);
$file_contents = file_get_contents($filepath);
$file_contents_base64 = base64_encode($file_contents);
$content = chunk_split($file_contents_base64);
$headers .= "--$uid\r\n"
. "Content-Type: application/octet-stream; name=\"$filename\"\r\n"
. "Content-Transfer-Encoding: base64\r\n"
. "Content-Disposition: attachment; filename=\"$filename\"\r\n\r\n"
. "$content\r\n\r\n";
}
$headers .= "--$uid--";
return mail($to, $subject, "", $headers);
}