Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Email delivery

Laravel mail fails with 'Unable to connect with STARTTLS' and SSL operation failed with code 1

ErrorUnable to connect with STARTTLS: stream_socket_enable_crypto(): SSL operation failed with code 1.

Short answer

The mail server offered STARTTLS, but PHP could not verify the certificate it presented. Either PHP has no usable CA bundle, or the server presents a certificate that is wrong, expired or from a private CA. Test the server with openssl first, then fix the side that fails.

  • Applies to Laravel 10, Laravel 11, Laravel 12, PHP 8.3, OpenSSL 3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask the mail server for its certificate using the same STARTTLS step Laravel performs, and read the Verification line.

    openssl s_client -starttls smtp -connect smtp.example.com:587 -servername smtp.example.com -brief
  2. 2Match what you see, then jump to its fix

    1. 1openssl s_client run on the same machine also reports a verification error, but the same command works from another machine.PHP has no CA bundle, or an outdated oneFix
    2. 2openssl s_client shows a Verification error on every machine, not only yours.The mail server presents a certificate that cannot be trustedFix
    3. 3The server is an internal relay or a staging box, and its certificate is signed by your own CA.A private CA or self-signed certificate in a test setupFix

What does the error look like?

  • Unable to connect with STARTTLS: stream_socket_enable_crypto(): SSL operation failed with code 1.
  • stream_socket_enable_crypto(): Peer certificate verification failed

Cause 1 of 3

PHP has no CA bundle, or an outdated one

1Check

PHP uses the file in openssl.cafile, then the directory in openssl.capath, to verify a peer. Both default to empty, and they can only be set in php.ini or per-directory configuration, not with ini_set().

php -i | grep -E "openssl.cafile|openssl.capath"

2Fix

Point openssl.cafile at a current PEM CA bundle in the php.ini used by PHP-FPM, then restart it.

; php.ini
openssl.cafile=/etc/ssl/certs/ca-certificates.crt

sudo systemctl restart php8.3-fpm

3Verify

Send a test mail from tinker.

php artisan tinker --execute="Mail::raw('test', fn(\$m) => \$m->to('you@example.com')->subject('test'));"

Confirmed by php.net

Did this fix it?

Cause 2 of 3

The mail server presents a certificate that cannot be trusted

1Check

A certificate that PHP cannot verify fails the same way on any client, so the test above fails on every machine. The -showcerts option lists the certificates the server sends.

openssl s_client -starttls smtp -connect smtp.example.com:587 -servername smtp.example.com -showcerts

2Fix

Ask the mail provider to install a valid certificate for the host name you use, or switch MAIL_HOST to the exact name on the certificate. If it is your own server, renew the certificate.

MAIL_HOST=mail.example.com

3Verify

openssl reports Verification: OK.

openssl s_client -starttls smtp -connect mail.example.com:587 -servername mail.example.com -brief

Confirmed by docs.openssl.org

Did this fix it?

Cause 3 of 3

A private CA or self-signed certificate in a test setup

1Check

Symfony Mailer, which Laravel uses, verifies the TLS peer by default. Verification can be turned off for development or self-signed certificates, which its docs do not recommend.

grep -n "MAIL_" .env

2Fix

Preferred: add your CA file to openssl.cafile as in the first cause. For a throw-away development box only, Symfony's verify_peer=0 DSN option disables the check; do not use it on the internet.

# Symfony DSN form, development only
smtp://user:pass@smtp.example.com:587?verify_peer=0

3Verify

Send a test mail, then switch back to a verified setup before going live.

php artisan config:clear

Confirmed by symfony.com

Did this fix it?

How do I stop it coming back?

  • Keep the OS CA package updated (ca-certificates on Debian and Ubuntu) and point php.ini at it.
  • Use the host name that is on the provider's certificate, never a bare IP address.
  • Monitor certificate expiry on any mail server you run yourself.
  • Never ship verify_peer=0 to production.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Email

PHP mail() returns true but the email never arrives

Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing

mail() returning true only means the message was accepted for delivery, not that it arrived.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.