localhost and "stream_socket_enable_crypto(): SSL operation failed with code 1"

localhost and "stream_socket_enable_crypto(): SSL operation failed with code 1"

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

Stefan Bogdanescu

Founder & Senior Architect

localhost and "stream_socket_enable_crypto(): SSL operation failed with code 1"

You are likely encountering a classic environment mismatch issue between your local development setup and your production server. The error message stream_socket_enable_crypto(): SSL operation failed with code 1 combined with error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed explicitly points to a failure in validating the SSL certificate presented by the remote server (in your case, Gmail's SMTP).

The fact that this works on your web host but fails on localhost is the critical clue. Production servers usually have a fully configured OpenSSL environment with the latest root certificates trusted by default. Local environments, particularly those running XAMPP, MAMP, or Docker, often lack these certificate bundles or rely on older versions of OpenSSL that do not trust the modern CA hierarchy used by public services like Gmail.

The Root Cause: Missing Certificate Bundle

When you attempt to connect to smtp.gmail.com using TLS encryption, your application establishes a raw TCP connection and then calls stream_socket_enable_crypto() to upgrade it to an SSL session. During this handshake, the server sends its certificate chain. Your local OpenSSL library attempts to verify this chain against a list of trusted Root Certificates (the Certificate Authority bundle).

If the verification fails with code 1 (SSL_ERROR_SSL), and the specific sub-error is ssl3_get_server_certificate:certificate verify failed, it almost certainly means your local system cannot find the certificate for the issuer that signed Gmail's certificate, or it considers the chain incomplete.

This is not a bug in SwiftMailer (the library shown in the stack trace using Swift_Transport_StreamBuffer). The logic is sound; the environment simply lacks the trust anchor required to validate the connection.

Solution: Injecting the CA Bundle

To resolve this on a local machine running XAMPP or similar, you need to manually provide the Certificate Authority bundle to the PHP stream context before attempting the TLS handshake.

You can do this by modifying your config/mail.php file or your specific mailer class instantiation to inject a custom CA path. Here is how you configure the SSL options in Laravel:

return [
    'driver' => env('MAIL_DRIVER', 'smtp'),
    'host' => env('MAIL_HOST', 'smtp.gmail.com'),
    'port' => env('MAIL_PORT', 587),
    'encryption' => env('MAIL_ENCRYPTION', 'tls'),
    'username' => env('MAIL_USERNAME'),
    'password' => env('MAIL_PASSWORD'),
    
    // Add these SSL options to handle local certificate issues
    'timeout' => null,
    'local_domain' => env('MAIL_FROM_DOMAIN', ''),
    
    'options' => [
        'ssl' => [
            'verify_peer' => true,
            'verify_peer_name' => true,
            'cafile' => __DIR__ . '/../vendor/phpseclib/phpseclib/phpseclib/Crypt/SSL/ca-bundle.crt', 
            // Or better yet, use the system bundle if available
            'local_cert' => null,
        ],
    ],
];

However, a simpler and more robust approach is to ensure the curl CA bundle is used, as many PHP distributions rely on it. You can set the stream context to use the system's CA bundle by modifying your mail configuration:

'mail' => [
    'driver' => 'smtp',
    'host' => 'smtp.gmail.com',
    'port' => 587,
    'encryption' => 'tls',
    'username' => 'your@gmail.com',
    'password' => 'app-password',
    
    // Inject the system CA bundle into the stream context
    'stream_options' => [
        'ssl' => [
            'verify_peer' => true,
            'verify_peer_name' => true,
            'cafile' => '/etc/ssl/certs/ca-certificates.crt', // Path varies by OS
            // On Windows (XAMPP), this might be 'C:/xampp/php/ca-bundle.crt' or similar
        ],
    ],
],

If you are on a fresh Linux installation, the file is typically located at /etc/ssl/certs/ca-certificates.crt. On macOS, it is often at /etc/ssl/cert.pem. On Windows with XAMPP, the path might differ, and sometimes the php.ini setting openssl.cafile needs to be explicitly set if the default isn't sufficient.

Verification Steps

Before assuming the fix works, verify that your local OpenSSL installation actually has access to these certificates. You can test this directly from the command line:

# Linux/Mac
openssl s_client -connect smtp.gmail.com:587 -showcerts < /dev/null 2>&1 | grep "Verify return code:"

# Windows (PowerShell)
openssl s_client -connect smtp.gmail.com:587 -showcerts < NUL

If this command returns verify error:num=20:unable to get local issuer certificate or similar, your OpenSSL installation is missing the root store. You must install the CA certificates package (e.g., ca-certificates on Ubuntu/Debian) or point PHP explicitly to a valid bundle file.

Alternative: Disabling Certificate Verification (Not Recommended)

Some developers might be tempted to set verify_peer to false in the stream options to bypass the error entirely. While this stops the error, it is a significant security risk. You are essentially telling your application to accept any certificate presented by the server, making you vulnerable to Man-in-the-Middle attacks. Given that Gmail requires valid certificates, disabling verification offers no functional benefit and compromises your app's security posture. Stick to injecting the correct CA bundle.

By ensuring your local development environment mirrors the production trust store, you eliminate this discrepancy without needing to change how you authenticate with Google or alter your application logic.

Our network