Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 469
raz3r Avatar asked Nov 21 '12 08:11

raz3r


People also ask

How do I fix SMTP failed to connect to server?

Change the server's SMTP restrictions. Then, add the specified website user to the list of users who are authorized to initiate outbound SMTP connections. Edit PHPMailer's configuration settings, such as host and port. Correct DNS resolution for the mail server.

What does could not connect to SMTP host mean?

This means your web server was unable to connect to mail.be-english.es. Typically this error is returned for one of the following reasons: SMTP settings are incorrect (wrong port, security setting, incorrect host). Your web server is blocking the connection.

Can't connect to Gmail SMTP host?

By default, gmail does not allow connections from third party software. If the option “access for less secure apps” is not enabled in the gmail account, users get the error “Unable to connect to SMTP host” in their websites.

Can not connect to any SMTP server?

Check whether there is network access from CSO to the SMTP server. Check whether the firewall is blocking SMTP traffic to SMTP server or whether the ports are blocked. If the server settings and authentication settings are correct, check whether the firewall is blocking port 587 and 465 and SMTP traffic.


1 Answers

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.

like image 158
user2365804 Avatar answered Sep 19 '22 12:09

user2365804