I have been working on a newsletter system for a site i'm finishing and everything is working fine apart from when i'm trying to send html/plain text email
I found an article on this site [URL unfurl="true"]http://www.tek-tips.com/faqs.cfm?fid=2681[/url]
I created my own function that gets post data from a form and creates the html. The function then calls another function with the code in the article to send the email.
The email sends fine but instead of an html message it appears as plain text but with all the html code displayed and there is no plain text version (i have checked) so it looks like it just sent a failed html version.
What could be causing this problem?
Here is my code
and here is what is output from that
I found an article on this site [URL unfurl="true"]http://www.tek-tips.com/faqs.cfm?fid=2681[/url]
I created my own function that gets post data from a form and creates the html. The function then calls another function with the code in the article to send the email.
The email sends fine but instead of an html message it appears as plain text but with all the html code displayed and there is no plain text version (i have checked) so it looks like it just sent a failed html version.
What could be causing this problem?
Here is my code
Code:
function sendBothEmail($plain_text, $HTML, $from, $to, $subject) {
$notice_text = "This is a multi-part message in MIME format.";
$semi_rand = md5(time());
$mime_boundary = "==MULTIPART_BOUNDARY_$semi_rand";
$mime_boundary_header = chr(34) . $mime_boundary . chr(34);
$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
--$mime_boundary--";
if (@mail($to, $subject, $body,
"From: " . $from . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/alternative;\n" .
" boundary=" . $mime_boundary_header))
return "Correct";
else
return "Email NOT sent successfully!";
}
Code:
function send_newsletter() {
$from = "Me <newsletters@mysite.com>";
$plain_text = "This is a plain text email.\r\nIt is very cool.";
$HTML = "<html><body>This is an <b style='color:purple'>HTML</b> text email.\r\nIt is very cool.</body></html>";
$to = "You <you@yoursite.com>";
$result = sendBothEmail($plain_text,$HTML,$from,$to,$subject);
if($result != 'Correct') {
return "Emails not sent";
}
}
and here is what is output from that
This is a multi-part message in MIME format.
--==MULTIPART_BOUNDARY_f50866ab15be0d00424d1166c9c4fb5f
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
This is a plain text email.
It is very cool.
--==MULTIPART_BOUNDARY_f50866ab15be0d00424d1166c9c4fb5f
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
<html><body>This is an <b style='colorurple'>HTML</b> text email.
It is very cool.</body></html>
--==MULTIPART_BOUNDARY_f50866ab15be0d00424d1166c9c4fb5f--