- 6 years ago
- Zaid Bin Khalid
- 8,403 Views
-
7
In this tutorial, I am going to show you how you can send an email with PHP mail function. In PHP you can send an email with HTML body for that you need to use PHP mail() function and needs to set some headers syntax given below. This function support (PHP 4, PHP 5, PHP 7).
mail ( string $to , string $subject , string $message [, mixed $additional_headers [, string $additional_parameters ]] ) : bool
In PHP mail() function you need to set 4 parameters.
- TO, Receiver, or receivers of the mail.
- SUBJECT, You need to set email subject.
- BODY, Message to be sent.
- HEADER, String or array to be inserted at the end of the email header.
This function will return True or False. Remember mail function will not work in Local server.
$body = '<table bgcolor="#FFFFFF" width="560" style="border:1px solid #ccc; opacity:0.8; font-family:Arial; font-size:14px; line-height:18px;" cellspacing="0" cellpadding="5" border="0" align="center">
<tbody>
<tr>
<td valign="top" align="left">
<p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<br>You can contact us any time by</p>
</td>
</tr>
<tr>
<td align="center"><a href="" style=" background: #007bff; color: #fff; padding: 10px; border-radius: 5px; border: 4px solid #0167d4;">Change Password</a></td>
</tr>
<tr>
<td> </td>
</tr>
</tbody>
</table>';
$name = 'Learn Code Web';
$email = '[email protected]';
$to = $useremail;
$header = "From: \"".$name."\" <".$email."> \n";
$header .= "To: \"".$name."\" <".$to."> \n";
$header .= "Return-Path: <".$email."> \n";
$header .= "MIME-Version: 1.0 \n";
$header .= "Content-Type: text/HTML; charset=ISO-8859-1 \n";
$subject = 'FORGET PASSWORD';
$mail = mail($to,$subject,$body,$header);
if(!$mail) {
echo '<div class="alert alert-danger p-1 mt-1"><i class="fa fa-fw fa-exclamation-triangle"></i> Email not send!</div>|***|0';
exit;
}else{
echo '<div class="alert alert-success p-1 mt-1"><i class="fa fa-fw fa-thumbs-up"></i> Cheack you email!</div>|***|1|***|index.php';
exit;
}
In the above snippet, you can see the HTML that we use to send a beautiful email with content. You can change the HTML body as per your need. And one thing more in the above snippet we use Bootstrap 4 not in email body but in mail function response.
If you use an array, in headers[] then you can use below snippets to send an email.
// To send HTML mail, the Content-type header must be set
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
// Additional headers
$headers[] = 'To: LearnCodeWeb <[email protected]>';
$headers[] = 'From: LearnCodeWeb <[email protected]>';
$headers[] = 'Cc: [email protected]';
$headers[] = 'Bcc: [email protected]';
After setup your headers you need to call a PHP mail() function syntax is given below.
// Mail it
mail($to, $subject, $message, implode("\r\n", $headers));
- 6 years ago
- Zaid Bin Khalid
- 8,403 Views
-
7