Php mail: how to send html?

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!


$to = '<a href="" class="__cf_email__" data-cfemail="ff9286929e9693bf929e9693d19c9092">[email protected]</a>';

$subject = 'I need to show html'; 

$from ='<a href="" class="__cf_email__" data-cfemail="26435e474b564a4366435e474b564a430845494b">[email protected]</a>'; 

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

ini_set("sendmail_from", $from);

$headers = "From: " . $from . "rnReply-To: " . $from . "";
  $headers .= "Content-type: text/htmlrn"; 
if (mail($to, $subject, $body, $headers)) {

 } else {



Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

use this header for the mail:

 $header  = "MIME-Version: 1.0rn";
 $header .= "Content-type: text/html; charset: utf8rn";

and for the content/body:

        <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

Method 2

Have you looked at the headers of the incoming mail? It says

Reply-To: [email protected]: text/html

Simply add another rn here:

Reply-To: " . $from . "rn";

Method 3

I recommend rather than messing around with doing this yourself you use one of the many free classes available all over the web to do it.

I would recommend: PHPMailer

Method 4

I found this works well!


//define the receiver of the email
$to = '<a href="" class="__cf_email__" data-cfemail="60190f151201040412051313200518010d100c054e030f0d">[email protected]</a>';
//define the subject of the email
$subject = 'Test HTML email'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with rn
$headers = "From: <a href="" class="__cf_email__" data-cfemail="7d0a181f101c0e09180f3d18051c100d1118531e1210">[email protected]</a>rnReply-To: <a href="" class="__cf_email__" data-cfemail="ceb9abaca3afbdbaabbc8eabb6afa3bea2abe0ada1a3">[email protected]</a>";
//add boundary string and mime type specification
$headers .= "rnContent-Type: multipart/alternative; boundary="PHP-alt-".$random_hash."""; 
//define the body of the message.
ob_start(); //Turn on output buffering
--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p> 

--PHP-alt-<?php echo $random_hash; ?>--
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";

Method 5

Simple answer: Don’t do it. HTML emails are evil and annoying. At least if there’s no PROPER plaintext version included. Proper = same information like in the HTML version, not just a stupid comment about getting another email client or a link to the html version if it’s available on the web.

If you really need it:

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x