英文:
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 '../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}";
}
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.
<?php
$servers = array(
array("smtp.gmail.com", 465),
array("smtp.gmail.com", 587),
);
foreach ($servers as $server) {
list($server, $port) = $server;
echo "<h1>Attempting connect to <tt>$server:$port</tt></h1>\n";
flush();
$socket = fsockopen($server, $port, $errno, $errstr, 10);
if(!$socket) {
echo "<p>ERROR: $server:$portsmtp - $errstr ($errno)</p>\n";
} else {
echo "<p>SUCCESS: $server:$port - ok</p>\n";
}
flush();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论