PHPMailer fails with 'certificate verify failed' when connecting to the SMTP server
Warning: 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.
Fix it fast
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.com2Match what you see, then jump to its fix
- 1
openssl s_client reports a verification error for the server's own certificate.The server's certificate is bad or expiredFix - 2
Other 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
SMTPSecure is not set, but the server advertises TLS, and the failure starts after the TLS upgrade.STARTTLS is offered automatically with an invalid certificateFix
- 1
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 -subject2Fix
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 service3Verify
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
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 cafile2Fix
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.pem3Verify
Restart PHP and send again.
php -i | grep cafileConfirmed by github.com
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
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
Back end
PHP cURL or Guzzle fails with SSL certificate error 60
curl: (60) SSL certificate problem: unable to get local issuer certificate
cURL cannot find a trusted CA file to check the server's certificate against.
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 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 fixLaravel mail fails with 'Unable to connect with STARTTLS' and SSL operation failed with code 1
stream_socket_enable_crypto(): Peer certificate verification failed
The mail server offered STARTTLS, but PHP could not verify the certificate it presented.
3 causes, each with a fix