Send an Email via Gmail SMTP Server in PHP using PHPMailer

In this tutorial, we going to see how to send an email via Gmail SMTP server in PHP using PHPMailer. We send an email with different mail transfer libraries using PHP. In this, We are going to use PHPMailer. These libraries give excellent features like SMTP authentication and more.

Send an Email via Gmail SMTP Server in PHP using PHPMailer


Using Gmail SMTP server sending emails by using PHPMailer. PHPMailer library is famous because of its excellent features are available. Some of those features are below,

1. PHPMailer supports both HTML content and plain text.
2. We can able to use an array of email addresses like to, cc, bcc, reply-to.
3. It also gives Secure/MIME encryption with email validation, SMTP authentication including multiple language support.

Before using Gmail SMTP.

To send an email you can use the mail server of another host but for this, you need first to have authentication. E.g. You need to send an email from Gmail mail server first you require Gmail account.

Gmail SMTP needs the TLS encryption so we set it accordingly. If you want to send mail via SMTP, you want to find the Hostname, Port number, SMTPSecure encryption type if authentication is needed you also want the username and password.

Now need to do some changes in your Gmail account settings. First login and open your Gmail account settings.

1. We want to off the 2-Step Verification before using it.

2-Step Verification gmail smtp

2. Next, go to less secure apps. The default Gmail less secure apps are in off. So here, you need to keep Allow less secure apps: ON.

less secure apps google

Required files are needed:

Now we going to use a PHPMailer library which you can download on GitHub.

In this, we going to send an mail using TextBox and TextArea without saving data in the database. Below you can see the full code.


<!doctype html>
    <meta charset="UTF-8">
    <title>SoftAOX - Send an Email via Gmail SMTP Server in PHP using PHPMailer</title>
    <link href="style.css" rel="stylesheet">
    <link href="" rel="stylesheet" id="bootstrap-css">
    <script src=""></script>
    <div class="inner contact">
        <!-- Form Area -->
        <div class="contact-form">
            <h1 align="center">SoftAOX</h1>
            <h2 align="center">Send an Email via Gmail SMTP Server in PHP using PHPMailer</h2>
            <!-- Form -->
            <form action="mail.php" method="post">
                <!-- Left Inputs -->
                <div class="col-xs-6">

                    <h3>Enter Your Email ID and Password</h3>
                    <p>Before using your Gmail ID and Password check whether your <a href="" target="_blank" rel="noopener noreferrer">Gmail less secure apps</a> is <b>on</b> or <b>not</b>.</p>
                    <input type="text" name="email" required class="form" placeholder="Enter your email ID" />

                    <input type="password" name="password" required class="form" placeholder="Password" />

                <!-- End Left Inputs -->

                <!-- Right Inputs -->
                <div class="col-xs-6">

                    <h3>To Address</h3>

                    <input type="email" name="toid" required class="form" placeholder="To : Email Id " />

                    <input type="text" name="subject" required class="form" placeholder="Subject" />

                    <textarea name="message" class="form textarea" placeholder="Message"></textarea>
                <!-- End Right Inputs -->

                <!--  Submit -->
                    <button type="submit" id="submit" name="send" class="form-btn semibold">Send Message</button>
                <!-- End Submit -->
                <!-- Clear -->
                <div class="clear"></div>

        <!-- End Contact Form Area -->

require_once 'PHPMailer/PHPMailerAutoload.php';

// Fetching data that is entered by the user
$email = $_POST['email'];
$password = $_POST['password'];
$to_id = $_POST['toid'];
$message = $_POST['message'];
$subject = $_POST['subject'];

// Configuring SMTP server settings
$mail = new PHPMailer;
$mail->Host = '';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $email;
$mail->Password = $password;
$mail->FromName = "SoftAOX - PHP Mailer";

// Email Sending Details
$mail->Subject = $subject;

// Success or Failure
if (!$mail->send()) {
$error = "Mailer Error: " . $mail->ErrorInfo;
echo '<p>'.$error.'</p>';
else {
echo '<p>Message sent!</p>';
echo '<p>Please enter valid data</p>';

The below code is used to require the PHPMailerAutoload file. This PHPMailerAutoload file is available in PHPMailer library.

require_once 'mailerClass/PHPMailerAutoload.php';

     padding:10px 0 10px;
     width: 100%;
     padding: 15px;
     border:1px solid rgba(0, 0, 0, 0.075);
     color:#727272 !important;
     -webkit-transition: all 0.4s;
     -moz-transition: all 0.4s;
     transition: all 0.4s;
     border:1px solid #8BC3A3;
     color: white;
     outline: none;
     border:1px solid #8BC3A3;
     height: 120px;
     max-height: 120px;
     max-width: 100%;
     padding:8px 12px;
     display: block;
     margin:10px 0 0px 0;
     -webkit-transition: all 0.3s;
     -moz-transition: all 0.3s;
     transition: all 0.3s;
     box-shadow: 0px 1px 4px rgba(0,0,0, 0.10);
     -moz-box-shadow: 0px 1px 4px rgba(0,0,0, 0.10);
     -webkit-box-shadow: 0px 1px 4px rgba(0,0,0, 0.10);
     display: block;
     height: auto;
     outline: none;
     -webkit-transition: all 0.3s;
     -moz-transition: all 0.3s;
     transition: all 0.3s;
     box-shadow: 0px 1px 4px rgba(0,0,0, 0.10);
     -moz-box-shadow: 0px 1px 4px rgba(0,0,0, 0.10);
     -webkit-box-shadow: 0px 1px 4px rgba(0,0,0, 0.10);
     color: white;
     opacity: 0.9;


I believe in this tutorial you feeling happy with sending an email via Gmail SMTP Server in PHP using PHPMailer. Using other SMTP servers also you can try to send mail.

Was this article helpful?

Yes No

How can we improve it?


We appreciate your helpul feedback!

Your answer will be used to improve our content. The more feedback you give us, the better our pages can be.

Follow us on social media:

Facebook Pinterest
Share via:


Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts.

Leave a Reply