PDA

View Full Version : multipart/alternative emails won't display correctly



bhDesign
04-21-2009, 08:27 AM
Can anyone point me to a resource that explains how to send text and HTML emails in the same message or can anyone find the bug in this code? When the email is read in Outlook the entire message is visible boundary text and HTML headers and the actual HTML is not interpreted. All the tags are shown. Squirrel Mail dosn't display anything.


//boundary
$boundary = "==Multipart_Boundary_x".md5(time())."x";

//mail from
$mailFrom = 'bills@bobharrisdesign.com';

//headers
$headers = "From: Williston Crossings <$mailFrom>\r\n";
$headers .= "To: $name <$email>\r\n";
$headers .= "Delivered-To: $name <$email>\r\n";
$headers .= "Reply-To: Williston Crossings <$mailFrom>\r\n";
$headers .= "Return-Path: <$mailFrom>\r\n";
$headers .= "Subject: Williston Crossings - Thank You\r\n";
$headers .= "Date: $dateSent\r\n";
$headers .= 'X-Mailer: PHP/'.phpversion()."\r\n";
$headers .= "X-sender: <$mailFrom>\r\n";
$headers .= "X-receiver: <$email>\r\n";

$headers .= "MIME-Version: 1.0;\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"$boundary\"\n";


$headersTxt = "Content-type: text/plain; charset=\"iso-8859-1\"\r\n";
$headersTxt .= "Content-Transfer-Encoding: 7bit\r\n\r\n";

$headersHTML = "Content-type: text/html; charset=\"iso-8859-1\"\r\n";
$headersHTML .= "Content-Transfer-Encoding: 7bit\r\n\r\n";


//Generate content from design file content
$fileArray = file('mail/contact_ReplyCustomer.htm');
$line = '';
foreach($fileArray as $line){
$line = str_replace('willistoncrossings.local/',$serverPath,$line);
$line = str_replace('*nameFull*',$name,$line);
$line = str_replace('*comments*',$comments,$line);
$line = str_replace('*email*',$email,$line);
$line = str_replace('*address*',$generalArray['place2'],$line);
$line = str_replace('*city*',$city,$line);
$line = str_replace('*state*',$state,$line);
$line = str_replace('*zip*',$zip,$line);

//add mail list message
if($mailList == 'checked' || $emailList == 'checked'){
$mailListMessage = 'We will also keep you informed of resort news and events.';
}
else{
$mailListMessage = '';
}
$line = str_replace('*mailListMsg*',$mailListMessage,$line );

$content_html .= $line;
$content_txt .= strip_tags($line);
}

//Subject
$subject = 'Williston Crossings - Thank You';

//create Body
$body = $boundary."\n";
$body .= $headersTxt;
$body .= "Test Text \r\n\r\n";
$body .= $boundary."\n";;
$body .= $headersHTML;
$body .= "$content_html \r\n\r\n";
$body .= $boundary."\n";

//mail to guest
$result = mail($email,
$subject,
$body,
$headers);

wysiwyg
04-21-2009, 10:11 AM
I've never done this before, but here goes.

//create Body
$body = "--{$boundary}\n";
$body .= $headersTxt;
$body .= "Test Text \r\n\r\n";
$body .= "--{$boundary}\n";
$body .= $headersHTML;
$body .= "{$content_html}\r\n\r\n";
$body .= "--{$boundary}--\n";

bhDesign
04-21-2009, 11:09 AM
My friend,
You are a genius. Thank you so much, it worked perfectly. I spent 2 days trying to figure that out.
Bob