Wednesday, November 26, 2014

how to send mail in php from localhost?




In previous chapter you got idea to configure your XAMPP. If you've configured then move to code to send mail and if not then configure at first. click here to configure.
Now it's time to write code in order to send mail. Below is the code to send mail using php:

<?php

$to = "your-gmail-id@gmail.com";
$subject = "HTML email using PHP";

$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This is an HTML Email send using php mail function.</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Avnish</td>
<td>alok</td>
</tr>
</table>
</body>
</html>
";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <your-gmail-id@gmail.com>' . "\r\n";

$chk=mail($to,$subject,$message,$headers);
if($chk)
echo "success";
else
echo "fail";
?>