简体   繁体   English

Wordpress.org服务器:PHP无法访问SMTP端口,但telnet可以

[英]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. 我一直在CentOS 6.6机器上设置Wordpress,在这方面我还很新。 New install so all up to date versions of httpd, php, mySQL and Wordpress. 新安装,因此所有最新版本的httpd,php,mySQL和Wordpress。

Everything now works except for sending out email using SMTP. 现在,除了使用SMTP发送电子邮件外,其他所有内容都可以正常工作。 I installed several plugins in Wordpress to configure SMTP, but sending the test email always results in an error like SMTP connect() failed. 我在Wordpress中安装了多个插件来配置SMTP,但是发送测试电子邮件始终会导致SMTP connect()失败之类的错误。 I've made more than sure my credentials are ok. 我已经做得比确定我的凭据还好。

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. 他们俩都毫无问题地将我连接到gmail的smtp。 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: 由于继续存在不允许Wordpress连接的错误,最终我决定将Wordpress排除在等式之外,然后在服务器上创建了一个测试php文件,其中包含以下代码,该代码对所有端口均返回“无响应”:

<?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. 将上面的php脚本更改为www.gmail.com并添加端口80和443会巧妙地显示80和443 ARE已打开,其余端口已关闭(这很有意义,因为我现在正在查看www服务器),因此该脚本似乎工作正常。

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) 我猜想PHP本身必须存在一些问题,不允许我通过任何SMTP端口出局(因为我可以从cmdline远程登录到这些端口)

I cannot figure out what the problem in PHP may be. 我无法弄清楚PHP中可能出现的问题。 Anyone got any pointers how to resolve this? 任何人有任何指示如何解决这个问题?

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. 找到我自己的答案了:)每当我尝试从PHP访问任何SMTP端口时,audit.log都会输入条目,这是事实:SELinux是罪魁祸首。

Temporarily disabling SELinux made it work again: 暂时禁用SELinux使它再次工作:

echo 0 >/selinux/enforce

After that proved to work, I re enabled SELinux using: 在证明有效之后,我使用以下命令重新启用了SELinux:

echo 1 >/selinux/enforce

... and finally I found that this cmd would configure SELinux to allow SMTP through apache/PHP: ...最后,我发现此cmd将配置SELinux以允许通过apache / PHP进行SMTP:

setsebool -P httpd_can_network_connect on

Finally! 最后! I spent at least 8 hours troubleshooting why I could send mail using the shell and not WordPress. 我花了至少8个小时进行故障排除,为什么我可以使用Shell而不是WordPress发送邮件。 This fixed my problem! 这解决了我的问题!

 $ sudo setsebool -P httpd_can_sendmail 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM