fsockopen():无法连接无法使用PHP(连接超​​时)

时间:2022-07-05 18:54:35

I have this code:

我有这个代码:

$domain = 'massag.com';

$hosts = array();
$mxweights = array();
getmxrr($domain, $hosts, $mxweights);

var_dump($hosts);
var_dump($mxweights);

$host = gethostbynamel($hosts[0])[0];
var_dump($host);

$f = @fsockopen($host, 25, $errno, $errstr, 10);

if(!$f) {
    var_dump('NOT CONNECTED');
}

It is not connected to smtp server but when I use command

它没有连接到smtp服务器但是当我使用命令时

smtp:217.196.209.9

SMTP:217.196.209.9

on mxtoolbox.com it is connected.

在mxtoolbox.com上它已连接。

Am I doing something wrong with PHP code? I already tried replace $host to smtp.massag.com but not helped.

我是否在使用PHP代码做错了什么?我已经尝试将$ host替换为smtp.massag.com,但没有帮助。

1 个解决方案

#1


1  

Using dig to query the IP provided or its reverse DNS it shows that there are no MX records so errors are expected.

使用dig查询提供的IP或其反向DNS,它表明没有MX记录,因此预计会出现错误。

dig -x 217.196.209.9 MX | grep 'IN.*MX'
;9.209.196.217.in-addr.arpa.    IN      MX

dig smtp.miramo.cz MX | grep 'IN.*MX'
;smtp.miramo.cz.                        IN      MX

But returns results on massag.com

但是在massag.com上返回结果

dig massag.com MX | grep 'IN.*MX'
;massag.com.                    IN      MX
massag.com.             85375   IN      MX      20 miramo3.miramo.cz.
massag.com.             85375   IN      MX      10 smtp.miramo.cz.

Finally, adding some tests to avoid unnecessary errors and using working domains

最后,添加一些测试以避免不必要的错误并使用工作域

<?php
$domain = 'massag.com';

if(getmxrr($domain, $hosts, $mxweights)){
    print_r($hosts);
    print_r($mxweights);
    if(count($hosts) > 0){
        $host = gethostbynamel($hosts[0])[0];
        print("Found host: " . $host . "\n");

        $f = fsockopen($host, 25, $errno, $errstr, 10);

        if(!$f){
            var_dump('NOT CONNECTED');
        }
    }else{
        print("no MX record found\n");
    }
}
?>

Result using tutorialspoint.com as domain:

使用tutorialspoint.com作为域的结果:

    Array
(
    [0] => ALT2.ASPMX.L.GOOGLE.com
    [1] => ASPMX.L.GOOGLE.com
    [2] => ALT1.ASPMX.L.GOOGLE.com
    [3] => ALT4.ASPMX.L.GOOGLE.com
    [4] => ALT3.ASPMX.L.GOOGLE.com
)
Array
(
    [0] => 5
    [1] => 1
    [2] => 5
    [3] => 10
    [4] => 10
)
Found host: 74.125.128.26

Using the domain provided by OP (massag.com)

使用OP提供的域名(massag.com)

    Array
(
    [0] => smtp.miramo.cz
    [1] => miramo3.miramo.cz
)
Array
(
    [0] => 10
    [1] => 20
)
Found host: 217.196.209.9

#1


1  

Using dig to query the IP provided or its reverse DNS it shows that there are no MX records so errors are expected.

使用dig查询提供的IP或其反向DNS,它表明没有MX记录,因此预计会出现错误。

dig -x 217.196.209.9 MX | grep 'IN.*MX'
;9.209.196.217.in-addr.arpa.    IN      MX

dig smtp.miramo.cz MX | grep 'IN.*MX'
;smtp.miramo.cz.                        IN      MX

But returns results on massag.com

但是在massag.com上返回结果

dig massag.com MX | grep 'IN.*MX'
;massag.com.                    IN      MX
massag.com.             85375   IN      MX      20 miramo3.miramo.cz.
massag.com.             85375   IN      MX      10 smtp.miramo.cz.

Finally, adding some tests to avoid unnecessary errors and using working domains

最后,添加一些测试以避免不必要的错误并使用工作域

<?php
$domain = 'massag.com';

if(getmxrr($domain, $hosts, $mxweights)){
    print_r($hosts);
    print_r($mxweights);
    if(count($hosts) > 0){
        $host = gethostbynamel($hosts[0])[0];
        print("Found host: " . $host . "\n");

        $f = fsockopen($host, 25, $errno, $errstr, 10);

        if(!$f){
            var_dump('NOT CONNECTED');
        }
    }else{
        print("no MX record found\n");
    }
}
?>

Result using tutorialspoint.com as domain:

使用tutorialspoint.com作为域的结果:

    Array
(
    [0] => ALT2.ASPMX.L.GOOGLE.com
    [1] => ASPMX.L.GOOGLE.com
    [2] => ALT1.ASPMX.L.GOOGLE.com
    [3] => ALT4.ASPMX.L.GOOGLE.com
    [4] => ALT3.ASPMX.L.GOOGLE.com
)
Array
(
    [0] => 5
    [1] => 1
    [2] => 5
    [3] => 10
    [4] => 10
)
Found host: 74.125.128.26

Using the domain provided by OP (massag.com)

使用OP提供的域名(massag.com)

    Array
(
    [0] => smtp.miramo.cz
    [1] => miramo3.miramo.cz
)
Array
(
    [0] => 10
    [1] => 20
)
Found host: 217.196.209.9