php PHPMailer - SSL3_GET_SERVER_CERTIFICATE:证书验证失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26827192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:04:31  来源:igfitidea点击:

PHPMailer - SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

phpphpmailer

提问by gor181

Have encountered an issue where email should be sent from an mail server which has self signed certificate, the error which I get is :

遇到应该从具有自签名证书的邮件服务器发送电子邮件的问题,我得到的错误是:

PHP 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 in class.smtp.php on line 327.

Has anyone encountered anything similar?

有没有人遇到过类似的事情?

EDIT:

编辑:

I have also tried to set the stream_context params (params: SSL context options):

我还尝试设置 stream_context 参数(参数:SSL 上下文选项):

$options['ssl']['verify_peer'] = false;
$options['ssl']['verify_peer_name'] = false;
$options['ssl']['allow_self_signed'] = true;

No luck, it still fails with the same error as pointed above.

不走运,它仍然失败,并出现与上述相同的错误。

Thanks.

谢谢。

回答by Synchro

PHP 5.6 introduces SSL certificate verification, so if your config is broken, it will fail with this error. You should fix your SSL, but you can revert to the old behaviour by setting the SMTPOptionsproperty to not verify certificates:

PHP 5.6 引入了 SSL 证书验证,因此如果您的配置被破坏,它将失败并显示此错误。您应该修复您的 SSL,但您可以通过将SMTPOptions属性设置为不验证证书来恢复旧行为:

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

Editing the library defeats the entire point of libraries - and if you do as Kaf's answer suggests, your code will break when you upgrade. Really, don't do that.

编辑库会破坏库的全部意义 - 如果您按照 Kaf 的回答进行操作,则升级时您的代码将中断。真的,不要那样做。

Editor's note: disabling SSL verification has security implications.Without verification of the authenticity of SSL/HTTPS connections, a malicious attacker can impersonate a trusted endpoint (such as GitHub or some other remote Git host), and you'll be vulnerable to a Man-in-the-Middle Attack. Be sure you fully understand the security issues before using this as a solution.

编者注:禁用 SSL 验证具有安全隐患。如果不验证 SSL/HTTPS 连接的真实性,恶意攻击者可以冒充受信任的端点(例如 GitHub 或其他一些远程 Git 主机),您将容易受到中间人攻击在使用它作为解决方案之前,请确保您完全了解安全问题。

回答by Kaf

I have the same problem. So i changed the file class.smtp.php in line 238:

我也有同样的问题。所以我在第 238 行更改了文件 class.smtp.php:

public function connect($host, $port = null, $timeout = 30, $options = array()) {
       if (count($options) == 0) {
           $options['ssl'] = array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true);
       }

now it works fine!

现在它工作正常!

Editor's note: disabling SSL verification has security implications.Without verification of the authenticity of SSL/HTTPS connections, a malicious attacker can impersonate a trusted endpoint (such as GitHub or some other remote Git host), and you'll be vulnerable to a Man-in-the-Middle Attack. Be sure you fully understand the security issues before using this as a solution.

编者注:禁用 SSL 验证具有安全隐患。如果不验证 SSL/HTTPS 连接的真实性,恶意攻击者可以冒充受信任的端点(例如 GitHub 或其他一些远程 Git 主机),您将容易受到中间人攻击在使用它作为解决方案之前,请确保您完全了解安全问题。

回答by SurfMan

I had the same problem. It turned out that my Postfix config was missing the intermediates and root certificates setting:

我有同样的问题。事实证明,我的 Postfix 配置缺少中间体和根证书设置:

smtpd_tls_CAfile=/etc/ssl/certs/intermediate-root-bundle.crt

Even though this Postfix config has worked for years with Outlook and Thunderbird, PHP was more picky and failed the SSL check.

尽管这个 Postfix 配置已经在 Outlook 和 Thunderbird 上工作了多年,但 PHP 更加挑剔并且未能通过 SSL 检查。

So even though you might be tempted to hack PHPMailer, please don't, and fix the underlying problem.

因此,即使您可能想破解 PHPMailer,也请不要这样做,并解决潜在的问题。

回答by alvinb

Just wanted to put my 2 cents in since I've been looking for a fix for days until I tried Kaf's solution and it worked!! Thanks @Kaf

只是想投入我的 2 美分,因为我几天来一直在寻找解决方案,直到我尝试了 Kaf 的解决方案并且它奏效了!!谢谢@卡夫

Anyways... For me, PHPMailer was working fine until I decided to upgrade PHP to PHP5.6

无论如何......对我来说,PHPMailer 工作正常,直到我决定将 PHP 升级到 PHP5.6

Changes were made to open ssl in PHP 5.6. Here is the official docs:

在 PHP 5.6 中对打开 ssl 进行了更改。这是官方文档:

http://php.net/manual/en/migration56.openssl.php

http://php.net/manual/en/migration56.openssl.php

From the docs it says to set verify_peerand verify_peer_nameto false

从文档中它说将verify_peerverify_peer_name设置为false

So just follow Kaf's answer and see if that works for you.

所以只需按照 Kaf 的回答,看看它是否适合你。

Editor's note: The doc also says this is not recommended! Disabling SSL verification has security implications.Without verification of the authenticity of SSL/HTTPS connections, a malicious attacker can impersonate a trusted endpoint (such as GitHub or some other remote Git host), and you'll be vulnerable to a Man-in-the-Middle Attack. Be sure you fully understand the security issues before using this as a solution.

编者按:文档也说不推荐这样做!禁用 SSL 验证具有安全隐患。如果不验证 SSL/HTTPS 连接的真实性,恶意攻击者可以冒充受信任的端点(例如 GitHub 或其他一些远程 Git 主机),您将容易受到中间人攻击在使用它作为解决方案之前,请确保您完全了解安全问题。