PHPMailer 无法连接到 SMTP 主机

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

出现以下错误:

SMTP -> 错误:无法连接到服务器:php_network_getaddresses:getaddrinfo 失败:不知道这样的主机。 (0) SMTP 错误:无法连接到 SMTP 主机。发送此邮件时出现问题!

这是我的配置文件设置,因为我遵循了这个 PHPMailer 教程

// Configuration settings for My Site

// Email Settings
$site['from_name'] = 'Manish Dekavadiya'; // from email name
$site['from_email'] = 'manish@<my-domain>.com'; // from email address

// Just in case we need to relay to a different server,
// provide an option to use external mail server.
$site['smtp_mode'] = 'enabled'; // enabled or disabled
$site['smtp_host'] = "smtp.<my-domain>.com";
$site['smtp_port'] = 587;
$site['smtp_username'] = "manish@<my-domain>.com";
$site['smtp_password']="<password>";

并使用了邮件程序类和扩展类,如下教程中所述:

/*****sendmail.php****/

// Grab our config settings
require_once($_SERVER['DOCUMENT_ROOT'].'/config.php');

// Grab the FreakMailer class
//echo $_SERVER['DOCUMENT_ROOT'];
require_once($_SERVER['DOCUMENT_ROOT'].'/lib/MailClass.inc');

// instantiate the class
$mailer = new FreakMailer();

// Set the subject
$mailer->Subject = 'This is a test';
 $mailer->SMTPDebug = 1;
// Body
$mailer->Body = 'This is a test of my mail system!';

// Add an address to send to.
$mailer->AddAddress('[email protected]', 'Manish Dekavadiya');

if(!$mailer->Send())
{
    echo 'There was a problem sending this mail!';
}
else
{
    echo 'Mail sent!';
}
$mailer->ClearAddresses();
$mailer->ClearAttachments();

当我尝试 phpmailer 文档@examples/test_smtp_gmail_basic.php 中给出的示例时,也遇到另一个错误

SMTP -> 错误:无法连接到服务器:php_network_getaddresses: getaddrinfo 失败:没有这样的主机 已知。 (0) SMTP 错误:无法 连接到 SMTP 主机。有一个 发送此邮件时遇到问题!

所以一定是设置或配置错误。不能有代码错误。

php smtp phpmailer
5个回答
7
投票

SMTP 是否已设置?如果是这样,它是否配置为在端口 587 上侦听 smtp..com?如果服务器不是您自己设置的,那么他们侦听 mail..com 的情况并不少见。另外,尝试连接到端口 25,看看是否可以将其配置为侦听默认 smtp 端口。

无论如何,错误消息都非常清楚。主机没有响应您的连接尝试。原因可能是服务器和 PHP 中的配置错误、防火墙问题、路由问题、DNS 问题等。


4
投票

希望这对其他人有帮助,因为我正在与同样的错误作斗争。首先我得到了。我收到一条错误消息,指出无法连接()。然后我打开调试,出现了你提到的错误。对我有用的解决方案是将 smtp..com 更改为 IP 地址。

$mail->Host = 'smtp.whateverDomain.com';

$mail->Host = 'theIPaddress';

调试

$mail->SMTPDebug  = 1; // enables SMTP debug information (for testing)
                           // 1 = errors and messages
                           // 2 = messages only

3
投票

我不久前遇到了这个问题,一切检查都很好,但问题仍然存在。重新启动 httpd.service 解决了这个问题。


0
投票

在运行带有 apache webserver 的 docker 容器的计算机重新启动后出现此问题。

Apache 服务器托管的 php 代码在计算机重新启动后停止发送电子邮件。

重新启动 apache docker 容器解决了问题。


0
投票

请指定哪个 IP 地址以及如何操作?

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