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