无法连接到 SMTP 主机

2022-01-17 00:00:00 smtp php phpmailer

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

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

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

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

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

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.

SMTP 服务器是一个侦听端口 25 的 Lotus Domino,CentOS 机器根本没有防火墙,奇怪的是即使 mail() 也不起作用.它什么也不返回(而在 Windows 上返回 1).如果我通过 CentOS 服务器通过 telnet 发送电子邮件,它工作得很好,所以我认为这不是网络问题.应该是和PHP相关的,但是不知道怎么弄的.

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.

<?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).

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

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

I debugged the error on PHPMailer and error happens here (class.smtp.php method 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……

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

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

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

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

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

推荐答案

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

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

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

错误消息将回显到屏幕.

Error messages will be echoed to screen.

更新:

使用 fsockopen() 的 permission denied 错误消息表明用户 PHP 运行as 是不允许打开套接字的.如果您仔细检查是否没有防火墙,则可能是 SELinux 问题 :-?

A permission denied error 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 :-?

相关文章