Create multipart email
| From: | Guus Ellenkamp | Date: | Thu, 28 May 2009 08:47:48 +0000 |
| Subject: | Create multipart email | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-293266@lists.php.net to get a copy of this message | ||
I'm trying to attach an uploaded file to an e-mail which I receive in
Outlook. Neither the first part, nor the second part displays properly. The
header looks ok when displayed on the screen. What am I missing?
See code below.
function xmail($mailto, $from_mail, $from_name, $replyto, $subject,
$message, $origname, $tempfile, $filetype) {
$file = $tempfile;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($origname);
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed;
boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: ".$filetype.";
name=\"".$name."\"\r\n"; // use
diff. tyoes here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment;
file=\"".$name."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
echo $header;
if (mail($mailto, $subject, "test", $header)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
}
// how to use
$my_name = "Guus";
$my_mail = "guus@activediscovery.net";
$my_replyto = "guus@activediscovery.net";
$my_subject = "This is a mail with attachment.";
$my_message = "Hallo,\r\ndo you like this script? I hope it will
help.\r\n\r\ngr. Olaf";
xmail("guus@activediscovery.net", $my_mail, $my_name, $my_replyto,
$my_subject, $my_message,$fileName, $fileTempName, $fileType);