php 无法连接到 SMTP 主机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13489037/
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 05:34:25  来源:igfitidea点击:

Could not connect to SMTP host

phpsmtpphpmailer

提问by raz3r

SMTP Error: Could not connect to SMTP host. Message could not be sent.

Mailer Error: SMTP Error: Could not connect to SMTP host.

SMTP 错误:无法连接到 SMTP 主机。无法发送消息。

邮件程序错误:SMTP 错误:无法连接到 SMTP 主机。

I can't seem to find a way to make PHPMailer work under CentOS. Mail work just fine under Windows with XAMPP but I always get this error under Linux.

我似乎找不到让 PHPMailer 在 CentOS 下工作的方法。邮件在 Windows 下使用 XAMPP 工作得很好,但在 Linux 下我总是收到这个错误。

The SMTP server is a Lotus Domino listening on port 25, CentOS machine has NO firewall at all and the strange thing is that even mail() does not work. It returns nothing (while on Windows returns 1). If I send an email through telnet via CentOS server it works just fine so I don't think it is a network problem. It must be related to PHP but I don't know how.

SMTP 服务器是一个 Lotus Domino 监听端口 25,CentOS 机器根本没有防火墙,奇怪的是即使 mail() 也不起作用。它不返回任何内容(而在 Windows 上返回 1)。如果我通过 CentOS 服务器通过 telnet 发送电子邮件,它工作得很好,所以我认为这不是网络问题。它必须与PHP有关,但我不知道如何。

<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "192.168.x.x";
$mail->SMTPAuth = false;
$mail->From = "[email protected]";
$mail->FromName = "XXX";
$mail->AddAddress("[email protected]");
$mail->IsHTML(true);
$mail->Subject = "Test";
$mail->Body    = "Test";
if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}
echo "Message has been sent";
?>

Just to clarify the code above works on XAMPP (Windows).

只是为了澄清上面的代码适用于 XAMPP (Windows)。

I debugged the error on PHPMailer and error happens here (class.smtp.php method Connect()):

我在 PHPMailer 上调试了错误,错误发生在这里(class.smtp.php 方法 Connect()):

$this->smtp_conn = @fsockopen($host,    // the host of the server
                             $port,    // the port to use
                             $errno,   // error number if any
                             $errstr,  // error message if any
                             $tval);   // give up after ? secs
// verify we connected properly
if(empty($this->smtp_conn)) {
  $this->error = array("error" => "Failed to connect to server",
                       "errno" => $errno,
                       "errstr" => $errstr);
  if($this->do_debug >= 1) {
    echo "SMTP -> ERROR: " . $this->error["error"] . ": $errstr ($errno)" . $this->CRLF . '<br />';
  }
  return false;
}

It looks like it can't open the Socket...

貌似打不开Socket...

UPDATE:Using $mail->SMTPDebug = 2; as suggested by Alvaro produced this output:

更新:使用 $mail->SMTPDebug = 2; 正如 Alvaro 所建议的那样产生了这个输出:

SMTP -> ERROR: Failed to connect to server: Permission denied (13)

SMTP -> 错误:无法连接到服务器:权限被拒绝 (13)

采纳答案by álvaro González

You can enable debug mode with the SMTPDebugproperty, e.g.:

您可以使用该SMTPDebug属性启用调试模式,例如:

$mail = new PHPMailer();
// 1 = errors and messages
// 2 = messages only
$mail->SMTPDebug  = 2;

Error messages will be echoed to screen.

错误消息将回显到屏幕上。

Update:

更新:

A permission deniederror message using fsockopen()suggests that the user PHP runs as is not allowed to open a socket. If you'd double-checked that there's no firewall, it's possible that's a SELinux problem:-?

一个权限被拒绝使用错误消息的fsockopen()表明,随着不允许打开一个套接字用户PHP运行。如果您仔细检查没有防火墙,那可能是SELinux 问题:-?

回答by user2365804

OS CentOS 6.3

操作系统 CentOS 6.3

Couldn't send emails

无法发送电子邮件

after some reserch turned out that SELinux is blocking the communication

经过一些研究,结果发现 SELinux 阻止了通信

SELinux is activated and configured by default. As such SELinux does not allow Apache (httpd,phpmailer) to use the sendmail function and make any sort of network connection.

SELinux 是默认激活和配置的。因此,SELinux 不允许 Apache (httpd,phpmailer) 使用 sendmail 功能并进行任何类型的网络连接。

Using the getsebool command we can check if httpd demon is allowed to make a connection over the network and send an email.

使用 getsebool 命令,我们可以检查是否允许 httpd Demon 通过网络建立连接并发送电子邮件。

getsebool httpd_can_sendmail
getsebool httpd_can_network_connect

This command will return a boolean on or off. If its off, we can set it on using the following:

此命令将返回一个布尔值 on 或 off。如果它关闭,我们可以使用以下方法设置它:

sudo setsebool -P httpd_can_sendmail 1
sudo setsebool -P httpd_can_network_connect 1

Now you can test your php, code to see if SendMail work properly or not.

现在您可以测试您的 php、代码以查看 SendMail 是否正常工作。