Laravel mail fails with 'Unable to connect with STARTTLS' and SSL operation failed with code 1
Unable 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.
Fix it fast
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 -brief2Match what you see, then jump to its fix
- 1
openssl 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
openssl s_client shows a Verification error on every machine, not only yours.The mail server presents a certificate that cannot be trustedFix - 3
The 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
- 1
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-fpm3Verify
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
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 -showcerts2Fix
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.com3Verify
openssl reports Verification: OK.
openssl s_client -starttls smtp -connect mail.example.com:587 -servername mail.example.com -briefConfirmed by docs.openssl.org
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_" .env2Fix
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=03Verify
Send a test mail, then switch back to a verified setup before going live.
php artisan config:clearConfirmed by symfony.com
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
PHPMailer fails with 'certificate verify failed' when connecting to the SMTP server
OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
PHPMailer checks the mail server's certificate and the check failed.
3 causes, each with a fixLaravel tries to send mail to mailpit:1025 instead of your real SMTP server
Symfony\Component\Mailer\Exception\TransportException
Laravel is still using the development mail host from the starter .env, mailpit on port 1025, which only exists inside Laravel Sail.
3 causes, each with a fixLaravel mail fails because an address does not comply with RFC 2822
Email "Jane Doe <jane@example.com>" does not comply with addr-spec of RFC 2822.
Symfony Mailer checks every address when the Address object is created.
3 causes, each with a fixLaravel says the email was sent, but nothing arrives
Mail::to($user)->send() returns normally
Mail::send returns without an error when the message was accepted by the configured mailer, not when it was delivered.
3 causes, each with a fixCodeIgniter email does not send and shows no error message
$email->printDebugger() prints an empty result
CodeIgniter signals a failed email only by send() returning false, with no exception.
4 causes, each with a fixPHP 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