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(sub str(PHP_OS,0,3) == 'WIN')) {
$eol = "\r\n";
} elseif (strtoupper(sub str(PHP_OS,0,3) == 'MAC')) {
$eol = "\r";
} else {
$eol = "\n";
}
// Set Up Message Details
$recipient = "foo@bar.co m";
$subject = "E-mail Message ".date("Y/m/d H:i:s");
$from_name = "Mr. Foo Bar";
$from_email = "foo@bar.co m";
$text_msg = $eol."TEXT MESSAGE BODY".$eol; // plain text version of the
message
$html_msg = $eol."<html><bo dy><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_nam e; // path + name of the file being attached
$handle = fopen($f_name, 'rb');
$f_contents = fread($handle, filesize($f_nam e));
$f_contents = chunk_split(bas e64_encode($f_c ontents));
$f_type = filetype($f_nam e);
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(r and())).".".pre g_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_boundar y.$eol;
$msg .= "Content-Type: $f_type; name=\"".$file_ name."\"".$eol;
$msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= "Content-Disposition: attachment; filename=\"".$f ile_name."\"".
$eol.$eol;
$msg .= $f_contents.$eo l.$eol;
$msg .= "--".$mime_boundar y.$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_boundar y.$eol;
// HTML Version
$htmlalt_mime_b oundary = $mime_boundary. "_htmlalt";
$msg .= "Content-Type: multipart/alternative; boundary=\"".
$htmlalt_mime_b oundary."\"".$e ol;
$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_boundar y."--".$eol.$eol ;
// SEND THE EMAIL
ini_set(sendmai l_from, $from_email); // the INI lines are to force the
From Address to be used !
mail($recipient , $subject, $msg, $headers);
ini_restore(sen dmail_from);
// DEBUG STUFF
//print("<pre>$ms g</pre>");
?>
--
Karl Groves
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(sub str(PHP_OS,0,3) == 'WIN')) {
$eol = "\r\n";
} elseif (strtoupper(sub str(PHP_OS,0,3) == 'MAC')) {
$eol = "\r";
} else {
$eol = "\n";
}
// Set Up Message Details
$recipient = "foo@bar.co m";
$subject = "E-mail Message ".date("Y/m/d H:i:s");
$from_name = "Mr. Foo Bar";
$from_email = "foo@bar.co m";
$text_msg = $eol."TEXT MESSAGE BODY".$eol; // plain text version of the
message
$html_msg = $eol."<html><bo dy><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_nam e; // path + name of the file being attached
$handle = fopen($f_name, 'rb');
$f_contents = fread($handle, filesize($f_nam e));
$f_contents = chunk_split(bas e64_encode($f_c ontents));
$f_type = filetype($f_nam e);
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(r and())).".".pre g_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_boundar y.$eol;
$msg .= "Content-Type: $f_type; name=\"".$file_ name."\"".$eol;
$msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= "Content-Disposition: attachment; filename=\"".$f ile_name."\"".
$eol.$eol;
$msg .= $f_contents.$eo l.$eol;
$msg .= "--".$mime_boundar y.$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_boundar y.$eol;
// HTML Version
$htmlalt_mime_b oundary = $mime_boundary. "_htmlalt";
$msg .= "Content-Type: multipart/alternative; boundary=\"".
$htmlalt_mime_b oundary."\"".$e ol;
$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_boundar y."--".$eol.$eol ;
// SEND THE EMAIL
ini_set(sendmai l_from, $from_email); // the INI lines are to force the
From Address to be used !
mail($recipient , $subject, $msg, $headers);
ini_restore(sen dmail_from);
// DEBUG STUFF
//print("<pre>$ms g</pre>");
?>
--
Karl Groves
Comment