send a multipart email (both HTML and plain text)

Example 1

<?php

$notice_text = “This is a multi-part message in MIME format.”;
$plain_text = “This is a plain text email.\r\nIt is very cool.”;
$html_text = “<html><body>This is an <b style=’color:purple’>HTML</b> text email.\r\nIt is very cool.</body></html>”;

$semi_rand = md5(time());
$mime_boundary = “==MULTIPART_BOUNDARY_$semi_rand”;
$mime_boundary_header = chr(34) . $mime_boundary . chr(34);

$to = ‘abc@abc.com’;
$bcc = “You <you@you.com>, Them <them@them.com>”;
$from = “Me.com <me@me.com>”;
$subject = “My Email”;

$body = “$notice_text

–$mime_boundary
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

$plain_text

–$mime_boundary
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

$html_text

–$mime_boundary–“;

if (@mail($to, $subject, $body,
“From: ” . $from . “\n” .
“bcc: ” . $bcc . “\n” .
“MIME-Version: 1.0\n” .
“Content-Type: multipart/alternative;\n” .
” boundary=” . $mime_boundary_header))
echo “Email sent successfully.”;
else
echo “Email NOT sent successfully!”;
?>

http://www.tek-tips.com/faqs.cfm?fid=2681

Example 2

Headers

MIME-Version: 1.0

From: Foo <foo@bar.com>

Subject: Test mail

Content-Type: multipart/alternative;boundary=np4f377521487cd

Body

This is a MIME encoded message.

 

–np4f377521487cd

Content-type: text/plain;charset=utf-8

 

This is the text/plain version.

 

–np4f377521487cd

Content-type: text/html;charset=utf-8

 

This is the <b>text/html</b> version.

 

–np4f377521487cd–


<?php

 

$boundary = uniqid(‘np’);

 

 

$headers = “MIME-Version: 1.0\r\n”;

$headers .= “From: Foo <foo@bar.com>\r\n”;

$headers .= “Subject: Test mail\r\n”;

$headers .= “Content-Type: multipart/alternative;boundary=” . $boundary . “\r\n”;

 

 

$message = “This is a MIME encoded message.”;

 

$message .= “\r\n\r\n–” . $boundary . “\r\n”;

$message .= “Content-type: text/plain;charset=utf-8\r\n\r\n”;

$message .= “This is the text/plain version.”;

 

$message .= “\r\n\r\n–” . $boundary . “\r\n”;

$message .= “Content-type: text/html;charset=utf-8\r\n\r\n”;

$message .= “This is the <b>text/html</b> version.”;

 

$message .= “\r\n\r\n–” . $boundary . “–“;

 

//mail(‘bar@foo.com’, ‘Test mail’, $message, $headers);

 

?>

 

http://krijnhoetmer.nl/stuff/php/html-plain-text-mail/
, , ,