Sending HTML Email using PHP

PHP has ability to send the mail from the domain using mail function,  this session we are going to discuss about the mail function and its benefit.  First we will create simple form and PHP script to send mail.

Our html file have to, subject and message to send mail.

<html>
<head>
<title>Codeasearch.com - Sending HTML Email using PHP - Email form</title>
</head>
<body>
<table>
<form action="mail.php" method="post">
<tr>
<td>To</td>
<td><input type="text" name="txtTo" /></td>
</tr>
<tr>
<td>Subject</td>
<td><input type="text" name="txtSubject" /></td>
</tr>
<tr>
<td>Message</td>
<td><textarea rows="6" cols="30" name="txtMessage"></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="btn" value="Send Mail" /></td>
</tr>
</form>
</table>
</body>
</html>

Our server side script will send the post data using mail function, which mentioned in below. If you are trying to send with html content It will send as it is (html tag in email)

<?php
// receiver of the mail
$to = $_POST["txtTo"];

// subject of the email to be sent
$subject = $_POST["txtSubject"];

// message to be sent
$message = $_POST["txtMessage"];

//from email address mentioned default
mail($to,$subject,$message,"From:info@domain.com");

Now we will send HTML in our Email by setting the MIME type in headers.

<?php
// receivers of the mail
$to = $_POST["txtTo"];

// subject of the email to be sent.
$subject = $_POST["txtSubject"];

// message to be sent
$message = $_POST["txtMessage"];

//from email address mentioned default
mail($to,$subject,$message,"From:info@domain.com");

You can also send HTML Email content for newsletter , user subscription and alerts, so it will be very readable instead of plain text for the users.