Easily send emails in PHP by configuring Gmail SMTP with PHPMailer for secure, fast delivery, ideal for contact forms, notifications, and automated email systems.
Key Points
Sending emails from a website is a key feature for many businesses, whether it's for contact forms, account verification, or transactional alerts. In this tutorial, we will walk through how to set up Gmail SMTP with PHPMailer in PHP, a secure and reliable method used by many custom web development projects.
This guide is ideal for developers providing website development services or working for a web development firm aiming to incorporate email functionality into client websites.
To start sending emails through Gmail's SMTP server, you need to update your Google account settings to allow this access.
Login to your Gmail account.
Go to Settings > See all settings > Forwarding and POP/IMAP.
Under IMAP Access, select Enable IMAP.
Save your changes.
Gmail requires 2-Step Verification for generating App Passwords. Enable it here:
https://myaccount.google.com/security
Since Google blocks access from less secure apps, you’ll need to use an App Password instead of your usual Gmail password.
Go to: https://myaccount.google.com/apppasswords
Under Select App, choose Mail.
Under Select Device, choose Other (Custom name) and type "PHPMailer".
Click Generate and copy the 16-character password provided.
Use this app password in your PHP script — not your actual Gmail login credentials.
To simplify the email sending process, we’ll use PHPMailer, a popular library employed by many web development companies.
Run the following command in your terminal:
This command will set up a vendor directory and install all dependencies required to send emails.
Now, let’s write a PHP script that uses Gmail SMTP to send an email.
Replace your-email@gmail.com and your-app-password with your own credentials. Replace the recipient address as needed.
You can run this file on any local development server like XAMPP, WAMP, or Laravel Valet. Alternatively, upload it to your live hosting environment.
How to test:
You should then receive the test email in your recipient inbox.
If the email isn’t sent, here are a few things to check:
$mail->SMTPDebug = 2; // Set to 3 for even more detail
Whether you’re a freelancer offering website development services or part of a full-scale web development company, having dependable email integration is crucial. Gmail SMTP guarantees:
For custom web development projects that include forms, order processing, or notifications, this setup is strongly recommended.
By following these steps, you can successfully set up Gmail SMTP to send emails in PHP using PHPMailer. This approach enhances security, boosts reliability, and suits both small and large applications. Looking to add features like email, login systems, or payment gateways to your site? Partner with a professional web development firm or seek advice from a web development expert.
©2025Digittrix Infotech Private Limited , All rights reserved.