This is a mail function I have started using. It is a combination of code I have found on the web and a bit of tweaking I have done here and there. It has worked great so far, but any suggestions to improve would be appreciated.
define ("NO_HTML", "This is a MIME encoded message.\r\n\r\n");
define ("FROM_NAME", "David Hasselhof");
define ("FROM_EMAIL", "david@foo.com");
function mailit($to,$subject,$message,$message_html="",$to_name="") {
//add From: header
if (FROM_NAME && FROM_EMAIL) {
$from_name=ereg_replace(","," ",FROM_NAME);
$from_name=ereg_replace(";"," ",FROM_NAME);
$headers = "From: ".FROM_NAME." <".FROM_EMAIL.">\r\n";
}
elseif (FROM_EMAIL) {
$headers = "From: ".FROM_EMAIL."\r\n";
}
if ($to_name) {
$to_name=ereg_replace(","," ",$to_name);
$to_name=ereg_replace(";"," ",$to_name);
$to = $to_name." <".$to.">";
}
if ($message_html) {
//specify MIME version 1.0
$headers .= "MIME-Version: 1.0\r\n";
//unique boundary
$boundary = uniqid("MAILIT");
//tell e-mail client this e-mail contains//alternate versions
$headers .= "Content-Type: multipart/alternative" .
"; boundary = $boundary\r\n\r\n";
//add message to people with clients who don't understand MIME
$headers .= NO_HTML;
//plain text version of message
$headers .= "--$boundary\r\n" .
"Content-Type: text/plain; charset=ISO-8859-1\r\n" .
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= chunk_split(base64_encode($message));
//HTML version of message
$headers .= "--$boundary\r\n" .
"Content-Type: text/html; charset=ISO-8859-1\r\n" .
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= chunk_split(base64_encode($message_html));
//send message
return mail($to, $subject, "", $headers);
}
else {
//plain text version of message
return mail($to, $subject, $message, $headers);
}
}
David (http://www.esquilax.com/baywatch/index.shtml)