
Fix Gmail SMTP Issues in PHP by Using App Password (Complete Tutorial)
Sending emails from a website using Gmail SMTP is a common and reliable practice. However, many developers and users often encounter issues when trying to authenticate using their regular Gmail password — which doesn’t work in most cases due to Google’s security policies.
In this guide, we’ll show you how to properly configure Gmail SMTP using App Passwords instead of your actual Gmail password. We’ll also walk you through the SMTP settings and how to send emails using PHP.
Common Mistake Most Users Make with Gmail SMTP
One of the most frequent issues people face while setting up Gmail SMTP is using their original Gmail password. Due to Google’s enhanced security, standard Gmail accounts with 2-Step Verification enabled will not work with traditional password-based logins on third-party apps like your website.
The correct way is to use an App Password, which is a unique 16-character code generated by Google for connecting apps securely.
What Is a Google App Password?
An App Password is a 16-digit passcode that gives a less secure app or device permission to access your Google Account. It’s used in place of your regular password when setting up email clients, SMTP, or other apps that need to communicate with Gmail.
Step-by-Step: How to Generate a Gmail App Password for SMTP
Follow these steps to generate your Gmail App Password:
Step 1: Visit App Passwords Page
(google login screen when we visit google app password screen)
Go to: https://myaccount.google.com/apppasswords

Step 2: Access App Password Section
Once logged in, you will be redirected to the App Password screen.
Step 3: Create a New App Password

For example Select App Name (like “MyDemoSMTP”) and press create button and you will a see a popup box with password details.
Google will generate a 16-character password in the format: abcd efgh ijkl mnop

note: mentioned password is just for sample. you will get different values everytime
Step 4: Format the Password ( Remove space between the password values)
Note: Remove the spaces to get: abcdefghijklmnop ( this is sampel value not original password)
Gmail SMTP Configuration Settings
Now that you have your App Password, you can set up your SMTP like this:
Parameter | Value |
SMTP Host | smtp.gmail.com |
SMTP Port | 587 (TLS) or 465 (SSL) |
SMTP User | yourgmailid@gmail.com |
SMTP Password | abcdefghijklmnop (App Password) |
Encryption | TLS or SSL |
Send Email Using PHP and Gmail SMTP
Here’s a simple example using PHPMailer to send an email via Gmail SMTP:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'yourgmailid@gmail.com';
$mail->Password = 'yourapppassword';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('yourgmailid@gmail.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->isHTML(true);
$mail->Subject = 'Test Email via Gmail SMTP';
$mail->Body = 'This is a test email sent using Gmail SMTP and PHPMailer.';
$mail->send();
echo 'Message has been sent!';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
Important Security Tips for Gmail SMTP Setup
Make sure to keep these essential points in mind when using Gmail SMTP in your PHP projects:
Never use your regular Gmail password for SMTP.
Google blocks SMTP access using your normal Gmail password due to security reasons. Your app will fail to send mail unless you’re using the correct method.Always generate and use a Gmail App Password.
App Passwords are special 16-digit codes generated by Google that allow you to send mail securely via Gmail SMTP.Make sure 2-Step Verification is enabled.
Google requires 2-Step Verification to be active in your account before you can generate an App Password. This is a security layer to confirm your identity.Use secure and well-maintained libraries like PHPMailer or SwiftMailer.
PHPMailer is a free, open-source PHP library that simplifies sending SMTP emails. It handles authentication, encryption, and formatting.
SMTP errors and authentication failures are common issues developers face when trying to send emails using Gmail in PHP — especially when unknowingly using the wrong credentials or setup method. Fortunately, by following the step-by-step guide above and using Gmail App Passwords, you can overcome these challenges and send emails securely and reliably from your application.
However, if you’re still facing technical issues or need help implementing advanced email features, you can always connect with an expert PHP developer to assist you. Whether it’s integrating SMTP, building secure backend systems, or customizing email workflows, professional help can save you time and ensure everything works smoothly.