How to send mail from localhost in PHP using XAMPP?

Sending email from localhost is very useful to quickly test or debug your web application. Setting up the mail function will need a few steps.

Send email using PHP mail()

Create php file

Create send_email_test.php file. Put the following code. It uses the PHP mail() function. By default, it will not work in localhost. To fix it, we need to use the Gmail SMTP server.

$send_to_email = "[email protected]";
$subject = "Verification Email";
$body = "Hi {$send_to_email}.<br /><br />";
$body .= "Test body of email";

$from_name = "YOURNAME";
$from_email = "[email protected]";

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: {$from_name} <{$from_email}> \n";

if (mail($send_to_email, $subject, $body, $headers)) {
    echo "Email sent.";
} else {
    echo "Unable to send email.";

Run in the browser

  1. Make sure XAMPP (Apache) is running.
  2. Open your browser, go to https://localhost/send_email_test.php
  3. At this point, it is not working yet. We need to follow the steps on the next section.

Use Gmail to send mail from localhost

Previously, we are using the PHPMailer library to send emails from localhost. It is not needed anymore. You can use a Gmail account to send an email from localhost.

Change php.ini

Open your php.ini file. In my case, it is found in the C:\xampp\php directory. Find the following settings and set the values.
sendmail_from = [email protected]
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

Change sendmail.ini

Open your sendmail.ini file. In my case, I found it in the C:\xampp\sendmail directory. Find the following settings and set the values. Change YOUROWNEMAIL and YOURGMAILPASSWORD to the account you want to use.
[email protected]

Switch on “Less secure apps”

  1. Login to GMail account you want to use.
  2. On the upper right, click your image > Manage your Google account.
  3. On the new tab, click Security > Scroll down to "Less secure app access".
  4. Switch on Less secure apps.

Restart XAMPP

  1. Using the XAMPP control panel, turn off Apache.
  2. Close the XAMPP control panel.
  3. Open XAMPP again to restart Apache.
  4. To test if it works, run send_email_test.php on the browser again.

What if it did not work?

You will need to see the Apache error logs and see the exact error message. Here's how:

  1. Open XAMPP control panel.
  2. On the Apache module, click "Logs" button.
  3. On the drop-down, click "Apache (error.log)"
  4. A notedpad will show up, it will show you the list of errors.

Hi! I'm Mike Dalisay, the co-founder of, a site that helps you build web applications with PHP and JavaScript. Need support? Comment below or contact [email protected]

I'm also passionate about technology and enjoy sharing my experience and learnings online. Connect with me on LinkedIn, Twitter, Facebook, and Instagram.