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

  • 95% of PHP developers prefer PHPMailer for sending secure and customizable emails via SMTP servers.
  • Gmail SMTP achieves a 99% inbox delivery rate when properly set up with app passwords.
  • Over 1 million websites utilise PHPMailer for contact forms and transactional email services.

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.

Step 1: Enable Gmail SMTP Access

To start sending emails through Gmail's SMTP server, you need to update your Google account settings to allow this access.

Enable IMAP in Gmail

  1. Login to your Gmail account.

  2. Go to Settings > See all settings > Forwarding and POP/IMAP.

  3. Under IMAP Access, select Enable IMAP.

  4. Save your changes.

Turn On 2-Step Verification

Gmail requires 2-Step Verification for generating App Passwords. Enable it here:

https://myaccount.google.com/security

Generate an App Password

Since Google blocks access from less secure apps, you’ll need to use an App Password instead of your usual Gmail password.

  1. Go to: https://myaccount.google.com/apppasswords

  2. Under Select App, choose Mail.

  3. Under Select Device, choose Other (Custom name) and type "PHPMailer".

  4. Click Generate and copy the 16-character password provided.

Use this app password in your PHP script — not your actual Gmail login credentials.

Step 2: Install PHPMailer via Composer

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:

                                        composer require phpmailer/phpmailer
                                    

This command will set up a vendor directory and install all dependencies required to send emails.

Step 3: Create the PHP Script (send-mail.php)

Now, let’s write a PHP script that uses Gmail SMTP to send an email.

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

require 'vendor/autoload.php'; // Composer autoload

$mail = new PHPMailer(true);

try {
    // SMTP configuration
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'your-email@gmail.com';     // Replace with your Gmail address
    $mail->Password = 'your-app-password';        // Replace with your 16-character App Password
    $mail->SMTPSecure = 'tls';                    // Use 'ssl' if you prefer
    $mail->Port = 587;                            // Use 465 for 'ssl'

    // Email headers
    $mail->setFrom('your-email@gmail.com', 'Your Name');
    $mail->addAddress('recipient@example.com', 'Recipient Name');
    $mail->addReplyTo('your-email@gmail.com', 'Your Name');

    // Email content
    $mail->isHTML(true);
    $mail->Subject = 'Test Email from PHP';
    $mail->Body    = '<b>This is a test email sent using Gmail SMTP and PHPMailer.</b>';
    $mail->AltBody = 'This is the plain text version for email clients that don’t support HTML.';

    // Send the email
    $mail->send();
    echo 'Message has been sent.';
} catch (Exception $e) {
    echo 'Message could not be sent. Error: ', $mail->ErrorInfo;
}
                                    

Replace your-email@gmail.com and your-app-password with your own credentials. Replace the recipient address as needed.

Step 4: Run and Test Your Script

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:

  1. Open your browser.
  2. Navigate to http://localhost/send-mail.php
  3. If configured correctly, you will see the message:
    "Message has been sent."

You should then receive the test email in your recipient inbox.

Troubleshooting Tips

If the email isn’t sent, here are a few things to check:

  • SMTP Port and Encryption: Ensure port 587 (TLS) or 465 (SSL) is open on your server.
  • App Password Validity: Double-check the app password is correct.
  • Enable PHPMailer Debug: Add the line below before send() to see detailed logs:

$mail->SMTPDebug = 2; // Set to 3 for even more detail

Why This Setup Matters for Custom Web Development

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:

  • Better deliverability rates
  • Stronger security using app passwords
  • Easy setup with trusted libraries like PHPMailer

For custom web development projects that include forms, order processing, or notifications, this setup is strongly recommended.

Final Words

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.

Tech Stack & Version

Frontend

  • Tailwind CSS
  • React.js

Backend

  • Node.js
  • PHP
  • MongoDB

Deployment

  • Vercel
  • DigitalOcean
img

©2025Digittrix Infotech Private Limited , All rights reserved.