繁体   English   中英

如何修复 SSL 证书验证失败

[英]how to fix SSL certificate verification failure

我在本地使用 PHPMailer,以及 Apache 和 PHP。 当我测试我的 SSL 配置和我的 cacerts 时,我得到

default_cert_file = C:\Program Files\Common Files\SSL/cert.pem
default_cert_file_env = SSL_CERT_FILE
default_cert_dir = C:\Program Files\Common Files\SSL/certs
default_cert_dir_env = SSL_CERT_DIR
default_private_dir = C:\Program Files\Common Files\SSL/private
default_default_cert_area = C:\Program Files\Common Files\SSL
ini_cafile = 
ini_capath = 

然后,我做

 var_dump(fsockopen("smtp.gmail.com", 465, $errno, $errstr, 3.0));
 var_dump($errno);
 var_dump($errstr);

我得到

fsockopen resource(2) of type (stream) int(0) string(0) "" 

在我的php.ini中, curl部分有curl.cainfo = C:/php/cacert.pem并且它可以工作。

但是当我尝试使用 PHPMailer 从本地主机发送邮件时,我不断收到

SSL operation failed with code 1. OpenSSL Error messages:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed 
Failed to enable crypto
unable to connect to ssl://smtp.gmail.com:465 (Unknown error)

我还访问了 SMTP 中使用的帐户,在https://accounts.google.com/DisplayUnlockCaptcha启用它。 并访问https://myaccount.google.com/lesssecureapps?pli=1并启用安全性较低的应用程序。 我想这是一个 SSL 错误而不是一个 PHPMailer。 坦率地说,我很困惑不知道如何解决它。 请原谅我的无知,任何关于出了什么问题以及如何解决它的帮助都会很棒。

PS,这是我发送邮件的php文件。 谢谢

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;    
require 'C:/php/PHPMailer/src/Exception.php';
require 'C:/php/PHPMailer/src/PHPMailer.php';
require 'C:/php/PHPMailer/src/SMTP.php';

$mail = new PHPMailer(true);                             
try {

    $mail->SMTPDebug = 3;
    $mail->isSMTP();                                      
    $mail->Host = 'smtp.gmail.com'; 
    $mail->SMTPAuth = true;                               
    $mail->Username = 'slevin@gmail.com';                 
    $mail->Password = 'secret';                           
    $mail->SMTPSecure = 'ssl';                            
    $mail->Port = 465;                                    

    //Recipients
    $mail->setFrom('slevin@gmail.com');
    $mail->addAddress('jacob@gmail.com');     


    //Content
    $mail->isHTML(true);                                 
    $mail->Subject = 'subject';
    $mail->Body    = 'HTML message body <b>in bold!</b>';
    $mail->AltBody = 'body in plain text';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}            
?>

更新

当我尝试$mail->SMTPSecure = 'ssl'; $mail->Port = 465; 我得到

2017-10-01 13:04:25 Connection: opening to ssl://smtp.gmail.com:465, timeout=300, options=array()
2017-10-01 13:04:26 Connection failed. Error #2: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed [C:\php\PHPMailer\src\SMTP.php line 324]
2017-10-01 13:04:26 Connection failed. Error #2: stream_socket_client(): Failed to enable crypto [C:\php\PHPMailer\src\SMTP.php line 324]
2017-10-01 13:04:26 Connection failed. Error #2: stream_socket_client(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error) [C:\php\PHPMailer\src\SMTP.php line 324]
2017-10-01 13:04:26 SMTP ERROR: Failed to connect to server: (0)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

当我尝试$mail->SMTPSecure = 'tls'; $mail->Port = 587; 我得到

2017-10-01 13:07:20 Connection: opening to smtp.gmail.com:587, timeout=300, options=array()
2017-10-01 13:07:21 Connection: opened
2017-10-01 13:07:21 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP l4sm5217189wrb.74 - gsmtp
2017-10-01 13:07:21 CLIENT -> SERVER: EHLO localhost
2017-10-01 13:07:21 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [85.75.196.114]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250 SMTPUTF8
2017-10-01 13:07:21 CLIENT -> SERVER: STARTTLS
2017-10-01 13:07:21 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2017-10-01 13:07:21 Connection failed. Error #2: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed [C:\php\PHPMailer\src\SMTP.php line 403]
SMTP Error: Could not connect to SMTP host.
2017-10-01 13:07:21 CLIENT -> SERVER: QUIT
2017-10-01 13:07:21 SERVER -> CLIENT: 
2017-10-01 13:07:21 SMTP ERROR: QUIT command failed: 
2017-10-01 13:07:21 Connection: closed
SMTP Error: Could not connect to SMTP host.
Message could not be sent.Mailer Error: SMTP Error: Could not connect to SMTP host.

刚刚收到相同的错误消息,也许您遇到了同样的问题:

  • 我的证书很好(我可以毫无问题地访问 HTTPS 域)
  • 无法通过 PHP 的stream_context_createstream_socket_client运行相同的请求
  • 我想检查 SSL 证书

通过 cURL 测试同一个域返回:

curl: (60) SSL certificate problem: certificate is not yet valid

事实证明,我在其中运行 PHP 和 cURL 的虚拟机出现了 11 天的错误(12 月 11 日而不是 12 月 22 日)。

通过修复计算机/虚拟机的日期(使用ntpntpdate-debian等),证书测试现在可以在 cURL 命令行或 PHP 中正常运行。

我最近在一个新的 Windows IIS 服务器上遇到了这个问题。 cURL 调用是从批处理脚本到我自己的域的,这两个脚本都在同一台服务器上运行。

我加了

127.0.0.1  mydomain.com

到主机文件,我在更改公共 DNS 时忘记删除它。

服务器在 Cloudflare 后面,所以我的 cURL 获得的是 CF 原始证书,而不是 Cloudflare 提供的真实证书。

删除主机条目清除了问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM