![](/img/trans.png)
[英]CurlException: [curl] 51: SSL: certificate verification failed
[英]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.
剛剛收到相同的錯誤消息,也許您遇到了同樣的問題:
stream_context_create
和stream_socket_client
運行相同的請求通過 cURL 測試同一個域返回:
curl: (60) SSL certificate problem: certificate is not yet valid
事實證明,我在其中運行 PHP 和 cURL 的虛擬機出現了 11 天的錯誤(12 月 11 日而不是 12 月 22 日)。
通過修復計算機/虛擬機的日期(使用ntp
、 ntpdate-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.