How to deal with self-signed TLS certificates in Laravel's SMTP driver?

How to deal with self-signed TLS certificates in Laravel's SMTP driver?

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

Stefan Bogdanescu

Founder & Senior Architect

How to deal with self-signed TLS certificates in Laravel's SMTP driver?

When configuring an application to send emails via an external SMTP server, you often encounter a wall of SSL certificate validation errors if that server uses a self-signed certificate or lacks a trusted Certificate Authority (CA) chain. The error stream_socket_enable_crypto(): SSL operation failed with code 1 combined with certificate verify failed is the classic symptom of Laravel attempting to enforce strict TLS verification against a host it cannot trust.

By default, PHP's OpenSSL extension verifies that the server's certificate matches the hostname provided in the configuration and checks if the issuer is present in the system's trusted CA bundle (usually /etc/ssl/certs/ca-certificates.crt on Linux). If your mail server at mail.mydomain.com presents a self-signed certificate, this verification fails immediately.

The standard solution involves disabling certificate verification within the Laravel configuration, but you must understand the implications of doing so before applying it.

Modifying the SMTP Configuration

Laravel 8 and later versions allow you to pass custom options directly into the underlying PHP stream context via the options key in your config/mail.php. To bypass the certificate check, add an entry for verify_peer set to false.

Here is how the configuration array should look:

return [

    'driver'     => 'smtp',

    'host'       => env('MAIL_HOST', 'mail.mydomain.com'),

    'port'       => env('MAIL_PORT', 26),

    'from'       => ['address' => env('MAIL_FROM_ADDRESS', 'mailer@mydomain.com'), 'name' => env('MAIL_FROM_NAME', 'Mailer')],

    'encryption' => env('MAIL_ENCRYPTION', 'tls'),

    'username'   => env('MAIL_USERNAME'),

    'password'   => env('MAIL_PASSWORD'),

    'timeout'    => null,

    'local_domain' => env('MAIL_FROM_DOMAIN', ''),

    'sendmail'   => env('MAIL_SENDMAIL_COMMAND', '/usr/sbin/sendmail -bs'),

    // The critical fix for self-signed certificates
    'options'    => [
        'verify_peer' => false,
        'verify_peer_name' => false,
        'SNI_hostname' => 'mail.mydomain.com', // Optional: helps with SNI verification if needed
    ],

    'pretend'    => env('MAIL_PRETEND', false),

];

Why verify_peer Matters

The verify_peer flag controls whether PHP checks the certificate against the trusted CA bundle. Setting it to false tells OpenSSL, "Trust whatever certificate this server presents." Similarly, verify_peer_name ensures the Common Name (CN) or Subject Alternative Name (SAN) in the certificate matches the hostname you configured. Disabling both prevents the specific SSL3_GET_SERVER_CERTIFICATE error caused by untrusted issuers or hostname mismatches.

The Security Trade-off

Disabling peer verification is a pragmatic fix for development environments or internal mail servers where security protocols are limited, but it introduces significant risks in production. Without this check, an attacker on the same network could perform a Man-in-the-Middle (MitM) attack. They could intercept your connection, present their own self-signed certificate, and you would accept it because verification is off.

In a production environment, the ideal long-term solution is to contact your hosting provider or system administrator to obtain a properly signed certificate from a trusted CA like Let's Encrypt, DigiCert, or Comodo. Once you have a valid certificate installed on mail.mydomain.com, you should remove the options block from your configuration to restore strict security checks.

Handling Legacy PHPMailer Issues

You may find references to similar issues in PHPMailer repositories regarding how to handle self-signed certificates. While those solutions often involve modifying global PHP settings or specific mailer instance options, Laravel's abstraction layer handles this differently by delegating strictly to the underlying stream context. The approach outlined above—passing options to the SMTP driver—is the native and most robust way to resolve these errors within the Laravel ecosystem without needing to patch external libraries or modify global system files.

If you are managing a fleet of applications and need to apply this fix across many environments, consider using a .env variable for the options to keep your config/mail.php clean:

'options' => [
    'verify_peer' => env('MAIL_VERIFY_PEER', false), // Defaults to false if not set in .env
],

Then, in your .env file, you can toggle this behavior based on the environment:

# .env
MAIL_VERIFY_PEER=false # For dev/staging with self-signed certs

This ensures that when you deploy to production with a valid CA certificate, you simply set MAIL_VERIFY_PEER=true (or remove the variable entirely, depending on your Laravel version's default behavior).

Our network