Skip to content
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Sending Emails in PHP Using PHPMailer library

Sending Emails in PHP Using PHPMailer library

Posted on June 23, 2019April 10, 2020 By XpertPhp No Comments on 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.

PHP
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.

PHP
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.

PHP
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->From = '[email protected]';   // 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.

Show Demo

Please follow and like us:
error
fb-share-icon
Tweet
fb-share-icon

Recommended Posts:

  • how to generate random alphanumeric string in php
  • disable browser back button using jquery
  • how to check curl is working or not in php
  • Laravel 7 ajax pagination example using jquery
  • How to Handle JSON data in PHP
Ajax, Jquery, Php Tags:form validation, gmail smtp, gmail smtp server, google send email, google smtp server, jQuery Ajax Form Handling, php mailer smtp, php send email, php with mysql, phpMailer, phpmailer smtp, send mail, send mail using smtp in php

Post navigation

Previous Post: How To Use Paypal Payment Gateway In Laravel Framework
Next Post: Login with Facebook using PHP

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tricks jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch Laravel Socialite laravel social login nodejs pagination payment gateway php with mysql react js tutorial rewrite rule send mail validation wysiwyg editor

Latest Posts

  • How to Convert Date and Time from one timezone to another in php
  • how to get current date and time in php
  • Drag and Drop Reorder Items with jQuery, PHP & MySQL
  • Laravel 9 Toastr Notifications Example Tutorial
  • Laravel 9 CRUD Operation Example Using Google Firebase
  • Laravel 9 CKeditor Image Upload With Example
  • Laravel 9 Summernote Image Upload With Example
  • Laravel 9 Stripe Payment Gateway Integrate Example
  • How To Send Email Using Mailtrap In Laravel 9
  • Laravel 9 Fullcalendar Ajax Example Tutorial

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Age Calculator Online
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Copyright © 2018 - 2022,

All Rights Reserved Powered by XpertPhp.com