为什么 PHPMailer 无法连接到我的 SMTP 服务器

Why does PHPMailer fail to connect to my SMTP server

PHPMailer 版本:6.5.0

当我尝试将我的 PHPMailer 脚本连接到我的 SMTP 服务器时,出现此错误:

SSL loaded 2021-07-07 20:57:08 Connection: opening to mail.solninjaa.com:587, timeout=300, options=array ( 'ssl' => array ( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true, ),)
2021-07-07 20:57:08 Connection failed. Error #2: stream_socket_client(): php_network_getaddresses: getaddrinfo failed: No such host is known. [C:\xampp\htdocs\php\vendor\phpmailer\phpmailer\src\SMTP.php line 388]
2021-07-07 20:57:08 Connection failed. Error #2: stream_socket_client(): unable to connect to mail.solninjaa.com:587 (php_network_getaddresses: getaddrinfo failed: No such host is known. ) [C:\xampp\htdocs\php\vendor\phpmailer\phpmailer\src\SMTP.php line 388]
2021-07-07 20:57:08 SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: No such host is known. (0)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

第一个“已加载 SSL”是我的脚本,用于检查它是否启用了 OpenSSL。

我的邮件服务器是:

mail.solninjaa.com

注意:“mail.solninjaa.com”也是 IMAP 和 POP3 地址。

我 运行 一个命令,用于测试邮件服务器是否真的是一个邮件服务器。 它回来说不是,这很奇怪,因为我可以在我的 Webmin / Virtualmin 仪表板(它们就像 CPanel)中看到它 运行。这可能是问题所在,但我对此表示怀疑。我想我可能会输入错误的端口(或类似的东西)。

我只是觉得值得一提。

我的 PHPMailer 脚本是:

<?php

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


/* Composer autoload.php file includes all installed libraries. */
require '.\vendor\autoload.php';


$mail = new PHPMailer(TRUE);


$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

$secretKey = "pleasedontsharethis";
$responseKey = $_POST['g-recaptcha-response'];
$userIP = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";

$response = file_get_contents($url);
$response = json_decode($response);
    
    
echo (extension_loaded('openssl')?'SSL loaded':'SSL not loaded')."\n";

    if ($response->success)
    {
        try {
   
            $mail->setFrom('otheremail@gmail.com', 'SolninjaA');
            $mail->addAddress('email@gmail.com', 'Name');
            $mail->Subject = 'Testing PHPMailer 2';

            $mail->msgHTML(file_get_contents('email_template.html'), __DIR__);

            $mail->AltBody = 'There was a malfunction with sending the HTML. Sorry.';
            // $mail->isSMTP();
            // $mail->Port = 587;
            // $mail->SMTPAuth = TRUE;

            // TESTING CUSTOM STMP SERVERS
            $mail->isSMTP();
            $mail->Host = 'mail.solninjaa.com';
            $mail->SMTPAuth = TRUE;
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
            $mail->Username = 'noreply@solninjaa.com';
            $mail->Password = 'mypassword';
            $mail->Port = 587;
            $mail->SMTPAutoTLS = false;
            $mail->SMTPDebug = 3;

               /* Disable some SSL checks. */
            $mail->SMTPOptions = array(
                'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
                )
            );
            
            /* Finally send the mail. */
            $mail->send();
         }
         catch (Exception $e)
         {
            echo $e->errorMessage();
         }
         catch (\Exception $e)
         {
            echo $e->getMessage();
         }
         echo "Thank You!" . " -" . "<a href='/dist' style='text-decoration:none;color:#ff0099;'> Return Home</a> we are Automatically redirecting you in 5 seconds.";
         header("Refresh:5; url=/dist");
        }
    else {
        echo "<span style='text-decoration:none;color: red;'>Sorry, something went wrong... Invalid Captcha, please try again.</span> <a href='/dist' style='text-decoration:none;color:#ff0099;'> Return Home</a> we are Automatically redirecting you in 5 seconds.";
        header("Refresh:5; url=/dist");
    }

?>

注意:我的原始脚本已将 XOAUTH2 设置注释掉(//、#),但出于安全原因我删除了这些设置。

注意:我的脚本还需要 Google reCaptcha,这就是为什么有额外的变量。

另外,如果我的自定义 SMTP 服务器无法正常工作,我可以创建一个 Google Workspace 帐户并使用该帐户获取 XOAUTH2 令牌吗?它会显示我的自定义电子邮件地址吗? (noreply@solninjaa.com)

它无法解析mail.solninjaa.com,我也不能。

你至少有一个 DNS 问题。