Sahil Kumar
4 months ago

Send Mail With Contact Form Using PHPMailer And Gmail SMTP

In this post, I'm going to show you how to send a mail with a contact form using Bootstrap 4, PHP, PHPMailer library and Gmail SMTP. Here I'll use Gmail as an outgoing SMTP server in the PHPMailer library and I'll use Bootstrap 4 for contact form design. So let's start this post step by step : 

Step 1:

In the first step, We'll create a project directory phpmailer inside localhost, I'm using XAMPP Server so I'll create inside htddocs.

Step 2:

In this step, We'll download the PHPMailer library in the project directory. So for this, We'll use Command Prompt, but before going to download the PHPMailer library first We've to download Composer, It's a dependency manager for PHP using this we can download the latest version of PHPMailer library. So if the composer is not installed on your system then just download from here and install it on your system. Once it is installed then we are ready to download the PHPMailer library.

Step 3:

Just open your project directory phpmailer and then press SHIFT and click the Right Mouse button now from the options menu click on Open Command Window Here. In the command line just write the below command and hit enter to download the PHPMailer library.

composer require phpmailer/phpmailer

Step 4:

Now open your project directory inside your favourite code editor and then create a new file index.php. Once you've downloaded and created the file then your project directory will look like this...

Send Mail Using PHPMailer PHP

Step 5:

Now in this step just open your index.php and writes the below codes :

  use PHPMailerPHPMailerPHPMailer;
  use PHPMailerPHPMailerException;

  require 'vendor/phpmailer/phpmailer/src/Exception.php';
  require 'vendor/phpmailer/phpmailer/src/PHPMailer.php';
  require 'vendor/phpmailer/phpmailer/src/SMTP.php';

  // Include autoload.php file
  require 'vendor/autoload.php';
  // Create object of PHPMailer class
  $mail = new PHPMailer(true);

  $output = '';

  if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

    try {
      $mail->Host = '';
      $mail->SMTPAuth = true;
      // Gmail ID which you want to use as SMTP server
      $mail->Username = '';
      // Gmail Password
      $mail->Password = 'Your Gmail Password';
      $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
      $mail->Port = 587;

      // Email ID from which you want to send the email
      // Recipient Email ID where you want to receive emails

      $mail->Subject = 'Form Submission';
      $mail->Body = "<h3>Name : $name <br>Email : $email <br>Message : $message</h3>";

      $output = '<div class="alert alert-success">
                  <h5>Thankyou! for contacting us, We'll get back to you soon!</h5>
    } catch (Exception $e) {
      $output = '<div class="alert alert-danger">
                  <h5>' . $e->getMessage() . '</h5>

<!DOCTYPE html>
<html lang="en">

  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Contact Us Using PHPMailer & Gmail SMTP</title>
  <link rel='stylesheet' href='' />

<body class="bg-info">
  <div class="container">
    <div class="row justify-content-center">
      <div class="col-lg-6 mt-3">
        <div class="card border-danger shadow">
          <div class="card-header bg-danger text-light">
            <h3 class="card-title">Contact Us</h3>
          <div class="card-body px-4">
            <form action="#" method="POST">
              <div class="form-group">
                <?= $output; ?>
              <div class="form-group">
                <label for="name">Name</label>
                <input type="text" name="name" id="name" class="form-control" placeholder="Enter Name" required>
              <div class="form-group">
                <label for="email">E-Mail</label>
                <input type="email" name="email" id="email" class="form-control" placeholder="Enter E-Mail" required>
              <div class="form-group">
                <label for="subject">Subject</label>
                <input type="text" name="subject" id="subject" class="form-control" placeholder="Enter Subject"
              <div class="form-group">
                <label for="message">Message</label>
                <textarea name="message" id="message" rows="5" class="form-control" placeholder="Write Your Message"
              <div class="form-group">
                <input type="submit" name="submit" value="Send" class="btn btn-danger btn-block" id="sendBtn">


Step 6:

Now you have to make some changes to the above codes...

$mail->Username = '';

In the Username field, you have to enter your Gmail ID which you want to use as an SMTP server

$mail->Password = 'Your Gmail Password';

In the Password field, you have to enter your Gmail ID password


In the setFrom field, you have to enter the same Gmail ID which you have entered in the Username field.


Now in the addAddress field, you have to enter another email id in which you want to receive form submission mails.

Step 7:

Everything is done in the coding part, now We've to change a setting in Gmail Account which We are using as the SMTP server. This is the most important step of all, without doing this We can't send email using the PHPMailer library. So let's change that setting :  

Just open the Google Home page and Sign In with your Gmail ID which you are using as an SMTP server and then click profile picture on the top right corner of the page, then click on Manage your Google Account

Send Mail With Contact Form Using PHPMailer And Gmail SMTP

Now click on the Security tab and make sure your 2-Step Verification is disabled like this...

Send Mail With Contact Form Using PHPMailer And Gmail SMTP

Now scroll down and go to Less secure app access and click on Turn on access Now next you have to enable this Allow less secure apps

Send Mail With Contact Form Using PHPMailer And Gmail SMTP

Now everything is ready just open this project in your localhost with an Internet connection and test.

If you want to run the same project in your live server then you just need a hosting with Apache Server and PHP Installed on the server. Just host all the files in your hosting and try. If it not work then just remove this line and then try.


If you want to do more things like send attachment with mail then you can check PHPMailer official Github Repo

If you are still facing any issue then you can comment down your issues I'll try to solve your problem. 

Post Author

Sahil Kumar
Sahil Kumar
Full Stack Web Developer

Hello! I'm a part-time blogger & Youtuber living in India. This is my personal blog where I write Web Design & Development tutorial posts!


© 2019 - 2021 | All rights reserved