php PHPMailer 无法连接到 SMTP 主机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6137304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:27:24  来源:igfitidea点击:

PHPMailer could not connect to SMTP host

phpsmtpphpmailer

提问by maniclorn

getting following error:

收到以下错误:

SMTP -> ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: No such host is known. (0) SMTP Error: Could not connect to SMTP host. There was a problem sending this mail!

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

This is my config file setting as I have followed this PHPMailer tutorial

这是我的配置文件设置,因为我遵循了这个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>";

and used mailer class and an extended class as mentioned in tutorial as following:

并使用了邮件程序类和教程中提到的扩展类,如下所示:

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

/*****发送邮件.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();

also getting another error when I tried an example given in phpmailer docs @ examples/test_smtp_gmail_basic.php

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

SMTP -> ERROR: Failed to connect toserver: php_network_getaddresses: getaddrinfo failed: No such host is known. (0) SMTP Error: Could not connect to SMTP host. There was a problem sending this mail!

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

so there must be setting or configuration error. there can't be code error.

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

采纳答案by inquam

Is SMTP set up on ? And if so is it configured to listen to smtp..com on port 587? If the server is not set up by yourself it's not uncommon that they listen to mail..com instead. Also, try to connect to port 25 to see if it might be configured to listen to the default smtp port.

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

The error messages is in any case very clear. The host is not responding to your connection attempt. The reason could be missconfigurations in both the server and in PHP, firewall issues, routing issues, DNS issues etc.

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

回答by JustLearninThisStuff

Hopefully this helps someone else out there because I was fighting this same error. First I was getting. I was getting an error that said it couldn't connect(). Then I turned on the debugging and got the error you mentioned. The solution that worked for me was to change the smtp..com to the ip address.

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

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

to

$mail->Host = 'theIPaddress';

debugging

调试

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

回答by user3790024

I had this problem a while ago, everything checked out just fine but the problem persisted. A reboot of httpd.service solved it.

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