php - 无法连接到 SMTP 主机

标签 php smtp phpmailer

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

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

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

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 = "xxx@xxx.it";
$mail->FromName = "XXX";
$mail->AddAddress("xxx@xxx.it");
$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";
?>

只是为了澄清上面的代码在 XAMPP (Windows) 上的工作原理。

我调试了 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;
}

看起来无法打开Socket...

更新:使用 $mail->SMTPDebug = 2;按照阿尔瓦罗的建议产生了这个输出:

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

最佳答案

操作系统 CentOS 6.3

无法发送电子邮件

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

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

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

getsebool httpd_can_sendmail
getsebool httpd_can_network_connect

此命令将返回 bool 值 on 或 off。如果它关闭,我们可以使用以下命令将其设置为打开:

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

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

关于php - 无法连接到 SMTP 主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13489037/

相关文章:

php - 如何使用 html 表单的输入根据显示的结果更新 sql 表中的字段,这些结果本身就是 sql 查询的输出?

php - 没有回复的线程 : Message-ID?

php - 在 PHPMailer 中使用变量的内容作为附件

java - 无法使用 javamail 向 Gmail 发送 HTML 电子邮件

tags - 如何在使用 Mailgun 的 SMTP 选项时控制跟踪选项和标签(即不使用他们的 API)

PHPmailer 类返回 true 但电子邮件未送达

php - 网上商店过滤器返回的行不正确

php - 将 jQuery $.post 返回数据设置为 JavaScript 变量

php - 在 PhpDocumentor 2 中使用 @package 而不是命名空间

php - 如何根据我的网站收到的电子邮件在数据库中创建记录?