Php mail: how to send html?

lleoun picture lleoun · Feb 4, 2011 · Viewed 34.9k times · Source

The code below is sending an email correctly but for the body. I need to show html in the body of the message and I cannot make it. The examples in the web won't send the email :(

How can I fix my code to send the email with the html in the body?

Thanks a ton!

<?php

$to = '[email protected]';

$subject = 'I need to show html'; 

$from ='[email protected]'; 

$body = '<p style=color:red;>This text should be red</p>';

ini_set("sendmail_from", $from);

$headers = "From: " . $from . "\r\nReply-To: " . $from . "";
  $headers .= "Content-type: text/html\r\n"; 
if (mail($to, $subject, $body, $headers)) {

  echo("<p>Sent</p>");
 } else {
  echo("<p>Error...</p>");
 }

?>

Answer

helle picture helle · Feb 4, 2011

use this header for the mail:

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

and for the content/body:

<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
... ... ...

it's important to use inline css commands and recommanded to use tables for the interface.

...

In your Mail-Body you than have to put HTML code with head and body