Sending Emails in PHP Using PHPMailer library
In this tutorial, we will tell you how to send email in PHP using the PhpMailer. 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.
1 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | <!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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | <?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->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.