PHPMailer generates PHP Warning: stream_socket_enable_crypto(): Peer certificate did not match expected

心已入冬 提交于 2019-11-27 00:56:41

I had the same problem and I found the answer in the PHPMailer documentation.

PHP 5.6 certificate verification failure

In a change from earlier versions, PHP 5.6 verifies certificates on SSL connections. If the SSL config of the server you are connecting to is not correct, you will get an error like this:

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

The correct fix for this is to replace the invalid, misconfigured or self-signed certificate with a good one. Failing that, you can allow insecure connections via the SMTPOptions property introduced in PHPMailer 5.2.10 (it's possible to do this by subclassing the SMTP class in earlier versions), though this is not recommended:

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

You can also change these settings globally in your php.ini, but that's a really bad idea; PHP 5.6 made this change for very good reasons.

Sometimes this behaviour is not quite so apparent; sometimes encryption failures may appear as the client issuing a QUIT immediately after trying to do a STARTTLS. If you see that happen, you should check the state of your certificates or verification settings.

For PHP 5.6 use the following. Adding "tls://" is the key.

$mail->Host = gethostbyname('tls://smtp.gmail.com');

See: http://php.net/manual/en/context.ssl.php

For those of you using cPanel, I tried the SMTP check code from the examples folder in PHPMailer and I got this same error:

PHP Warning: stream_socket_enable_crypto(): Peer certificate  CN=*.mail.dreamhost.com' did not match expected CN=mx1.sub4.homie.mail.dreamhost.com' in /home/ikbb/domains/dev.ikbb.com/public_html/includes/phpmailer/5.2.10/class.smtp.php

I realized that it was not an error related to PHPMailer, so I searched for similar errors related to CentOS and I found this link that shed some light: Issue sending mails through 3rd party. You have to take a look at "SMTP Restrictions" in cPanel.

I had a similar problem after I've upgraded to PHP 5.6 on my WordPress machine. The WP Mail SMTP by WPForms (wp-mail-smtp) plugin were configured to use localhost as SMTP Host. I've changed it to the FQHN (Fully Qualified Host Name) as it is defined in the SSL cert. After this change it is working fine.

You might probably solved your problem already. But since other developer might be stucked on this, I will propose something that worked for me. I had the same problem with Laravel 4.2 Swiftmailer instead of PHPMailer, but with a Dreamhost VPS account. I didn't want to hack SMTP options verify_peer, allow_self_signed or set encryption from ssl to null. I didn't want neither to purchase a pro certificate for my Staging VPS, and I'm not on Production yet.

What I tried that didn't work from mail.php:

<?php
return array(

'driver' => 'smtp',

'host' => 'mail.mywebsite-staging.com',

'port' => 25,

'from' => array('address' => 'mywebsite@mywebsite-staging.com', 'name' => 'MyWebsite Staging'),

'encryption' => 'tls',

'username' => 'mywebsite@mywebsite-staging.com',

'password' => 'myPASS',

'sendmail' => '/usr/sbin/sendmail -bs',

'pretend' => false
);
?>

I found this Dreamhost Certificate Domain Mismatch Error documentation which tells us that our Mail server certificate resigns from a sub-domain (2 sub level) of dreamhost mail (*.mail.dreamhost.com) where * is a group that contains multiple mail accounts.

You have to go to:

  • Dreamhost Web Panel > Support > Data-Centers

and you will see what group your mail server belongs to, so you'll know which host to use in mail.php.

  • homiemail-sub3 => sub3.mail.dreamhost.com
  • homiemail-sub4 => sub4.mail.dreamhost.com
  • homiemail-sub5 => sub5.mail.dreamhost.com
  • homiemail-master => homie.mail.dreamhost.com

Mine was homiemail-sub4, then I used 'host' => 'sub4.mail.dreamhost.com',

Then no certificate problem. If using Mail Server from another Provider then try to check it wild is use also for your mail certificate.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!