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/