· Kalpa Madhushan · development · 3 min read

How to Send Emails from Localhost with XAMPP Using Gmail App Password

Learn how to configure PHP's mail function to send emails from localhost using Gmail's SMTP server and an App Password in XAMPP. This step-by-step guide ensures secure and reliable email testing during development.

Learn how to configure PHP's mail function to send emails from localhost using Gmail's SMTP server and an App Password in XAMPP. This step-by-step guide ensures secure and reliable email testing during development.

How to Send Emails from Localhost with XAMPP Using Gmail App Password

Sending emails directly from your local development environment can be a great way to test and debug email functionality without deploying your project to a live server. In this guide, we’ll walk you through how to configure the PHP mail() function to work with Gmail’s SMTP server using an App Password in XAMPP.

Why Use Gmail with XAMPP?

XAMPP doesn’t come with a built-in mail server, so PHP’s mail() function won’t work out of the box. By connecting PHP to Gmail’s SMTP server, we can send emails from localhost securely and reliably.

Steps to Configure PHP Mail with Gmail SMTP

1. Create a Gmail App Password

Before you start, ensure that 2-Step Verification is enabled for your Gmail account. Without this, you won’t be able to generate an App Password.

  • Go to your Google Account settings.
  • Navigate to Security > App passwords.
  • Choose an app (e.g., Mail) and device (e.g., Windows Computer), then click Generate.
  • A 16-character App Password will be displayed. Note it down—you’ll use it in your configuration.

2. Edit sendmail.ini

This file is typically located at C:\xampp\sendmail\sendmail.ini.

Update or add the following settings:

smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=tls
auth_username=your-email@gmail.com
auth_password=your-app-password
force_sender=your-email@gmail.com

Replace your-email@gmail.com with your actual Gmail address and your-app-password with the App Password you generated.

3. Edit php.ini

This file is usually found at C:\xampp\php\php.ini.

Find the [mail function] section and update it like so:

[mail function]
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = your-email@gmail.com
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"

Make sure the sendmail_path points to the correct location of sendmail.exe on your system.

4. Restart XAMPP

After saving your changes to php.ini and sendmail.ini, restart both Apache and Sendmail (if listed) via the XAMPP Control Panel. This ensures that all configuration changes take effect.

5. Test the Configuration

Create a simple PHP script to send a test email and verify that everything is working:

<?php
$to = 'recipient@example.com';
$subject = 'Test Email from Localhost';
$message = 'Hello! This is a test email sent from localhost using Gmail SMTP.';
$headers = "From: Your Name <your-email@gmail.com>\r\n";

if(mail($to, $subject, $message, $headers)){
  echo 'Email sent successfully!';
} else {
  echo 'Email sending failed.';
}
?>

To change the sender name, modify the From header as shown above. Replace Your Name with the desired sender name and your-email@gmail.com with your Gmail address. Similarly, update recipient@example.com with the recipient’s email address.

If you’ve followed all steps correctly, this script should send a test email from your local machine using Gmail’s SMTP server!


Now you can build and test full email workflows directly from your development environment using XAMPP and Gmail. Happy coding!

Back to Blog

Related Posts

View All Posts »