Why are the html tags showing up in my string in PHP?

10,578

Solution 1

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.

Solution 2

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

Solution 3

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"); 
Share:
10,578
zeckdude
Author by

zeckdude

Front and back-end Developer from Los Angeles, CA

Updated on June 05, 2022

Comments

  • zeckdude
    zeckdude almost 2 years

    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?

  • Anriëtte Myburgh
    Anriëtte Myburgh over 14 years
    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?
  • VJ2013
    VJ2013 almost 8 years
    You need to add the header: Content-Type: text/html