note 39292 added to function.mail
| From: | torfi at goobubble dot com | Date: | Fri, 23 Jan 2004 11:13:21 +0000 |
| Subject: | note 39292 added to function.mail | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-64060@lists.php.net to get a copy of this message | ||
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");
----
Manual Page -- http://www.php.net/manual/en/function.mail.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39292
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39292&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39292&report=yes
Search -- http://master.php.net/manage/user-notes.php