Search code examples
phphtml-emailmail-form

Mail Function displaying HTML as Text


I have the following mail function:

<?php
{
$to = "alee@####.com";
$subject = "General Contact.";
$headers  = "From: ####<noreply@####.com>\r\n";
$headers .= "Reply-To: info@####.com\r\n";
$headers .= "Return-Path: info@####.com\r\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$name = filter_input(INPUT_POST, 'name');
$telephone = filter_input(INPUT_POST, 'phone');
$nature = filter_input(INPUT_POST, 'nature');   
$comments = filter_input(INPUT_POST, 'comments');
$message = 
"<span style='font-weight: bold;'>Name: </span>"."<br />".$name."<br />"."<br />".
"<span style='font-weight: bold;'>Telephone: </span>"."<br />".$telephone."<br />"."<br   />".
"<span style='font-weight: bold;'>Nature of enquiry: </span>"."<br />".$nature."<br />"."<br />".
"<span style='font-weight: bold;'>Comments: </span>"."<br />".$comments."<br />"."<br />";

mail($to, $subject,"We have received a message via the online form at www.####.com<br />  <br />" . $message, $headers);
echo "Your message was successfully sent. Please <a href='contact.php'>click here</a> to return to the site.";
?>
<?php  }
?>

The problem I have is that when the form is used and the message hits my inbox, the HTML is being displayed as plain text, and not being displayed properly. So I can see all the inline HTML element, i.e. the
will be shown as part of the paragraph and not display a new line. Any help would be awesome!

EDIT:

Reply-To: info@###.com
Return-Path: info@###.com
MIME-Version: 1.0
Content-type: text/html; charset: utf8

We have received a message via the online form at www.###.com<br /><br /><span    style='font-weight: bold;'>Name: </span><br />Aaron Lee<br /><br /><span style='font-weight:   bold;'>Telephone: </span><br />#####<br /><br /><span style='font-weight:   bold;'>Nature of enquiry: </span><br />###<br /><br /><span style='font-weight:   bold;'>Comments: </span><br />Testttttttttttt<br /><br />

Solution

  • Try to replace the lines :

    $headers .= 'MIME-Version: 1.0' . "\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    

    With :

    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset: utf8\r\n";