How to setup mail in XAMPP locally?

How to setup mail in XAMPP locally?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why XAMPP SMTP Settings Often Break Local Email Sending

Configuring email functionality in a local development environment like XAMPP is a common stumbling block for PHP learners. The issue usually stems from a fundamental misunderstanding of how the mail() function operates under the hood versus what you need for actual delivery. When you inspect your php.ini file and see settings like SMTP = localhost and smtp_port = 25, it looks intuitive, but relying on these defaults in XAMPP often results in silent failures or connection timeouts.

The core problem lies in the distinction between a simple mail transfer agent (MTA) and a dedicated SMTP server. The PHP mail() function, when configured with SMTP = localhost, attempts to hand off the message to an SMTP daemon running on your machine. However, XAMPP's Apache distribution does not ship with an MTA like Postfix or Exim by default. It also typically does not run an SMTP listener on port 25 unless you explicitly install and configure something like vswsmtp or a similar tool. Without this daemon listening on port 25, PHP throws a fatal error stating "Could not connect to the mail server," or worse, it silently fails depending on your error reporting settings.

To get email working locally without installing complex server software, you should modify your php.ini configuration file located in XAMPP's installation directory (usually C:\xampp\php\php.ini). The most reliable approach is to switch from the built-in SMTP protocol to using a local socket or a dedicated mail handler.

First, locate the [mail function] section in your php.ini. You will see lines like:

SMTP = localhost
smtp_port = 25
sendmail_path = 

If you want PHP to attempt a connection via SMTP on port 25, you must ensure an actual SMTP service is running. If you do not want to install external services, the better strategy is to tell PHP to use the system's default mailer, which on Windows often points to sendmail.exe or utilizes a specific handler. However, since XAMPP runs on Windows, the standard sendmail_path line is usually commented out or irrelevant unless you have installed a specific MTA.

A more robust solution for local development that avoids port 25 entirely is to use a library like PHPMailer or SwiftMailer. These libraries abstract away the low-level mail transfer details and allow you to specify an external SMTP server (like your company's mail server or a service like Gmail/Outlook) directly from your code. This bypasses the need for localhost to act as an MTA.

To implement this, install Composer packages rather than relying on XAMPP's bundled libraries:

composer require phpmailer/phpmailer

Once installed, you can update your PHP script to use a live SMTP server configuration instead of trying to configure localhost:

require 'vendor/autoload.php';

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

$mail = new PHPMailer(true);
try {
    $mail->isSMTP();
    $mail->Host       = 'your-smtp-server.com'; // e.g., smtp.gmail.com or your corporate server
    $mail->SMTPAuth   = true;
    $mail->Username   = 'user@domain.com';
    $mail->Password   = 'your-password';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port       = 587;

    $mail->setFrom('from@example.com', 'Sender Name');
    $mail->addAddress('recipient@example.com', 'Recipient Name');

    $mail->Subject = 'Test Email';
    $mail->Body    = 'This is a test email from local XAMPP.';

    $mail->send();
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

If you strictly require using the mail() function without Composer, you must install a local SMTP daemon. On Windows, tools like vswsmtp or configuring Postfix with a specific Windows backend are required to listen on port 25. Without this step, changing SMTP in php.ini to localhost is effectively useless because no process is accepting connections there.

For developers moving from local XAMPP setups to production, it is critical to note that these configurations rarely work out-of-the-box on live servers. Production environments typically run Linux with services like Postfix or Sendmail installed via package managers. If you are working on a project that might eventually go to production, consider adopting a framework like Laravel early on. Laravel includes built-in mail configuration that seamlessly handles switching between local testing drivers (like log or array) and real SMTP providers (like Mailgun or Amazon SES) through .env files. This decouples your development workflow from the specific quirks of XAMPP's default mail handling.

Ultimately, the decision to stick with XAMPP's native mail() function or switch to an SMTP library depends on your immediate needs. If you need to send emails locally without installing additional daemons, using an external SMTP relay via a library like PHPMailer is the cleanest path forward.

Our network