Send email from localhost running XAMMP in PHP using GMAIL mail server

Send email from localhost running XAMMP in PHP using GMAIL mail server

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

Stefan Bogdanescu

Founder & Senior Architect

Send email from localhost running XAMPP in PHP using GMAIL mail server

The mail() function in PHP is notoriously deceptive. It often returns true even when the message never leaves your server, a behavior that stems from how the underlying operating system handles the delivery queue rather than actual SMTP communication. In many local development environments like XAMPP, the default configuration relies on a local sendmail.exe binary. When you attempt to bypass this by configuring php.ini with Gmail's SMTP settings, you are fighting against a legacy architecture that simply cannot bridge the gap without specific adjustments.

The most immediate hurdle visible in your logs is the error: Must issue a STARTTLS command first. This indicates a handshake failure between the PHP process and Google's mail server. Your current php.ini configuration sets SMTP = smtp.gmail.com and smtp_port = 587, which are correct for Gmail. However, the critical piece missing is the authentication method required to secure that connection. Modern SMTP servers, including Gmail's at port 587, require the client to upgrade a plaintext connection to TLS immediately after the handshake. If your local sendmail wrapper attempts to authenticate before establishing this encrypted tunnel, or if it fails to pass the STARTTLS command correctly, Gmail rejects the request.

Furthermore, there is a fundamental architectural conflict in your php.ini setup. You have enabled SMTP and smtp_port, but you also retain sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t". When PHP's mail() function is invoked, it checks for the existence of a valid SMTP host. If one is found, it attempts to connect directly. However, if the underlying system still tries to invoke the local sendmail binary defined in sendmail_path, that binary often lacks the logic to handle SMTP authentication or TLS negotiation correctly unless explicitly configured via sendmail.ini.

To resolve this on XAMPP, you must ensure the PHP process communicates directly with Gmail without relying on the intermediate sendmail.exe wrapper for authentication. The most robust approach involves modifying the php.ini file located in your XAMPP installation directory (typically C:\xampp\php\php.ini).

You should set the following parameters explicitly:

SMTP = smtp.gmail.com
smtp_port = 587
sendmail_from = myemail@gmail.com
; Ensure sendmail_path is either commented out or left default if using direct SMTP
; If you must use a wrapper, ensure it supports TLS and Auth.

Crucially, the error log message suggests that the STARTTLS command was not issued first. This often happens when the connection protocol is mismatched. Gmail requires TLS for port 587. If your local environment forces SSL (port 465) logic onto port 587, or vice versa, the handshake fails. Ensure you are using TLS and not SSL on port 587.

If you continue to rely on the sendmail.ini file for authentication, verify that the force_sender matches your authenticated username exactly. Additionally, Gmail has strict application-authorization policies. If you are using an app-specific password (common when 2FA is enabled), ensure that auth_password in your configuration reflects that token, not your primary account password.

For production environments or more complex routing needs, consider switching to a dedicated mailgun or SendGrid service, or utilize the phpmailer library which handles SMTP connections and TLS handshakes with significantly less ambiguity than the core mail() function. While XAMPP is designed for rapid development, its default email stack often obscures these underlying connection details until they fail spectacularly, as seen in your "successfully sent" but undelivered scenario.

If you are building a larger application where email reliability is critical, frameworks like Laravel offer built-in mail drivers that abstract this complexity away entirely. You can read more about configuring reliable email transport in modern PHP stacks at https://laravelcompany.com.

Our network