AdRock Posted March 26, 2009 Share Posted March 26, 2009 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 http://www.tek-tips.com/faqs.cfm?fid=2681 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 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!"; } 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='color:purple'>HTML</b> text email. It is very cool.</body></html> --==MULTIPART_BOUNDARY_f50866ab15be0d00424d1166c9c4fb5f-- Quote Link to comment https://forums.phpfreaks.com/topic/151310-htmlplain-text-email-not-sending-properly/ Share on other sites More sharing options...
MadTechie Posted March 26, 2009 Share Posted March 26, 2009 Your header is messed up! it should be something like this $boundary = uniqid(time()); $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); Also Note that using the above you can probably get away with this //mail('', 'Test mail', $message, $headers); as the header is complete Quote Link to comment https://forums.phpfreaks.com/topic/151310-htmlplain-text-email-not-sending-properly/#findComment-794795 Share on other sites More sharing options...
AdRock Posted March 27, 2009 Author Share Posted March 27, 2009 Just out of curiosity, how does the email send if i do away with //mail('', 'Test mail', $message, $headers); Quote Link to comment https://forums.phpfreaks.com/topic/151310-htmlplain-text-email-not-sending-properly/#findComment-794818 Share on other sites More sharing options...
MadTechie Posted March 27, 2009 Share Posted March 27, 2009 The to: is in the header.. infact you could remove everything except the header if you construct it well.. Quote Link to comment https://forums.phpfreaks.com/topic/151310-htmlplain-text-email-not-sending-properly/#findComment-794859 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.