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.

In today’s digital age, email communication plays a pivotal role in both personal and professional spheres. Sending plain text emails is common, but sometimes, you may need to enhance your emails with HTML content to make them visually appealing and informative. This tutorial will guide you through the process of adding HTML content to your emails using PHP’s powerful mail() function. By the end of this tutorial, you’ll have the knowledge and tools to create beautifully formatted emails that stand out in your recipients’ inboxes.

How to add HTML content in php mail() function

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

The PHP Code

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?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);

?>

Incorporating HTML content into your PHP-generated emails opens up a world of possibilities for creating engaging and dynamic communication. Whether you’re sending newsletters, transactional emails, or notifications, the ability to include images, links, and customized styling can significantly improve the impact of your messages. By following the steps outlined in this tutorial, you’ve learned how to harness the full potential of PHP’s mail() function to craft HTML-rich emails that leave a lasting impression on your recipients.

Remember to test your emails thoroughly and adhere to best practices to ensure compatibility across various email clients and devices. As you continue to refine your skills and explore advanced HTML email techniques, you’ll be well-equipped to deliver compelling content and maintain effective communication in your web applications or business processes.


3 Comments

Abdul Baquee · October 25, 2011 at 7:53 pm

Lovely dude 🙂

beshoy William · July 19, 2012 at 1:51 pm

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

How to send email using HTML templates in Codeigniter - Anil Labs · December 7, 2017 at 10:54 am

[…] how to send email using HTML templates in Codeigniter, earlier post we learned how to send mails using HTML content and using phpmailer and gmail. Now we learn how to send mails in Codeigniter. By follow the below […]

Leave a Reply

Avatar placeholder

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