Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTML email is sent as text

Status
Not open for further replies.

AdRock952

Technical User
Feb 6, 2004
16
GB
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
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='color:purple'>HTML</b> text email.
It is very cool.</body></html>

--==MULTIPART_BOUNDARY_f50866ab15be0d00424d1166c9c4fb5f--
 
my advice is to use phpmailer.

in the alternative here is a mail function i posted here a few years ago

Code:
 //create a boundary for multipart messages
    $b = md5(time());
    $sep = "\r\n";
    //create the headers for a multipart email
    
    $headers  = "From: $from$sep";
    $headers .= "To: $to$sep";
    $headers .= "Return-Path: $from$sep";
    $headers .= "MIME-Version: 1.0$sep";
    $headers .= "Content-Type: multipart/mixed; boundary=\"$b\"$sep";
    $headers .= "$sep";    

    //add useful information for bad email clients
    
    $message = "This is a multi-part message in mime format $sep";
    $message .= "$sep";    

    //create the message part
    $message .= "--$b$sep";
    $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"$sep";
    $message .= "Content-Transfer-Encoding: 8bit$sep";
    $message .= $sep;
    $message .= $msg;
    $message .= $sep;
    $message .= $sep;
    //finish the mail
    $message .= "--$b--$sep";

    //send the mail
    $result = @mail($to, $subject,$message, $headers);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top