note 39292 deleted from function.mail by irchtml
| From: | irchtml@php.net | Date: | Sat, 28 Feb 2004 20:02:37 +0000 |
| Subject: | note 39292 deleted from function.mail by irchtml | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-65943@lists.php.net to get a copy of this message | ||
Note Submitter: torfi@goobubble.com
----
Here´s my mod of the very helpful piece of code that was posted by
Jan Philipp Fiedler (thanks!) below.
Mods:
1. Attach multiple files and supply filenames for attached files
(param $files is hash-array like
Array("filename.ext"=>"/path/to/filename.ext")
2. specify linebreak character as needed by your mailer
(some mailers will convert \n to \r\n automatically, so try
with either "\r\n" or "\n" as last parameter to see which
works for you)
function mail_attach($to, $from, $subject, $message, $files,$lb="\n") {
// $to Recipient
// $from Sender (like "email@domain.com" or "Name <email@domain.com>")
// $subject Subject
// $message Content
// $files hash-array of files to attach
// $lb is linebreak characters... some mailers need \r\n, others need \n
$mime_boundary = "<<<:" . md5(uniqid(mt_rand(), 1));
$header = "From: ".$from.$lb;
$header.= "MIME-Version: 1.0".$lb;
$header.= "Content-Type: multipart/mixed;".$lb;
$header.= " boundary=\"".$mime_boundary."\"".$lb;
$content = "This is a multi-part message in MIME format.".$lb.$lb;
$content.= "--".$mime_boundary.$lb;
$content.= "Content-Type: text/plain; charset=\"iso-8859-1\"".$lb;
$content.= "Content-Transfer-Encoding: 7bit".$lb.$lb;
$content.= $message.$lb;
$content.= "--".$mime_boundary.$lb;
if(is_array($files)) {
foreach($files as $filename=>$filelocation) {
if(is_readable($filelocation)) {
$data = chunk_split(base64_encode(implode("", file($filelocation))));
$content.= "Content-Disposition: attachment;".$lb;
$content.= "Content-Type: Application/Octet-Stream;"
$content.= " name=\"".$filename."\"".$lb;
$content.= "Content-Transfer-Encoding: base64".$lb.$lb;
$content.= $data.$lb;
$content.= "--".$mime_boundary.$lb;
}
}
}
if(mail($to, $subject, $content, $header)) {
return TRUE;
}
return FALSE;
}
// Example 1:
// Attach one file, "\n" as linebreak (default):
$arrFiles=Array("myfile.pdf"=>"/path/to/myfile.pdf");
mail_attach("to","from","subject","plaintext
msg",$arrFiles);
// Example 2:
// Attach two files, "\r\n" as linebreak:
$arrFiles=Array("myfile.pdf"=>"/path/to/myfile.pdf",
"anotherfile.pdf"=>"/path/to/anotherfile.pdf");
mail_attach("to","from","subject","plaintext
msg",$arrFiles,"\r\n");