WordPress.org server: PHP cannot access SMTP ports but telnet can

I’ve been setting up WordPress on a CentOS 6.6 machine and I’m pretty new at this stuff. New install so all up to date versions of httpd, php, mySQL and WordPress.

Everything now works except for sending out email using SMTP. I installed several plugins in WordPress to configure SMTP, but sending the test email always results in an error like SMTP connect() failed. I’ve made more than sure my credentials are ok.

Read More

From the command line I tried these:

telnet smtp.gmail.com 25

and

openssl s_client -connect smtp.gmail.com:465

And both of them connect me into gmail’s smtp without issue. Because of the continuing error that WordPress is not allowed to connect, finally I decided to take WordPress out of the equation and I created a test php file on my server containing the following code that returns a “not responding” for all ports:

<?php
$host = 'smtp.gmail.com';
$ports = array(25, 465, 587);
foreach ($ports as $port)
{
$connection = @fsockopen($host, $port);

if (is_resource($connection))
{
    echo '<h2>' . $host . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') is open.</h2>' . "n";

    fclose($connection);
}

else
{
    echo '<h2>' . $host . ':' . $port . ' is not responding.</h2>' . "n";
}
}

Altering the php script above to www.gmail.com and adding ports 80 and 443 neatly shows 80 and 443 ARE open and the rest is closed (which makes sense as I’m looking at a www server now), so the script appears to be working ok.

I’m guessing it has to be some issue in PHP itself not allowing me to go out through any of the SMTP ports (as I can telnet to these ports from the cmdline)

I cannot figure out what the problem in PHP may be. Anyone got any pointers how to resolve this?

Related posts

Leave a Reply

2 comments

  1. Found my own answer 🙂 The fact the audit.log entered entries every time I tried to access any of the SMTP ports from PHP rang the bell: SELinux was the culprit.

    Temporarily disabling SELinux made it work again:

    echo 0 >/selinux/enforce
    

    After that proved to work, I re enabled SELinux using:

    echo 1 >/selinux/enforce
    

    … and finally I found that this cmd would configure SELinux to allow SMTP through apache/PHP:

    setsebool -P httpd_can_network_connect on
    
  2. Finally! I spent at least 8 hours troubleshooting why I could send mail using the shell and not WordPress. This fixed my problem!

     $ sudo setsebool -P httpd_can_sendmail 1