|
Posted by Karl Groves on 11/29/06 18:58
I'm trying to work out a mail system which can send an attachment as well
as an HTML formatted message (and a default plain text version).
I found some pretty good code on PHP.net and modified it a little but I
can't seem to get it to work.
It attaches the file properly, but only displays the Plain Text message,
even in an HTML-capable mail client.
I'm guessing it has something to do with the placement of the boundaries,
but I can't figure it out.
TIA
<?php
// Is the OS Windows or Mac or Linux
if (strtoupper(substr(PHP_OS,0,3) == 'WIN')) {
$eol = "\r\n";
} elseif (strtoupper(substr(PHP_OS,0,3) == 'MAC')) {
$eol = "\r";
} else {
$eol = "\n";
}
// Set Up Message Details
$recipient = "foo@bar.com";
$subject = "E-mail Message ".date("Y/m/d H:i:s");
$from_name = "Mr. Foo Bar";
$from_email = "foo@bar.com";
$text_msg = $eol."TEXT MESSAGE BODY".$eol; // plain text version of the
message
$html_msg = $eol."<html><body><strong>HTML MESSAGE BODY</strong></body>
</html>".$eol; // teh HTML goodness
// File for Attachment
$file_name = "file.gif"; // name of the file being attached
$f_name = $path.$file_name; // path + name of the file being attached
$handle = fopen($f_name, 'rb');
$f_contents = fread($handle, filesize($f_name));
$f_contents = chunk_split(base64_encode($f_contents));
$f_type = filetype($f_name);
fclose($handle);
// Headers
$headers = "";
$headers .= "From: $from_name <$from_email>".$eol;
$headers .= "Reply-To: $from_name <$from_email>".$eol;
$headers .= "Return-Path: $from_name <$from_email>".$eol;
$headers .= "X-Sender: <$from_email>".$eol;
$headers .= "Errors-To: <$from_email>".$eol;
$headers .= "Message-ID: <".md5(uniqid(rand())).".".preg_replace("/[^a-z0-
9]/i", "", $from_name).">".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol;
$headers .= "X-Priority: 3\n";
// Boundary for marking the split & Multitype Headers
$mime_boundary = "php-".md5(rand());
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".
$mime_boundary."\"".$eol;
$msg = "";
// Attachment
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: $f_type; name=\"".$file_name."\"".$eol;
$msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= "Content-Disposition: attachment; filename=\"".$file_name."\"".
$eol.$eol;
$msg .= $f_contents.$eol.$eol;
$msg .= "--".$mime_boundary.$eol;
// Text Version
$msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= "This is a multi-part message in MIME format.".$eol;
$msg .= "If you are reading this, please update your email-reading-
software.".$eol;
$msg .= $text_msg;
$msg .= "--".$mime_boundary.$eol;
// HTML Version
$htmlalt_mime_boundary = $mime_boundary."_htmlalt";
$msg .= "Content-Type: multipart/alternative; boundary=\"".
$htmlalt_mime_boundary."\"".$eol;
$msg .= "--".$htmlalt_mime_boundary.$eol.$eol;
$msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= $html_msg;
$msg .= $eol.$eol."--".$htmlalt_mime_boundary."--".$eol.$eol;
// Finish Message
$msg .= "--".$mime_boundary."--".$eol.$eol;
// SEND THE EMAIL
ini_set(sendmail_from, $from_email); // the INI lines are to force the
From Address to be used !
mail($recipient, $subject, $msg, $headers);
ini_restore(sendmail_from);
// DEBUG STUFF
//print("<pre>$msg</pre>");
?>
--
Karl Groves
www.karlcore.com
Navigation:
[Reply to this message]
|