views:

376

answers:

4

I'm using a mail function which is sending back a message that is contained within the variable $body. I want to send the message to myself with certain words in bold and a number of break tags.

Here is my code(HEREDOC SYNTAX):

$body = <<<CTS
<h1><b>Order for $name on datevariable</b></h1><br /><br />

<b><u>Administrative Details</u></b><br />
<b>Employee ID:</b> $id<br />
<b>Cost Center:</b> $cost_center<br />
<b>Approved By:</b> $approved_by<br />
<b>Delivery Bldg:</b> $delivery_bldg<br />
<b>Delivery Contact Email:</b> $delivery_contact<br />
<b>Ext:</b> $del_contact_ext<br />
CTS;

For some reason when I receive the email, it looks like this:

<h1><b>Order for James Important on datevariable</b></h1><br /><br />
<b><u>Administrative Details</u></b><br />
<b>Employee ID:</b> 213123<br />
<b>Cost Center:</b> 132123<br />
<b>Approved By:</b> Chris Seckla<br />
<b>Delivery Bldg:</b> 6<br />
<b>Delivery Contact Email:</b> [email protected]<br />
<b>Ext:</b> 56<br />

It fills in the variable values but for some reason ignores the html tags. Can someone please tell me how to fix this?

Also, it is ignoring the break tags and only putting breaks when I leave a line of whitespace. Any ideas on this?

+3  A: 

You have to set an additional header when sending from php for the email to display using html format:

Content-Type: text/html; charset=whatever

MartinodF
+10  A: 

If this is an email, you have to set the mime type to text/html instead of text/plain. Otherwise your email reader will interpret the message as plain text and ignore all the html tags.

For example, lets say you are calling the mail function like this:

mail( $to, $subject, $message, $headers )

In $headers, you'd need something along these lines (adjusted for your particular case, of course):

$headers = "From: [email protected]\r\n";
$headers .= "Reply-To: [email protected]\r\n";
$headers .= "Content-Type: text/html";

If you want to send both plain text and html email (so non-html readers can see a clean version of the email without all those tags), I would suggest taking a look at this tutorial for a more detailed explanation of multipart emails.

Marc W
+3  A: 

Make sure you send the header that states it is HTML...

example:

mail('[email protected]', 'Subject',
'<html><body><b>Hello</b></body></html>',
"To: The Receiver <[email protected]>\n" .
"From: The Sender <[email protected]>\n" .
"MIME-Version: 1.0\n" .
"Content-type: text/html; charset=iso-8859-1");
Phobis
Whenever I include this line: "MIME-Version: 1.0\n", Apple Mail shows the HTML tags inside the body, I have to remove it to have Mail show HTML, why is this anyone?
Anriëtte Combrink
A: 

If you would like to send mail with PHP I would recommend you with PEAR Mail_Mime package instead of plain mail function

Pavels