PHP: Send HTML Email (mail)

by Yang Yang on April 23, 2009

As we all know the simplest approach to create an email message and send it out is to use the php mail() function. A typical usage example would be:

mail('', 'Subject Title', 'Message body goes here.');

However, as the mail() function sends emails in text/plain mime type by default, if you include HTML code in the message body, it would not be interpreted as HTML at all. Instead, all the tags and attributes are displayed as they are.

To work around this and send HTML email with PHP mail() function, you will have to modify the message mime type to text/html. In practice, use the mail() function with an additional argument to set arbitrary headers for the email message:

$to = '';
$subject = 'Purchase Successful!';
$message = '
	<title>Purchase Successful!</title>
	<p>Here are the order details:</p>
	<p> ... </p>
	<p><a href="">back to store</a></p>

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

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

Now the email dispatched to the recipient will have all the HTML in it working as expected.

Yasitha Dinushi October 18, 2010 at 3:06 pm

Hi, can i send html emails with attachments

Yang Yang October 19, 2010 at 4:24 pm


Seems to me you have read the post here:

Aneeq May 3, 2012 at 2:45 pm

To send HTML mail in PHP, you need to use some additional headers. Below is the code to send HTML mail in PHP.

$to = ““;
$subject= “Subject of email”;
$message= “This is HTML mail.”;
$fromName = “Name of the sender”;
$fromEmail = ““;

// To send HTML mail, the Content-type header must be set
$headers = “MIME-Version: 1.0″ . “\r\n”;
$headers .= “Content-type: text/html; charset=iso-8859-1″ . “\r\n”;

// Additional headers
$headers .=”From: $fromName ”.”\r\n”;

//Mail function
mail($to, $subject, $message, $headers);

sonelal October 5, 2012 at 12:52 pm

This code is not working for sending long html message.
Please give me some idea to send long html message or you can say long html tables.
Your help is very appreciated.

Filippo October 25, 2012 at 1:08 pm

try using something like this:

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

$to = ‘’;

$subject = ‘Website Change Reqest’;

$headers = “From: ” . strip_tags($_POST[‘req-email’]) . “\r\n”;
$headers .= “Reply-To: “. strip_tags($_POST[‘req-email’]) . “\r\n”;
$headers .= “CC:\r\n”;
$headers .= “MIME-Version: 1.0\r\n”;
$headers .= “Content-Type: text/html; charset=ISO-8859-1\r\n”;

$message = ”;
$message .= ‘Hello, World!’;
$message .= ‘This is a cool way to write long html email’;
$message .= ”;

hope can help, regards


asd May 15, 2012 at 1:29 am

thank you so much şukunu verdim piç 🙂 translate from google translate turkish to english

Bruce August 1, 2012 at 10:30 pm

I am new to PHP,

i had a mail form-page with $to, $from, $subject and $x1, $x2, $x3 ( as costum headers).

of which i fill-up the required fields i.e $to, $from, $subject and a optional field $x1 and deliberately keep $x2, $x3 fields empty, now when i submit the form the mail is sent….so far no problems.

Now when i check the mail headers of the received mail i get to see all the headers i.e. $to, $from, $subject, $x1, $x2, $x3. Of which $x2 and $x3 off-course are with empty headers.

Mail Headers from received mail

To: [email protected]
Subject: test-3
From: [email protected]
Reply-To: [email protected]
X-1: x1_header

Rafiq August 2, 2012 at 5:51 pm

If the content/Message comes from any Editor then How to send Html email

gabrial August 9, 2012 at 6:57 am

thanks a lot … lottttttttt .. i was really struggling with that .. now its ok

Vipin.RV October 2, 2012 at 2:01 pm

realy simple. expecting more ,need help

Chinthaka March 17, 2013 at 2:24 pm

Simple 7 very Useful thing. Thank You !

Aby April 14, 2013 at 8:25 pm

PHP Code

$to = “”;
$from = $email;
$subject = “Time Sheet”;

$message = ”;
$message .= ”;
$message .= “”.$name.”” . strip_tags($_REQUEST[“getDesignation”]) . ” “. strip_tags($_REQUEST[“getEmail”]) .” “. strip_tags($_REQUEST[“getPhone”]) .””;
$message .= “9 am – 11 am 11 am – 1 pm 2 pm – 4 pm 4 pm – 6 pm Work Location: “;
$message .= “” . strip_tags($txtarea1) . “” . strip_tags($txtarea2) . “” . strip_tags($txtarea3).”” . strip_tags($txtarea4). “” . strip_tags($txtarea5) . “”;
$message .= “”;
$message .= “”;

$headers = “From:” . $from;
echo “Mail Sent.”;

IN Gmail
Mr Admin Istrator IT Manager 9198477633429 am – 11 am 11 am – 1 pm 2 pm – 4 pm 4 pm – 6 pm Work Location: Teatttttttttttttttttttttttttaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbcccccccccccccccccccddddddddddddddd

Please give a solution for that……

rulo May 4, 2013 at 7:25 am

example isnot working i received just text in inbox hotmail

Purchase Successful!

Here are the order details:

back to store

Comments on this entry are closed.

Previous post:

Next post: