【问题标题】:Wordpress.org server: PHP cannot access SMTP ports but telnet canWordpress.org 服务器:PHP 无法访问 SMTP 端口,但 telnet 可以
【发布时间】:2015-04-02 14:04:08
【问题描述】:

我一直在 CentOS 6.6 机器上设置 Wordpress,我对这些东西还很陌生。全新安装所有最新版本的 httpd、php、mySQL 和 Wordpress。

现在一切正常,除了使用 SMTP 发送电子邮件。我在 Wordpress 中安装了几个插件来配置 SMTP,但是发送测试电子邮件总是会导致类似 SMTP connect() failed 的错误。我已经非常确定我的凭据没有问题。

从命令行我尝试了这些:

telnet smtp.gmail.com 25

openssl s_client -connect smtp.gmail.com:465

他们两个都可以毫无问题地将我连接到 gmail 的 smtp。由于 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";
}
}

将上面的 php 脚本更改为 www.gmail.com 并巧妙地添加端口 80 和 443 会显示 80 和 443 是打开的,其余的是关闭的(这很有意义,因为我现在正在查看 www 服务器),所以脚本似乎工作正常。

我猜这一定是 PHP 本身的问题,不允许我通过任何 SMTP 端口(因为我可以从 cmdline 远程登录到这些端口)

我无法弄清楚 PHP 中的问题可能是什么。任何人都知道如何解决这个问题?

【问题讨论】:

  • 发现每次刷新php测试页面,audit.log都会有新的条目进入:
  • type=SYSCALL msg=audit(1422965981.489:1703803): arch=c000003e syscall=42 成功=没有退出=-13 a0=1f a1=7f12c44a0ef8 a2=10 a3=7f12c005514c items=0 ppid= 53588 pid=53590 auid=0 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=492 comm="httpd" exe="/usr/ sbin/httpd" subj=unconfined_u:system_r:httpd_t:s0 key=(null) type=AVC msg=audit(1422965981.489:1703804): avc: denied { name_connect } for pid=53590 comm="httpd" dest=465 scontext= unconfined_u:system_r:httpd_t:s0 tcontext=system_u:object_r:smtp_port_t:s0 tclass=tcp_socket

标签: php wordpress apache smtp centos6


【解决方案1】:

找到我自己的答案 :) 每次我尝试从 PHP 访问任何 SMTP 端口时,audit.log 都会输入条目这一事实敲响了警钟:SELinux 是罪魁祸首。

暂时禁用 SELinux 使其再次工作:

echo 0 >/selinux/enforce

证明有效后,我重新启用了 SELinux:

echo 1 >/selinux/enforce

...最后我发现这个 cmd 会将 SELinux 配置为允许通过 apache/PHP 进行 SMTP:

setsebool -P httpd_can_network_connect on

【讨论】:

    【解决方案2】:

    终于!我花了至少 8 个小时来排查为什么我可以使用 shell 而不是 WordPress 来发送邮件。这解决了我的问题!

     $ sudo setsebool -P httpd_can_sendmail 1
    

    【讨论】:

      猜你喜欢
      • 2015-03-22
      • 2013-03-13
      • 1970-01-01
      • 2017-09-06
      • 1970-01-01
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多