13
votes

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 = "[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).

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)

2
Have you tried specifying login credentials?Neil Knight
There are none. What do you mean by the way? You mean set them as ""?raz3r
FYI tryed $mail->SMTPAuth = true; $mail->Username = ""; $mail->Password = ""; just now, no luck.raz3r
Can you paste SMTP info from your php.ini file or phpinfo() ?GBD
The only two SMTP related variables that I can see with phpinfo() are SMTP (localhost) and smtp_port (25). They have the exact value on Windows as well. I tryed changing SMTP according to my actual IP but it changed nothing. Do I have to restart Apache when I change these values?raz3r

2 Answers

12
votes

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.

Update:

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

42
votes

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.