Home >Backend Development >PHP Tutorial >Why Does PHPMailer Emit a PHP Warning About a Mismatched Certificate in PHP 5.6?
PHPMailer Emits PHP Warning Due to Mismatched Certificate
PHPMailer users may encounter a warning when connecting to SMTP hosts on PHP 5.6:
PHP Warning: stream_socket_enable_crypto(): Peer certificate did not match expected
This warning stems from the increased security in PHP 5.6, which strictly verifies SSL certificates. The warning suggests that the certificate presented by the SMTP host does not match the expected certificate for that host.
Root Cause:
The mismatch occurs when the SMTP host presents a certificate that does not have a common name (CN) that matches the expected hostname. For example, if you are connecting to mx1.sub4.homie.mail.dreamhost.com and the certificate CN is *.mail.dreamhost.com, PHP will raise the warning.
Solution:
To resolve this issue, there are two approaches:
$mail->SMTPOptions = array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ) );
Note: Disabling certificate verification is not recommended and should be used only as a temporary solution.
Conclusion:
The PHP Warning "stream_socket_enable_crypto(): Peer certificate did not match expected" can be addressed by either contacting the SMTP host to update their certificate or by configuring PHPMailer to ignore certificate verification. The recommended solution is to have the SMTP host provide a valid certificate that matches the hostname.
The above is the detailed content of Why Does PHPMailer Emit a PHP Warning About a Mismatched Certificate in PHP 5.6?. For more information, please follow other related articles on the PHP Chinese website!