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
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);
?>
$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);
?>
Lovely dude 🙂
Thanks a lot man, This was really so Helpful, Thanksssssss
Pingback : How to send email using HTML templates in Codeigniter - Anil Labs