Mailer Error: SMTP connect() failed in php mailer( https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting)

Mailer Error: SMTP connect() failed in php mailer( https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting)

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

Stefan Bogdanescu

Founder & Senior Architect

Resolving PHPMailer's "SMTP connect() failed" Error on localhost

When running a local development server, such as XAMPP, you may encounter the error Mailer Error: SMTP connect() failed when attempting to send emails via PHPMailer. This issue typically stems from a mismatch between how your application attempts to communicate with an external mail server and the security protocols enforced by that server.

In the scenario provided, the developer configures PHPMailer to use Gmail's SMTP service (ssl://smtp.gmail.com) with SSL encryption on port 465. However, the error persists even after modifying php.ini files to include or remove the extension=php_openssl.dll line. This indicates that the root cause is not a missing OpenSSL extension in the default configuration but rather a fundamental incompatibility in how the application context (localhost) handles SSL connections versus how Gmail expects them.

The Root Cause: TLS vs. SSL and Port Configuration

The error occurs because modern SMTP servers, including Gmail's smtp.gmail.com, require Secure Sockets Layer (SSL) or Transport Layer Security (TLS) encryption. When using port 465, the connection must be wrapped in an SSL handshake immediately upon connection. The code snippet sets $mailer->SMTPSecure = 'ssl' and $mail->Port = 465, which is correct for Gmail's legacy SSL port.

However, many local environments struggle to establish this specific type of encrypted connection. The issue often lies in the fact that localhost or 127.0.0.1 connections can sometimes bypass strict SSL verification if not explicitly forced, or the underlying PHP build might lack the necessary cryptographic libraries required to negotiate an SSL handshake with port 465.

Simply uncommenting or commenting out extension=php_openssl.dll in c/xampp/apache/bin/php.ini is often insufficient if the server restarts using a different configuration file (e.g., c/xampp/php/php.ini) or if the OpenSSL extension is not loaded correctly in the specific Apache instance serving the script. You must ensure that the php_openssl.dll is active in the environment where email.php executes.

Recommended Fix: Switch to TLS on Port 587

While port 465 works for Gmail, it is increasingly less common and can be problematic in local environments due to strict SSL requirements. A more robust solution is to switch to TLS (STARTTLS), which uses port 587. This method encrypts the connection after the initial handshake, making it slightly more flexible regarding local security settings.

To resolve the SMTP connect() failed error without relying solely on SSL for port 465, update your PHPMailer configuration as follows:

$mail = new PHPMailer();

// Set to true to enable SMTP authentication
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // Remove the 'ssl://' prefix
$mail->SMTPAuth = true;
$mail->Username = 'xxxx@gmail.com';
$mail->Password = 'zzz'; // Consider using an App Password for Gmail accounts

// Switch to TLS encryption and port 587
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

By removing the ssl:// prefix from the host and switching $mailer->SMTPSecure to 'tls', you align with modern SMTP standards. This configuration is generally more reliable across different PHP builds and operating systems, including Windows-based XAMPP setups.

Verifying the OpenSSL Extension

If the TLS approach still yields connection failures, verify that the OpenSSL extension is actually loaded in your active PHP environment. The source material mentions modifying files in c/xampp/apache/bin/php.ini and c/xampp/php/php.ini. In a typical XAMPP installation, Apache might use one configuration file while CLI scripts or other modules use another.

To confirm the extension status, create a test file named test_openssl.php in your root directory with the following content:

<?php
if (extension_loaded('openssl')) {
    echo "OpenSSL is loaded.";
} else {
    echo "OpenSSL is NOT loaded. Check your php.ini configuration.";
}
?>

Access this file via your browser. If it reports that OpenSSL is not loaded, you must ensure the line extension=php_openssl.dll (or .so on Linux) is uncommented in the specific php.ini file being used by the Apache server serving your site. Restart the XAMPP services after making changes to ensure the new configuration takes effect.

Alternative: Using App Passwords

Finally, note that standard Gmail passwords are no longer accepted for SMTP access if two-factor authentication (2FA) is enabled on the account. Even with the correct SSL/TLS settings and OpenSSL extensions active, authentication will fail if using a regular password. You must generate an "App Password" specifically for less secure apps via your Google Account security settings and use that string in the $mail->Password variable. This step is critical for any Gmail-based SMTP setup to function correctly.

Our network