SMTP错误:无法连接到服务器 – smtp.gmail.com

huangapple go评论62阅读模式
英文:

SMTP ERROR: Failed to connect to server - smtp.gmail.com

问题

我遇到了我的代码问题,我正在尝试使用PHPMailer发送电子邮件,但它给我以下错误:SMTP ERROR: Failed to connect to server: Network is unreachable (101)<br>SMTP Error: Could not connect to SMTP host. Failed to connect to server<br>。

我的代码如下:

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

require '../lib/PHPMailer/Exception.php';
require '../lib/PHPMailer/PHPMailer.php';
require '../lib/PHPMailer/SMTP.php';

$mail = new PHPMailer(true);
try {
    $mail->isSMTP();
    $mail->Host       = 'smtp.gmail.com';
    $mail->SMTPAuth   = true;
    $mail->Username   = 'contactDash@gmail.com';
    $mail->Password   = '*******';
    $mail->SMTPSecure = 'ssl';
    $mail->Port       = 465;
    $mail->setFrom('contactDash@gmail.com');
    $mail->addAddress('adminDashCompany@dashCompany.com.mx');
    $mail->isHTML(true);
    $mail->Subject = 'Application';
    $mail->Body    = '<h1>Application</h1>';

    $mail->send();
    $exito = 1;
} catch (Exception $e) {
    $exito = 0;
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

在本地主机上以这种方式配置时,它可以完美工作,但是当我将其上传到我的主机(cpanel)时,它会引发错误并失败,我已经在检查IP是否被阻止,更改端口和加密,但它仍然引发相同的错误。

是否有人有我可能遗漏的配置或建议?

英文:

I have a problem with my code, I am trying to send an email using PHPMailer but it gives me the following error: SMTP ERROR: Failed to connect to server: Network is unreachable (101)<br>
SMTP Error: Could not connect to SMTP host. Failed to connect to server<br>.

My code is like this:

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

    require &#39;../lib/PHPMailer/Exception.php&#39;;
    require &#39;../lib/PHPMailer/PHPMailer.php&#39;;
    require &#39;../lib/PHPMailer/SMTP.php&#39;;
    
    $mail = new PHPMailer(true);
    try {
        $mail-&gt;isSMTP();
        $mail-&gt;Host       = &#39;smtp.gmail.com&#39;;
        $mail-&gt;SMTPAuth   = true;
        $mail-&gt;Username   = &#39;contactDash@gmail.com&#39;;
        $mail-&gt;Password   = &#39;*******&#39;;
        $mail-&gt;SMTPSecure = &#39;ssl&#39;;
        $mail-&gt;Port       = 465;
        $mail-&gt;setFrom(&#39;contactDash@gmail.com&#39;);
        $mail-&gt;addAddress(&#39;adminDashCompany@dashCompany.com.mx&#39;);
        $mail-&gt;isHTML(true);
        $mail-&gt;Subject = &#39;Application&#39;;
        $mail-&gt;Body    =&#39;&lt;h1&gt;Application&lt;/h1&gt;&#39;;

        $mail-&gt;send();
        $exito = 1;
    } catch (Exception $e) {
        $exito = 0;
        echo &quot;Message could not be sent. Mailer Error: {$mail-&gt;ErrorInfo}&quot;;
    }

It works perfectly in localhost configured in this way, but when I upload it to my host (cpanel), it throws the error and fails, I'm already checking if the ip is blocked, changing ports and encryption but it throws the same error.

Will someone have a configuration present that I am missing or a recommendation?

答案1

得分: 0

你应该验证主机的端口是否开放,并消除可能存在的问题之一。

这里有一个简单的脚本,尝试通过不同的端口连接,并针对每个端口返回一个成功或错误消息。

<?php
$servers = array(
    array("smtp.gmail.com", 465),
    array("smtp.gmail.com", 587),
);

foreach ($servers as $server) {
    list($server, $port) = $server;
    echo "<h1>尝试连接到 <tt>$server:$port</tt></h1>\n";
    flush();
    $socket = fsockopen($server, $port, $errno, $errstr, 10);
    if(!$socket) {
      echo "<p>错误: $server:$port - $errstr ($errno)</p>\n";
    } else {
      echo "<p>成功: $server:$port - 正常</p>\n";
    }
    flush();
}

希望这有所帮助!

英文:

You should validate that the ports are open at your host and eliminate at least one of the possible issues.

Here's a simple script that attempts to connect through various ports, and returns a SUCCESS or ERROR message for each.

&lt;?php
$servers = array(
    array(&quot;smtp.gmail.com&quot;, 465),
    array(&quot;smtp.gmail.com&quot;, 587),
);

foreach ($servers as $server) {
    list($server, $port) = $server;
    echo &quot;&lt;h1&gt;Attempting connect to &lt;tt&gt;$server:$port&lt;/tt&gt;&lt;/h1&gt;\n&quot;;
    flush();
    $socket = fsockopen($server, $port, $errno, $errstr, 10);
    if(!$socket) {
      echo &quot;&lt;p&gt;ERROR: $server:$portsmtp - $errstr ($errno)&lt;/p&gt;\n&quot;;
    } else {
      echo &quot;&lt;p&gt;SUCCESS: $server:$port - ok&lt;/p&gt;\n&quot;;
    }
    flush();
}

huangapple
  • 本文由 发表于 2023年6月29日 07:24:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定