如何解决SSL证书验证问题?

问题描述 投票:0回答:1

我正在开发一个网站,我想添加电子邮件发送选项。

我写了以下代码。

$mail = new PHPMailer();

$mail->isSMTP();
$mail->Host = ’[email protected]’;
$mail->SMTPAuth = true;
$mail->Username = *****;
$mail->Password = ****;
$mail->SMTPSecure = 'tls';
$mail->Port = 25;
$mail->setFrom(’****.com’,’John’);

$mail->addAddress(’*****.com’,’Doe’);

$mail->Subject = 'email test;
$mail->isHTML(TRUE);
$mail->Body = 'html>';


if(!$mail->send()){
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

但是我收到以下错误。

连接失败。错误 #2:stream_socket_enable_crypto():SSL 操作失败,代码为 1。OpenSSL 错误消息:错误:1416F086:SSL 例程:tls_process_server_certificate:证书验证失败 [PHPMailer-master\src\SMTP.php 第 490 行] SMTP 错误:无法连接到 SMTP 主机。连接失败。 Stream_socket_enable_crypto():SSL 操作失败,代码为 1。OpenSSL 错误消息:错误:1416F086:SSL 例程:tls_process_server_certificate:证书验证失败

我多次更改端口。而且我还多次更改了 SMTPSecure 的值。

更新:

总错误信息:

2024-07-02 10:51:46 Connection: opening to [email protected]:25, timeout=300, options=array()
2024-07-02 10:51:46 Connection: opened
2024-07-02 10:51:46 SMTP INBOUND: "220 [email protected] NO UCE ESMTP Postfix (2.10.1)"
2024-07-02 10:51:46 SERVER -> CLIENT: 220 [email protected] NO UCE ESMTP Postfix (2.10.1)
2024-07-02 10:51:46 CLIENT -> SERVER: EHLO localhost
2024-07-02 10:51:46 SMTP INBOUND: "[email protected]"
2024-07-02 10:51:46 SMTP INBOUND: "250-PIPELINING"
2024-07-02 10:51:46 SMTP INBOUND: "250-SIZE 20857600"
2024-07-02 10:51:46 SMTP INBOUND: "250-VRFY"
2024-07-02 10:51:46 SMTP INBOUND: "250-ETRN"
2024-07-02 10:51:46 SMTP INBOUND: "250-STARTTLS"
2024-07-02 10:51:46 SMTP INBOUND: "250-AUTH PLAIN LOGIN"
2024-07-02 10:51:46 SMTP INBOUND: "250-ENHANCEDSTATUSCODES"
2024-07-02 10:51:46 SMTP INBOUND: "250-8BITMIME"
2024-07-02 10:51:46 SMTP INBOUND: "250 DSN"
2024-07-02 10:51:46 SERVER -> CLIENT: [email protected] 20857600250-VRFY250-ETRN250-STARTTLS250-AUTH PLAIN LOGIN250-ENHANCEDSTATUSCODES250-8BITMIME250 DSN
2024-07-02 10:51:46 CLIENT -> SERVER: STARTTLS
2024-07-02 10:51:46 SMTP INBOUND: "220 2.0.0 Ready to start TLS"
2024-07-02 10:51:46 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2024-07-02 10:51:46 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 [\PHPMailer-master\src\SMTP.php line 490]
SMTP Error: Could not connect to SMTP host.
Connection failed. stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed
2024-07-02 10:51:46 CLIENT -> SERVER: QUIT
php ssl ssl-certificate phpmailer
1个回答
0
投票

根据详细的错误消息和您提供的 PHPMailer 代码,尝试与您的 SMTP 服务器建立 TLS 连接时,SSL 证书验证似乎失败。

解决问题的步骤

  1. 确保正确的 SMTP 配置:
    • 验证 SMTP 主机、用户名和密码是否正确。
    • 确认您的 SMTP 服务器在端口 25 上支持 TLS(或使用端口 587 进行 TLS)。
  2. 禁用 SSL 验证进行测试:
    • 暂时禁用 SSL 证书验证,看看这是否是问题的原因。

更新了 PHPMailer 代码

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

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    // Server settings
    $mail->SMTPDebug = 2;  // Enable verbose debug output
    $mail->isSMTP();  // Send using SMTP
    $mail->Host       = '[email protected]';  // Set the SMTP server to send through
    $mail->SMTPAuth   = true;  // Enable SMTP authentication
    $mail->Username   = '*****';  // SMTP username
    $mail->Password   = '*****';  // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;  // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` for SSL
    $mail->Port       = 25;  // TCP port to connect to

    // Disable SSL certificate verification for testing
    $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );

    // Recipients
    $mail->setFrom('****@****.com', 'John');
    $mail->addAddress('****@****.com', 'Doe');

    // Content
    $mail->isHTML(true);  // Set email format to HTML
    $mail->Subject = 'email test';
    $mail->Body    = '<html>...</html>';  // HTML body

    // Send email
    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

如有需要,可采取其他步骤

  • 更新 PHPMailer:确保您拥有最新版本的 PHPMailer。

  • 安装 CA 证书:确保您的系统具有正确的 CA 证书。

  • 使用 OpenSSL 测试连接:使用以下命令手动测试连接和 SSL 证书:

    openssl s_client -starttls smtp -connect [电子邮件受保护]:25

© www.soinside.com 2019 - 2024. All rights reserved.