Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Email delivery

PHPMailer fails with 'certificate verify failed' when connecting to the SMTP server

ErrorWarning: stream_socket_enable_crypto(): SSL operation failed with code 1.

Short answer

PHPMailer checks the mail server's certificate and the check failed. The cause is a bad or expired certificate on the server, an old CA bundle in PHP, or STARTTLS being offered with an invalid certificate. Test the server with openssl s_client first. Disabling verification with SMTPOptions is a last resort for debugging only.

  • Applies to PHP 8.3, PHP 8.4, PHPMailer 6, OpenSSL 3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Connect to the SMTP server yourself and read the certificate result. Use the same host name and port that PHPMailer uses.

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

    1. 1openssl s_client reports a verification error for the server's own certificate.The server's certificate is bad or expiredFix
    2. 2Other tools on the same machine trust the server, but PHP does not; the machine is old or not updated.PHP's CA certificate file is outdatedFix
    3. 3SMTPSecure is not set, but the server advertises TLS, and the failure starts after the TLS upgrade.STARTTLS is offered automatically with an invalid certificateFix

What does the error look like?

  • Warning: stream_socket_enable_crypto(): SSL operation failed with code 1.
  • OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

Cause 1 of 3

The server's certificate is bad or expired

1Check

PHPMailer's own troubleshooting page lists a bad or expired certificate on the mail server first among the causes. Connect with s_client and read the certificate details and the verify result.

openssl s_client -connect smtp.example.com:587 -starttls smtp -servername smtp.example.com </dev/null 2>/dev/null | openssl x509 -noout -dates -subject

2Fix

Renew the certificate on the mail server. Only the mail server's owner can fix a bad certificate; if you run it yourself, install a valid one and restart the mail service.

# on the mail server: install a valid, unexpired certificate, then restart the mail service

3Verify

s_client now ends with a successful verify result and the script sends.

openssl s_client -connect smtp.example.com:587 -starttls smtp -servername smtp.example.com </dev/null 2>&1 | grep -i "verif"

Confirmed by github.com

Did this fix it?

Cause 2 of 3

PHP's CA certificate file is outdated

1Check

PHP reads trusted CAs from the file set in openssl.cafile (and curl.cainfo for curl). The troubleshooting page says the preferred fix is current CA certificates, which normally arrive through OS updates.

php -i | grep cafile

2Fix

Update the operating system first. If PHP still has no current bundle, download one, store it, and point php.ini at it.

; php.ini
openssl.cafile = /etc/ssl/cacert.pem
curl.cainfo = /etc/ssl/cacert.pem

3Verify

Restart PHP and send again.

php -i | grep cafile

Confirmed by github.com

Did this fix it?

Cause 3 of 3

STARTTLS is offered automatically with an invalid certificate

1Check

Since PHPMailer 5.2.10, SMTPAutoTLS turns encryption on whenever the server advertises it, even without SMTPSecure. If that certificate is invalid, the handshake fails. Also check that port and mode are paired correctly: STARTTLS with 587, SMTPS with 465.

grep -rn "SMTPSecure\|SMTPAutoTLS\|->Port" src/

2Fix

Set the mode and port as a matching pair. Turning SMTPAutoTLS off sends mail without encryption, so use it only until the server certificate is repaired.

$mail->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// temporary, only while the server certificate is being fixed:
// $mail->SMTPAutoTLS = false;

3Verify

Send a test message with $mail->SMTPDebug = 2 and read the SMTP conversation.

$mail->SMTPDebug = 2;

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Keep the operating system and its CA certificates updated.
  • Check the mail server's certificate dates now and then.
  • Never leave verify_peer disabled in production; the PHPMailer docs warn it removes most of the protection of TLS.
  • Pair SMTPSecure and Port correctly: STARTTLS with 587, SMTPS with 465.

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

Next step

Want this handled for good?

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