How to add HTML content in php mail() function

In this post i would like to explain about how to add HTML content to PHP mail() function. Few servers are supports PHP mailer class then we can send mails with Sending mails using PHP mailer class . And few servers are not supporting PHP mailer class. For those below function will be useful to send mails with HTML content.

How to add HTML content in PHP mail() function by Anil Kumar Panigrahi

How to add HTML content in PHP mail() function by Anil Kumar Panigrahi

The PHP Code

 

<?php
$to = "[To Address]";
$subject = "Contact Details";

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

// Additional headers
$headers .= 'To: Anil Labs <To Address>' . "\r\n";
$headers .= 'From: Anil Labs <To address>' . "\r\n";
// Additional headers
$email_body = "<table width="721" height="186" border="0" align="left">
  <tr>
    <td colspan="
3" style="background-color:#666666; color:#FFFFFF" align="left"><strong>Contact  - Us </strong></td>
 </tr>
  <tr><td>First Name : </td> <td>:</td> <td>Anil Labs</td><tr>
   <tr><td>Email</td> <td>:</td> <td>To Address</td></tr>
   <tr><td>Subject</td> <td>:</td> <td>".$subject."</td></tr>
   <tr><td valign='top'>Comments</td> <td valign='top'>:</td> <td>test</td></tr>
   
 
</table>";

$message = "
<html>
<head>
<title></title>
</head>
<body ><center>
".stripslashes($email_body)."
</center>
</body>
</html>
";

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

?>

Anil Kumar Panigrahi

With more than 10 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online

Contact : [email protected]

You may also like...

2 Responses

  1. Thanks a lot man, This was really so Helpful, Thanksssssss

Leave a Reply

Your email address will not be published. Required fields are marked *