
Author
Sahil Kumar
Publihsed On
May 31, 2024
Updated On
Aug 18, 2024
Category
PHP
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 the PHPMailer library. So, if the composer is not installed on your system, then just download it 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, your project directory will look like this…

Step 5:
Now in this step just open your index.php and writes the below codes:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
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->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
// Gmail ID which you want to use as SMTP server
$mail->Username = '[email protected]';
// 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
$mail->setFrom('[email protected]');
// Recipient Email ID where you want to receive emails
$mail->addAddress('[email protected]');
$mail->isHTML(true);
$mail->Subject = 'Form Submission';
$mail->Body = "<h3>Name : $name <br>Email : $email <br>Message : $message</h3>";
$mail->send();
$output = '<div class="alert alert-success">
<h5>Thankyou! for contacting us, We'll get back to you soon!</h5>
</div>';
} catch (Exception $e) {
$output = '<div class="alert alert-danger">
<h5>' . $e->getMessage() . '</h5>
</div>';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<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='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css' />
</head>
<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>
<div class="card-body px-4">
<form action="#" method="POST">
<div class="form-group">
<?= $output; ?>
</div>
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" id="name" class="form-control" placeholder="Enter Name" required>
</div>
<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>
<div class="form-group">
<label for="subject">Subject</label>
<input type="text" name="subject" id="subject" class="form-control" placeholder="Enter Subject"
required>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea name="message" id="message" rows="5" class="form-control" placeholder="Write Your Message"
required></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" value="Send" class="btn btn-danger btn-block" id="sendBtn">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Step 6:
Now you have to make some changes to the above codes...
$mail->Username = '[email protected]';
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
$mail->setFrom('[email protected]');
In the setFrom field, you have to enter the same Gmail ID which you have entered in the Username field.
$mail->addAddress('[email protected]');
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

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

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

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 does not work, then just remove this line and then try.
$mail->isSMTP();
If you want to do more things like send attachments with mail, then you can check PHPMailer's official GitHub Repo
If you are still facing any issues, then you can comment down your issues I'll try to solve your problem.

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!
Know more about me
SUBMIT GUEST POST 🚀
Ready to contribute? Head to the article dashboard to publish your own insightful articles and share them with our audience!
Go to dashboardRECENT POSTS

How Fintech Startups Can Leverage Custom Software Development for Success
6 months ago

Laravel Filament and Livewire
8 months ago

Building a Secure Authentication System with PHP OOP, PDO, MySQL, and Bootstrap 5
1 year ago

10 Essential Web Design Principles for Creating a User-Friendly Website
1 year ago

Send Mail with Contact Form Using PHPMailer & Gmail SMTP | 2023
1 year ago