How To Send Mail With HTML and CSS Style

In this tutorial I will described How to send mail through PHP with HTML tag. Normally, when we are using basic mail() function of PHP, Its not included HTML tag.So if You want mail with HTML tag then you need define Header "Content-type:text/html" of email.

We can add CSS style with HTML content as well and send using PHP mail. The combination of CSS and HTML will create a beautiful and nice HTML email with PHP.

You can also check other tutorial of php mail,

Sample Code To send mail With HTML and CSS style.

$to = [email protected];
$subject = "Expired access token of Facebook";
$messageHed = '

<i>Dear Admin,
        The Facebook Application Token will expire next couple of days.
        Please, Login and regenerate access token.</i>
';
$headers='';
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From:' [email protected]. "\r\n";
$messageBody = "
<div class="topHeader">
<div class="logo"> <img src="". IMGESPATH ."a_logo.png" alt="abcLogo"></div>
</div>
<div>".$messageHed."</div>
<div class="datacontainer">
<div class="Data_table">

";
foreach($results as $result){
$messageBody .= "

";
}
$messageBody .= '
<table cellpadding="0" cellspacing="1" border="0" width="100%" bgcolor="#fff">
<tbody>
<tr>
<th align="left">Application Name</th>
<th align="left">Application SiteId</th>
</tr>
<tr>
<td colspan="2"></td>
</tr>
<tr>
<td>".$result['Name']."</td>
<td>".$result['ApplicationSiteId']."</td>
</tr>
</tbody>
</table>
</div>
</div>
';
try{
$isSend = mail($to,$subject,$messageBody,$headers);
if($isSend) {
echo "Mail sent on " . date('Y-m-d H:i:s') .'.';
}
}catch (Exception $e){
echo $e->getMessage();
}

I have added extra headers Content-Type which notify mail services that parse this email as HTML.PHP mail headers are providing options to add optional parameters in php mail() function.You can add From,CC,Bcc and attachment etc suing php mail headers.

One thought on “How To Send Mail With HTML and CSS Style

Leave a Reply

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