Link to home
Start Free TrialLog in
Avatar of matiascx
matiascxFlag for China

asked on

Why fsopensock can not work in my php environment?

I am working out an order management php srcipt. On order is subscribed by customer, the php code will send email to administrator via smtp. That was written via fsockopen connecting to smtp server. Unfortunately, it does not work. I have checked the  allow_url_fopen	was On	On.
Can any epxert give me idea how to solve that?
My server is: zend ce with 5.3.9 PHP version.
Thanks!
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

What kind of internet connection is PHP using? Are you testing it from your home or something? In many cases, internet providers will block attempts to send email via SMTP in order to prevent viruses (the ones that try to send themselves without you knowing about it). If your internet provider gives you an SMTP server to use for your own email, try connecting to that one instead.

If you have control over the remote SMTP server, you can always have it use another port instead of port 25 and that can work sometimes, too.
php code will send email to administrator

Maybe you can just use the mail() function.
http://us.php.net/manual/en/function.mail.php

You might want to run this script and see if Suhosin or safe_mode is blocking your script.
<?php phpinfo();

Open in new window

Avatar of matiascx

ASKER

Dear Sir,
I was able to connect to the email server with outlook express.
It should be the php problem because the problem exist in the fsopensock failed with the same server/account information that used by outlook express.
What is more, I paste the phpinfo output for your information.
Please give more idea on this issue.

Thanks!
phpinfo-output.txt
I see no evidence of safe_mode or Suhosin (and that is good!)  But I also see this.  I am not sure it is a problem but it looks odd to me.
sendmail_path      no value      no value

Can you please run this script?
<?php
error_reporting(E_ALL);
$x = mail('Ray.Paseur@Gmail.com', 'EE Test', 'matiascx');
if (!$x) echo "MAIL FAILED";

Open in new window

Hi Matias,

On Windows, the sendmail_path isn't necessary ("sendmail_path" is for Linux, and it looks like you're using a Windows server), but the SMTP value -is- used by Windows PHP when using the mail() function. Currently, it looks like SMTP is set to localhost, so unless you're running mail services on the same server, that mail() call will probably not work.

However, the fsockopen should be opening the port just fine. When you say "it does not work", what actually happens? Does the script abort at that attempt, or does it throw an error, or does it hang, or something else? Explain in detail exactly what happens.

Also, you said you're using the same server/account information from Outlook Express. I'm assuming you're using a standard, non-authenticated mail service, so can you post your code here (if it does use a username and password, just change the values, but the server name is safe to post) ?
Hi, Ray_Paseur,
If I run the scripts you mentioned the internal server will happen.
In my environment, I use windows server 2003 with zend server CE which will not support the mail function.
BR/matiascx
Understood.  I will leave this question to someone more familiar with Windows.  You're in good hands with gr8gonzo.  You might also want to ask a moderator to add the question to the "PHP for Windows" Zone.
Hi, gr8gonzo,
I have used the email of test@gzymail.com with password: 11111111 for the account information which will be used by our php script. That account can work within outlook express to send/receive emails(Also, that mail.gzymail.com is our email server hosted by our company server).
The php code can not work in the fsockopen, the socket handler is NULL after that function call in php script.
Here, I attach the error php code for your information.


function smtp_sockopen_relay(){
            $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");
            $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
            if (!($this->sock && $this->smtp_ok())) {
                  echo("relay SOCKET open failed"); ====>Here is the error, $this->sock returns null
                  $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");
                  $this->log_write("Error: ".$errstr." (".$errno.")\n");
                  return FALSE;
            }
            $this->log_write("Connected to relay host ".$this->relay_host."\n");
            return TRUE;;
      }
Change this:
$this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");
to this:
$this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port.", " . $this->time_out . "s timeout\n");

And what does the log look like after you run it? Do you see the error values?
Hi, gr8gonzo,
I have changed the code as you tell me. But I do not know where to check the error info.
"relay SOCKET open failed" is printed in the firefox interface.
How can I check the error log?
Hi matascx,
Sorry - my last message didn't go through. I don't know how to check your error log. Can you post the full code of the class? It seems like a custom class, so there is no way for anyone to know how to check the error log without seeing the code.
Hi, gr8gonzo,
I pasted simple smtp script here for you to debug and give help.
The configuration has been done in the code.
And the fsockopen always return fail.
Can you help to check it.
Looking forward to your reply.

Thanks!
smtpmail-fsockopen-fail.zip
That zip file had different code than what you've posted - it does not contain anything about logging.

Try running this as a separate PHP file and tell us the output:
<?php
$errno = false;
$errstr = "";
if(fsockopen("mail.gzymail.com",25,$errno,$errstr))
{
	echo "Success!";
}
else
{
	echo "Failure: Error Number $errno :: $errstr";
}
?>

Open in new window

Hi,gr8gonzo

Because I found similar error happens on the fsockopen, so, i use another more simple script to do this test.

Here is the error information printed out:
"
Failure: Error Number 10061 :: No connection could be made because the target machine actively refused it.
"

Waiting for your reply.
Thanks
Hi, expert,
I have caught the packet trace via wireshark, but I can see none packet is sent out via my NIC interface.
Please be aware about this.

Thanks!
SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi, gr8gonzo,
It seems you are right!
My PC is windows XP and have two NIC card: wireline and wifi card.
I have no personal firewall installed on it.
How to check and confirm your idea?

Thanks!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi, gr8gonzo,
I have done according to your proposal. I found the wifi nic has increased when the script was running. My PC first send out dns request to the mail server, and the dns server reply with the right address. Then nothing can be seen initiated by PC connecting to mail server.
That is why the 10061 error I reported in previous comment.
On the mail server, we have see nothing, that is also the evidence, the fsockopen does not work!
Can you give more idea to check the firewall you mentioned?

Thanks!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi, gr8gonzo,
Thanks for your analysis.
The 3rd one will not be the problem, because I have found port 25 traffic was used by outlook express;
I will figure out the root cause which should be the 1st or 2nd possible cause you mentioned.
My pc has installed the macafee, but i have not seen the blocked outgoing traffic which is generated by php.
Any proposal?

Thanks!
matias, I would try to see if McAfee has a log file somewhere that you can check. Or if there's a way to disable McAfee entirely for a few minutes and try the PHP script while it is disabled...
Hi, gr8gonzo,
Sorry, I do not know where is the macafee log file.
I have checked the outbound block interface, i only see some igmp outbound was blocked by macafee.

Thanks!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi, gr8gonzo,
Yes, When I use the fsockopen to google.com using:
$server="www.google.com";
fsockopen($server, 80, $errno, $errstr, $timeout);

I have seen the packet sent out to google 80 port and also the return is ok.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!
Were you able to fix the problem?