Tuesday, July 31, 2018

fsockopen(): unable to connect not work with PHP (Connection timed out)

Leave a Comment

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:217.196.209.9

on mxtoolbox.com it is connected.

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

2 Answers

Answers 1

Remove @ in your call to fsockopen(); so you can see any potential errors you have happening in your configuration.

This code seems to be working fine to me.

$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'); } 

example output

Something to consider is that the getmxrr() function was not available on Windows platforms before php version 5.3.0. If you're on Windows ensure you're at least on that version of php or later.

Answers 2

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

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

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:

    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)

    Array (     [0] => smtp.miramo.cz     [1] => miramo3.miramo.cz ) Array (     [0] => 10     [1] => 20 ) Found host: 217.196.209.9 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment