为什么 SMTP 错误?当我在本地主机上运行时它可以工作。但是当我把我的服务器 SMTP 错误。有人可以解释一下吗?

问题描述 投票:0回答:1
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

//Load Composer's autoloader
require 'vendor/autoload.php';

function send_password_reset($get_name, $get_email, $token)
{
$mail = new PHPMailer(true);

$mail->SMTPDebug  = 3;                       
$mail->isSMTP();                                
$mail->Host       = 'smtp.gmail.com';
$mail->SMTPAuth   = true;                      

$mail->Username   = '';                     
$mail->Password   = '';

$mail->SMTPSecure = 'tls';             
$mail->Port       = 587;

$mail->setFrom('[email protected]','E-TICKETING REMBANGAN');
$mail->addAddress($get_email);

$mail->isHTML(true);
$mail->Subject = 'Reset Password Notification';

$email_template = "
<html>
<head>
    <style>
        .button {
            display: inline-block;
            padding: 10px 20px;
            text-align: center;
            text-decoration: none;
            background-color: #4CAF50;
            color: white;
            border-radius: 10px;
            margin: auto;
            display: block;
            width: 200px;
            text-decoration: none; /* Removes underline */
            color: white; /* Makes text color white */
        }
        .button-container {
            text-align: center;
        }
    </style>
</head>
<body>
    <h2>Anda menerima email karena kami mendapatkan permintaan Pembaharuan password untuk akun anda pada laman <b>Wisata Rembangan</b></h2>
    <h5>Jangan bagikan link ini kepada siapapun, abaikan email ini jika bukan ada yang meminta perubahan password!</h5>
    <div class='button-container'>
        <a href='https://wisata-rembangan.mifc.myhost.id/password-change.php?token=$token&email=$get_email' class='button'>Klik Disini</a>
    </div>
</body>
</html>";

$mail->Body = $email_template;
$mail->send();
}
  • 2023-11-30 07:42:23 连接:打开 smtp.gmail.com:25,超时=300,选项=array()
  • 2023-11-30 07:42:23 连接:已打开
  • 2023-11-30 07:42:23 服务器 -> 客户端:220-odin.idserverhost.com ESMTP Exim 4.96.2 #2 Thu, 30 Nov 2023 14:42:23 +0700 220-我们不授权使用使用该系统传输未经请求的 220 和/或批量电子邮件。
  • 2023-11-30 07:42:23 客户端 -> 服务器:EHLO wisata-rembangan.mifc.myhost.id
  • 2023-11-30 07:42:23 服务器 -> 客户端:250-odin.idserverhost.com 你好 wisata-rembangan.mifc.myhost.id [103.66.86.234]250-SIZE 52428800250-8BITMIME250-PIPELINING250-PIPECONNECT250 -授权普通登录250-启动TLS250帮助
  • 2023-11-30 07:42:23 客户端 -> 服务器:STARTTLS
  • 2023-11-30 07:42:23 服务器 -> 客户端:220 TLS 继续
  • 2023-11-30 07:42:23 连接失败。错误#2:stream_socket_enable_crypto():对等证书 CN=`odin.idserverhost.com' 与预期的 CN=`smtp.gmail.com' 不匹配 [/home/mifcmyho/wisata-rembangan.mifc.myhost.id/vendor/ phpmailer/phpmailer/src/SMTP.php 第 476 行]
  • SMTP 错误:无法连接到 SMTP 主机。连接失败。 Stream_socket_enable_crypto():对等证书 CN=`odin.idserverhost.com' 与预期的 CN=`smtp.gmail.com' 不匹配
  • 2023-11-30 07:42:23 客户端 -> 服务器:退出
  • 2023-11-30 07:42:23
  • 2023-11-30 07:42:23
  • 2023-11-30 07:42:23 连接:已关闭
  • SMTP 错误:无法连接到 SMTP 主机。连接失败。 Stream_socket_enable_crypto():对等证书 CN=`odin.idserverhost.com' 与预期的 CN=`smtp.gmail.com' 不匹配

根据提供商的说法,没有任何端口被阻止。我的讲座说的是我的 php 邮件程序的版本。

有人可以告诉我出了什么问题吗?

php smtp gmail phpmailer smtp-auth
1个回答
0
投票

此错误表明 odin.idserverhost.com 提供的服务器证书与 smtp.gmail.com 的预期证书不匹配。 SSL/TLS 协商失败,因为您要连接的服务器 (odin.idserverhost.com) 不是预期的 SMTP 服务器 (smtp.gmail.com)。

以下是您可以检查或考虑的一些事项:

正确的 SMTP 服务器设置: 确保您使用的 Gmail SMTP 服务器设置正确。 Gmail 的 SMTP 和 SSL 的正确服务器是端口 465 上的 smtp.gmail.com。

验证主机名: 仔细检查您用于 SMTP 连接的主机名。它应该是 smtp.gmail.com,而不是 odin.idserverhost.com。该错误表明您要连接的服务器具有颁发给 odin.idserverhost.com 的证书,这与预期的 smtp.gmail.com 不匹配。

使用 TLS 而不是 SSL: 您可能想尝试使用 TLS,而不是使用 SSL。对于 Gmail,您应该使用 TLS 连接到端口 587。这可以在您的邮件配置中设置。

检查服务器证书: 确保 odin.idserverhost.com 的证书有效且在您的服务器上配置正确。如果证书有任何问题,可能会导致 SSL/TLS 协商失败。

防火墙和网络问题: 检查是否有任何防火墙或网络问题阻止您的服务器在所需端口上连接到 smtp.gmail.com。

调试工具: 您可以使用 OpenSSL 等工具来调试 SSL/TLS 连接并检查证书。这可以帮助您识别 SSL/TLS 握手的任何问题。

请记住避免在代码中硬编码特定服务器信息,并使用配置文件或环境变量以获得灵活性。

通过解决这些问题,您应该能够排除并解决 SMTP 连接问题。如果问题仍然存在,您可能需要联系您的托管提供商或系统管理员寻求帮助。

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