Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Email delivery

PHPMailer: the SMTP certificate does not match the host name you connect to

Errorstream_socket_enable_crypto(): Peer certificate did not match expected

Short 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.

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

Fix it fast

  1. 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 -text
  2. 2Match what you see, then jump to its fix

    1. 1$mail->Host contains digits such as 203.0.113.10Host is an IP addressFix
    2. 2The 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. 3You need mail to flow while the provider fixes its certificateVerification switched off as a last resortFix

What does the error look like?

  • stream_socket_enable_crypto(): Peer certificate did not match expected
  • Certificate 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.php

2Fix

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.php

Confirmed by github.com

Did this fix it?

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 certificate

2Fix

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.php

Confirmed by php.net

Did this fix it?

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.php

Confirmed by github.com

Did this fix it?

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

Next step

Want this handled for good?

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