PHPMailer: the SMTP certificate does not match the host name you connect to
stream_socket_enable_crypto(): Peer certificate did not match expectedShort answer
PHP compares the certificate of the mail server with the host name used to open the connection. If you connect with an IP address or an alias that the certificate does not list, the check fails. Set the Host to a name the certificate really lists. Switching verification off is a last resort.
Fix it fast
1Find out which cause you have
Read the names inside the certificate the server presents, then compare them with the Host value in your code.
openssl s_client -connect smtp.example.com:587 -starttls smtp -servername smtp.example.com </dev/null 2>/dev/null | openssl x509 -noout -subject -text2Match what you see, then jump to its fix
- 1
$mail->Host contains digits such as 203.0.113.10Host is an IP addressFix - 2
The openssl output lists other names than your Host value (for example the provider's main domain)Host is a name the certificate does not listFix - 3
You need mail to flow while the provider fixes its certificateVerification switched off as a last resortFix
- 1
What does the error look like?
stream_socket_enable_crypto(): Peer certificate did not match expectedCertificate name checks fail when the SMTP host is a raw IP address
Cause 1 of 3
Host is an IP address
1Check
PHPMailer's troubleshooting page says that connecting by a raw IP address will usually cause certificate name checks to fail.
grep -rn "Host" send-mail.php2Fix
Use the server's host name, the one that appears in the certificate.
$mail->Host = 'smtp.example.com';3Verify
Send a test message and check that the name warning is gone.
php send-mail.phpConfirmed by github.com
Cause 2 of 3
Host is a name the certificate does not list
1Check
PHP builds the name to verify from the host in the connection, unless peer_name is set. verify_peer_name is on by default.
$mail->Host = 'mail.mydomain.example'; // alias, not on the certificate2Fix
Point Host at a name from the certificate. If the provider tells you to connect through an alias, set the stream option peer_name to the certificate's name, which keeps verification switched on.
$mail->Host = 'mail.mydomain.example';
$mail->SMTPOptions = [
'ssl' => [
'peer_name' => 'smtp.example.com',
],
];3Verify
Send a test message; the warning should not appear.
php send-mail.phpConfirmed by php.net
Cause 3 of 3
Verification switched off as a last resort
1Check
PHPMailer documents a SMTPOptions block that turns the checks off, and says it defeats much of the point of using a secure transport.
grep -rn "SMTPOptions" .2Fix
Use it only for a short, known test, then remove it. The proper fix is a certificate that matches the name, which only the mail server owner can install.
$mail->SMTPOptions = [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
],
];3Verify
Remove the block again once the certificate is fixed and send another test message.
php send-mail.phpConfirmed by github.com
How do I stop it coming back?
- Always use the provider's documented host name for SMTP, never an IP address.
- Do not leave verify_peer_name set to false in production code.
- Recheck the certificate names when a provider changes servers.
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 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