Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
720 views
in Technique[技术] by (71.8m points)

php - Could not connect to SMTP host

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

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

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.

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";
?>

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

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;
}

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

OS CentOS 6.3

Couldn’t send emails

after some reserch turned out that SELinux is blocking the communication

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.

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_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:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...