Gmail SMTP New Method 2023 – PHP

In this tutorial, I am going to show you, how you can use Gmail SMTP for sending emails in PHP.

Video tutorial – Gmail SMTP in PHP

I will be using PHPMailer library. You can find all the code for sending email from PHPMailer documentation.

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

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

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = 'your_email_address@gmail.com';                     //SMTP username
    $mail->Password   = 'app_password';                               //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
    $mail->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

    //Recipients
    $mail->setFrom('your_email_address@gmail.com', 'Your name');
    $mail->addAddress('support@adnan-tech.com', 'Adnan Tech');

    //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

Line #18: You need to set your host to smtp.gmail.com

Line #20: Write your email address

Line #21: Write the app password you will get from your google account in the next step.

First, you need to go to your google account.

1. Manage google account
Manage google account

Go to security.

2. Security
Security

Then go to 2-step verification.

3. 2-step verification
2-step verification

After that, go to app passwords.

4. App passwords
App passwords

Select the app as “mail” and device as “other”.

Select app password for Gmail SMTP - PHP
Select app password

Set whatever name you want and generate.

Write app password name for Gmail SMTP - PHP
Write app password name

This will generate an app password.

App password for Gmail SMTP - PHP
App password for Gmail SMTP – PHP

You can use this in your SMTP code. Line #21 of the above code.

Make sure there is no space in password. Then try again. You should receive an email now.

Learn how to do it in Node JS from here.

If you need any help in following this, kindly do let me know.