使用PHPMailer 中的报错解决 "Connection failed. Error #2: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:"

PHPMailer项目地址:https://github.com/PHPMailer/PHPMailer

项目中用到PHPMailer,使用过程中报错:"Connection failed. Error #2: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:"

由于我用的第三方smtp是ssl链接,所以需要再添加一些参数:

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

官方是这样说明的:"

This is covered in the troubleshooting docs. PHP 5.6 verifies SSL certificates by default, and if your cert doesn't match, it will fail with this error. The correct solution is to fix your SSL config - it's not PHP's fault!

If you're sending on localhost you can use isMail() and it won't go through an encryption layer at all."

翻译如下:

疑难解答文档中介绍了这一点。默认情况下,PHP 5.6验证SSL证书,如果您的证书不匹配,则会出现此错误。正确的解决方案是修复您的SSL配置 - 这不是PHP的错误! 如果您正在本地发送,您可以使用isMail(),而不会通过加密层。

 

可是,我用的是php7.1啊!可见,php7及以上版本保留了php5.6这一特点。加了上述参数后问题解决了。

不过,官方的建议是,为了安全起见,还是建议大家效验ssl证书的。关于PHP是如何效验ssl证书的,请参考:https://secure.php.net/manual/en/context.ssl.php

 

在PHPMailer里效验证书的方法,是有给出配置的:

$mail->SMTPOptions = array (
    'ssl' => array(
        'verify_peer'  => true,
        'verify_depth' => 3,
        'allow_self_signed' => true,
        'peer_name' => 'smtp.example.com',
        'cafile' => '/etc/ssl/ca_cert.pem',
    )
);

 

出处:https://github.com/PHPMailer/PHPMailer/issues/368

posted on 2017-08-24 09:01  wpjamer  阅读(7695)  评论(0编辑  收藏  举报

导航