tags:

views:

1323

answers:

5
+1  Q: 

php fsockopen

I have a simple php script on a server that's using fsockopen to connect to a server.

<?php
$fp = fsockopen("smtp.gmail.com", 25, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    echo fgets($fp, 1024);
    fclose($fp);
}
?>

The problem is the the script times out and fails to connect. If i change the port from 25 to 80 for example it works without problems on any host. So the problem seems to be only the port 25 no matter what host i use, i tried a lot of them and all work for port 80 and others but for 25 fails.
Connections are not blocked form firewall as if i telnet from shell it successfully connects to any port on any host.
Any idea what could be the problem as it's really weird?

LE: If i run the same php script from the shell, php scriptname.php it works so only when i run it by http it fails. I have apache with SuPHP so the problem is around here somewhere

+2  A: 

Interesting... Some firewalls can block specific program's connections to specific ports. Please check it again, try to stop firewall completely. Also try to stop any anti-spyware.

maxnk
It's a CentOS server, no anti-spyware, and i'm not aware that iptables can block only a certain program.
daniels
Ok, you right, iptables not an application firewall.So may be your ISP blocks smtp connection from their web server (it often happens). And it works with telnet because web-server process (with PHP) open connections from other IP address - thats explain all.
maxnk
And how to check it - you can try to connect to web server on test host from your php script and from telnet and take a look at logs to see the source's IP addresses for both connections. If I'm right, they will be different.
maxnk
i used telnet from shell, from the same server's shell, and it's the same ip address as the server has only one ip.
daniels
iptables can do app- and user-specific firewalling. Try asking your host.
derobert
+1  A: 

I've run into some strange issues with PHP's socket handling, too. It ended up being a problem with the system it was running on. Have you tried running your code on a different machine?

ieure
yes, on a different machine it works, but i need to figure it out how to troubleshoot on that machine.
daniels
+2  A: 

Like maxnk mentioned firewalling is the most likely issue, either on the server, or by your ISP. Port 25 is frequently firewalled as a method to prevent spam.

Just as a quick test, since you mentioned gmail, you might want to try connecting to port 587 instead. Gmail listens for smpt on this alternate port in addition to port 25 to help users bypass overly restrictive firewalls.

Zoredache
Yes, I'm able to connect using 587, but i'm trying to figure it out what's the problem using port 25. Why i am able to connect using shell but not using php? Where could the problem be? Where to look?
daniels
+1  A: 

Hi , I think the connection problem is with your machine. I just copied your code into a script on my machine(linux suse) and ran it with php -f test_script. I got the following message

220 mx.google.com ESMTP j8sm1814228gvb.0

Ronald Conco
yes, the problem is with my machine for sure, but i can't figure it out where and what to do to fix, it's something related to php suexec config
daniels
A: 

CentOS can have SELinux enabled which can cause connection weirdness. Have you checked your error logs?

Mark White