In this tutorial, we will tell you how to send email in PHP using the PhpMailer(Sending Emails in PHP Using PHPMailer library). PHP provides the mail() function to send email but sometimes that not working. so we will use the PhpMailer library.
basically phpmailer provide an object-oriented interface so we can easily send mail using that library. so we will Download composer and install the composer. follow below URL to you can download composer https://getcomposer.org/
After the installation composer, open the cmd and you can download PhpMailer library using the below command.
composer require phpmailer/phpmailer
Now, we will create just contact form using the bootstrap and pass the data through the ajax in the below example.
<!DOCTYPE html> <html lang="en"> <head> <title>Sending Emails in PHP Using PHPMailer library - XpertPhp</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script> <script> $(document).ready(function(){ $("#frmContact").validate({ rules: { name: { required:true }, email: { required:true, email:true }, subject: { required:true }, message: { required:true } }, messages: { }, submitHandler: function(form) { var fomr = $('form')[0]; var formData = new FormData(fomr); $.ajax({ type: 'POST', url: "contact_sendmail.php", data:formData, cache:false, contentType: false, processData: false, success: function(data) { if(data=='Message Send Successfully') { alert(data); } else { alert(data); } } }); return false; } }); }); </script> </head> <body> <div class="container"> <h2>Contact form</h2> <form id="frmContact" name="frmContact"> <div class="form-group"> <label for="name">Name:</label> <input type="text" class="form-control" placeholder="Enter Name" name="name" id="name"> </div> <div class="form-group"> <label for="email">Email:</label> <input type="email" name="email" placeholder="Enter Email" id="email" class="form-control"> </div> <div class="form-group"> <label for="subject">Subject:</label> <input type="text" name="subject" id="subject" placeholder="Enter Subject" class="form-control"> </div> <div class="form-group"> <label for="message">Message:</label> <input type="text" name="message" id="message" placeholder="Enter Message" class="form-control"> </div> <button type="submit" name="btnContact" class="btn btn-default">Send</button> </form> </div> </body> </html>
We will pass data when sending the mail which we the get data through ajax. we have to also the configuration of the setting for the send mail using PhpMailer. we have configurated in below example for the send mail so you can see them.
<?php $to = $_POST['email']; $name = $_POST['name']; $subject = $_POST['subject']; $message = $_POST['message']; if(!empty($to) && !empty($name) && !empty($subject) && !empty($message)) { require_once "vendor/autoload.php"; $mail = new PHPMailer; $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = ''; // Specify main and backup SMTP servers $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = ''; // SMTP username $mail->Password = ''; // SMTP password $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted $mail->Port = 25; // TCP port to connect to $mail->From = 'xpertphp2018@gmail.com'; // Add a recipient email $mail->FromName = 'XpertPhp'; // Add a recipient Name $mail->addAddress($to); // Add a Sender email $mail->isHTML(true); // Set email format to HTML $mail->Subject = $subject; $emailData = ''; $emailData.='<table width="100%" border="0" style="border-spacing:0px; color:#0e1114;">'; $emailData.='<tr style="background:#1eaace;">'; $emailData.='<td style="padding:20px 30px;">'; $emailData.='<table width="100%" border="0" class="header" style="border-spacing:0px;">'; $emailData.='<tr>'; $emailData.='<td style="color:#fff;font-size:22px;font-weight:bold;">XpertPhp</td>'; $emailData.='<td><h2 style="color:#ffffff; font-size:22px; line-height:36px; margin:0px; text-align:right; text-transform:uppercase; font-weight:bold;">Contact</h2></td>'; $emailData.='</tr>'; $emailData.='</table>'; $emailData.='</td>'; $emailData.='</tr>'; $emailData.='<tr style="background:#f7f7f7;">'; $emailData.='<td style="padding:30px 25px;">'; $emailData.='<table width="100%" border="0" style="border-spacing:0px;">'; $emailData.='<tr>'; $emailData.='<td style="padding:10px 20px;width:150px;"><h6 style="color:#0e1114; font-size:15px; line-height:28px; margin:0px; text-align:left; text-transform:uppercase; font-weight:bold;";>Your Name:</h6></td>'; $emailData.='<td style="text-align:left; font-size:14px;">'.$_POST["name"].'</td>'; $emailData.='</tr>'; $emailData.='<tr>'; $emailData.='<td style="padding:10px 20px;width:150px;"><h6 style="color:#0e1114; font-size:15px; line-height:28px; margin:0px; text-align:left; text-transform:uppercase; font-weight:bold;";>Your Email:</h6></td>'; $emailData.='<td style="text-align:left; font-size:14px;">'.$_POST["email"].'</td>'; $emailData.='</tr>'; $emailData.='<tr>'; $emailData.='<td style="padding:10px 20px;width:150px;"><h6 style="color:#0e1114; font-size:15px; line-height:28px; margin:0px; text-align:left; text-transform:uppercase; font-weight:bold;";>Your Subject:</h6></td>'; $emailData.='<td style="text-align:left; font-size:14px;">'.$_POST["subject"].'</td>'; $emailData.='</tr>'; $emailData.='<tr>'; $emailData.='<td style="padding:10px 20px;width:150px;"><h6 style="color:#0e1114; font-size:15px; line-height:28px; margin:0px; text-align:left; text-transform:uppercase; font-weight:bold;";>Your Message:</h6></td>'; $emailData.='<td style="text-align:left; font-size:14px;">'.$_POST["message"].'</td>'; $emailData.='</tr>'; $emailData.='</table>'; $emailData.='</td>'; $emailData.='</tr>'; $emailData.='</table>'; $mail->Body = $emailData; if(!$mail->send()) { echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; } } else { echo 'Message could not be sent.'; } ?>
I think would you like this article, so you can click on “Show Demo” button and you can see this demo article.