Linux 如何检查端口 465 和 587 是否已使用 PHP 打开?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10127691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:45:11  来源:igfitidea点击:

How can I check if ports 465 and 587 are open with PHP?

phplinuxsmtpports

提问by cwd

I'm trying to use PHPMailer to send e-mails with SMTP and gmail. The exact script am using works on other servers but it is not working on this particular hosting company's server.

我正在尝试使用 PHPMailer 通过 SMTP 和 gmail 发送电子邮件。我使用的确切脚本适用于其他服务器,但不适用于该特定托管公司的服务器。

I have checked the phpinfo()and it tells me that allow_url_fopenis onand there are no disabled_functionslike fopenlisted.

我已经检查过了phpinfo(),它告诉我allow_url_fopenon这样,并且没有列出disabled_functions类似的内容fopen

The script fails and it tells me either:

脚本失败,它告诉我:

SMTP -> ERROR: Failed to connect to server: Connection timed out (110) 

or else

要不然

SMTP Error: Could not authenticate.

I'm assuming this is because it can not connect, because again this work on other servers and the authentication credentials are correct.

我假设这是因为它无法连接,因为这在其他服务器上再次起作用并且身份验证凭据是正确的。

So I ask more generally, is there a way I can use PHP or jailshell sshto check and see if the ports are actually open or not?

所以我更一般地问,有没有办法可以使用PHP或jailshellssh来检查端口是否实际打开?

采纳答案by netcoder

You can check for open/available ports with fsockopen:

您可以使用以下命令检查开放/可用端口fsockopen

$fp = fsockopen('127.0.0.1', 25, $errno, $errstr, 5);
if (!$fp) {
    // port is closed or blocked
} else {
    // port is open and available
    fclose($fp);
}

...where 5is the timeout in seconds until the call fails.

...5呼叫失败之前的超时时间(以秒为单位)。

This is probably due to a firewall issue where your hosting provider is blocking you from connecting to outbound sockets and/or specific ports. Keep in mind that it is a very usual security configuration to block outbound SMTP ports. Back in the day, only port 25was blocked, but I'm starting to see more and more SSL variants being blocked as well.

这可能是由于防火墙问题,您的托管服务提供商阻止您连接到出站套接字和/或特定端口。请记住,阻止出站 SMTP 端口是一种非常常见的安全配置。过去,只有端口25被阻止,但我开始看到越来越多的 SSL 变体也被阻止。

Most providers and hosting companies will only allow you to connect to their own SMTP server to prevent spammers from relaying junk mail.

大多数提供商和托管公司只允许您连接到他们自己的 SMTP 服务器,以防止垃圾邮件发送者转发垃圾邮件。