SSL3_GET_SERVER_CERTIFICATE 使用 IIS 的 Windows 10 Pro 上的证书验证失败

SSL3_GET_SERVER_CERTIFICATE certificate verify failed on Windows 10 Pro with IIS

在 Windows 10 上由 IIS 托管的 PHP 上 PHPMailer 尝试通过 smtp.google.com 发送电子邮件时,我收到此错误消息:

Connection failed. 
Error #2: stream_socket_enable_crypto(): SSL operation failed with code 1. 
OpenSSL Error messages
error:14090086
SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

我确定该错误的关键部分是证书验证失败。我是 运行 OpenSSL,它已启用并链接到我的 php。这是我本地 PHP 实例输出的一些信息:

OpenSSL support => enabled
OpenSSL Library Version => OpenSSL 1.0.1p 9 Jul 2015
OpenSSL Header Version => OpenSSL 1.0.1p 9 Jul 2015

Directive => Local Value => Master Value
openssl.cafile => no value => no value
openssl.capath => no value => no value

这是我的 PHP 代码:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';

$mail = new PHPMailer;
$mail->isSMTP(); 
$mail->SMTPDebug = 2; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
$mail->Host = "smtp.gmail.com"; // use $mail->Host = gethostbyname('smtp.gmail.com'); // if your network does not support SMTP over IPv6
$mail->Port = 587; // TLS only
$mail->SMTPSecure = 'tls'; // ssl is depracated
$mail->SMTPAuth = true;
$mail->Username = $smtpUsername;
$mail->Password = $smtpPassword;
$mail->setFrom($emailFrom, $emailFromName);
$mail->addAddress($emailTo, $emailToName);
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("test body"); //$mail->msgHTML(file_get_contents('contents.html'), __DIR__); //Read an HTML message body from an external file, convert referenced images to embedded,
$mail->AltBody = 'HTML messaging not supported';
// $mail->addAttachment('images/phpmailer_mini.png'); //Attach an image file

if(!$mail->send()){
    echo "Mailer Error: " . $mail->ErrorInfo;
}else{
    echo "Message sent!";
}

我对 OpenSSL 和证书问题一窍不通。我试图理解 PHPMailer troubleshooting guide更新 CA 证书 部分,但我感到迷茫和困惑。有人可以给我一组我可以遵循的步骤并解决我本地 Windows 10 机器上的证书问题吗?

要解释该指南,请下载 the CA bundle from curl 并将其存储在您的文件系统中的某个位置。采用您保存它的路径并在您的 php.ini 文件中添加一行:

openssl.cafile = $path

$path 是您保存 CA 证书的位置。然后重新启动您的网络服务器以获取 ini 更改。

如果有效,您应该会在 phpinfo() 的输出中看到该设置,并且它还应该提供 PHP 当 PHPMailer 使用它时验证证书所需的内容.请注意,如果服务器提供的证书确实无效或过期,这将无济于事,但鉴于这是一个众所周知的问题和解决方案,我希望它能奏效。