Solved

Why fsopensock can not work in my php environment?

Posted on 2012-04-08
30
451 Views
Last Modified: 2012-04-26
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!
0
Comment
Question by:matiascx
  • 13
  • 12
  • 3
30 Comments
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 37821458
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37821475
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

0
 

Author Comment

by:matiascx
ID: 37822830
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
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37822940
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

0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 37823005
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) ?
0
 

Author Comment

by:matiascx
ID: 37823024
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
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37823053
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.
0
 

Author Comment

by:matiascx
ID: 37823066
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;;
      }
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 37823143
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?
0
 

Author Comment

by:matiascx
ID: 37823186
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?
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 37848922
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.
0
 

Author Comment

by:matiascx
ID: 37852034
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
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 37852760
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

0
 

Author Comment

by:matiascx
ID: 37855569
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
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:matiascx
ID: 37855587
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!
0
 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 500 total points
ID: 37863980
If no packets are being sent, but you can connect to the mail server from the same machine, then it sounds like there's a firewall installed on the server and the firewall is preventing outbound communication for PHP. Can you confirm whether or not a firewall is installed, and if so, try deactivating it temporarily and retrying the test?
0
 

Author Comment

by:matiascx
ID: 37864857
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!
0
 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 500 total points
ID: 37865101
When you were checking wireshark, did you check both NICs to make sure no connection was even being -attempted- ?  Wireshark only monitors one NIC / interface at a time, if I recall correctly.

Bring up the interface monitor on wireshark that just shows the # of packets sent in realtime on each interface, and while that is up, try running your script to see if any of the interfaces are at least increasing their packet counts at all. If you see increases on one interface, then try the trace again on that interface and run your script while tracing.

Now that I re-read your message, the fact that the server actively refused it sounds more like PHP is able to reach the mail server, but when it asks the server for that port, the mail server is saying, "No!" Usually if packets can't go out at all, there's a different message about being unable to reach the server.

Does your mail server have any network logging enabled or do you have a server admin that can check the logs or watch the mail server's security logs and network interfaces while you try to connect?
0
 

Author Comment

by:matiascx
ID: 37865547
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!
0
 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 500 total points
ID: 37865926
It's hard to tell remotely, but if you have any firewalls installed at all, they have the capability of blocking outbound traffic. What confuses me is that you said that the same computer that is running the PHP script also has Outlook Express on it and is connecting to the same mail server on the same port. If that's the case, then either:

1. PHP has an internal bug in your build (you can always try connecting to google.com on port 80 just to make sure it's not a fsockopen bug)

2. There's a software application or firewall that is actively blocking outbound connections based on applications (so it is allowing Outlook Express but not PHP). I can't suggest much here except for just searching your installed programs for it. There's a lot of possibilities - McAfee, Norton or Symantec, ZoneAlarm, etc, etc... most antivirus vendors sell firewalls inside of their "internet security" products.

SMTP is a generic protocol, so a hardware firewall outside the PC would block all SMTP traffic, so Outlook Express wouldn't work, either.

3. Outlook Express isn't actually connecting on port 25 but is "updating" to a different, secure port, like 465. The best way to confirm this is to run wireshark while you make a successful connection with Outlook and look at the NUMERIC port numbers. If this is the case, then maybe all port 25 traffic IS being blocked and maybe you need to use a different port to be able to connect and authenticate to your mail server.
0
 

Author Comment

by:matiascx
ID: 37866109
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!
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 37867097
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...
0
 

Author Comment

by:matiascx
ID: 37872387
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!
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 37874878
Did you try the fsockopen call to a different server? For example, a different mail server on port 25 or www.google.com on port 80?
0
 

Author Comment

by:matiascx
ID: 37875173
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.
0
 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 500 total points
ID: 37875979
In that case, I do not think I can solve your problem after this. It sounds like you have an application firewall issue, and if I had to guess, it would be McAfee. However, McAfee has a lot of different products, so it's a little hard to narrow down the problem for you, but this is the type of thing you should be looking for:

https://kc.mcafee.com/corporate/index?page=content&id=KB50707

The instructions may be different for whatever product you have.
0
 

Author Closing Comment

by:matiascx
ID: 37896945
Thanks!
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 37897122
Were you able to fix the problem?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is about my first experience with programming Arduino.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now