Stooney Posted August 2, 2008 Share Posted August 2, 2008 So I am able to connect to the pop3 server, log in, and check how many messages there are. I am having a problem with server responses though. In the following code, I don't understand why on the marked line I get a response. Right after I get the server response after sending the password I am able to fgets() and receive the info as if i had just sent the 'stat' command. But I didn't send it at all. Is this just an automated response I should expect, or a setting with the server? Also, I never seem to get a response from the server when sending the 'list' command. I'm assuming I don't get a response because the browser just waits until timeout at that part of the script. I am new to sockets and decided to learn them using pop3 as a project. Any help is appreciated. <?php $fp=fsockopen("pop.1and1.com", 110, $errno, $errstr, 30); if($fp){ echo 'Connected!<br>'; $username="USER [email protected]\r\n"; $password="PASS mypassword\r\n"; $us=fwrite($fp, $username, strlen($username)); $ur=fgets($fp); echo 'Username sent, server response: '.$ur.'<br>'; $ps=fwrite($fp, $password, strlen($password)); $pr=fgets($fp); echo 'Password sent, server response: '.$pr.'<br>'; //Question refers to this line $res=fgets($fp); $parts=explode(" ", $res); echo $parts[4].' messages on server<br><br>'; //I don't get a response from this part. $get=fwrite($fp, 'list'); $msg=fgets($fp); echo $msg; } else{ echo 'Failed Connecting!<br>'; } ?> Link to comment https://forums.phpfreaks.com/topic/117785-solved-pop3-and-fsockopen/ Share on other sites More sharing options...
unkwntech Posted August 2, 2008 Share Posted August 2, 2008 I'm not familiar with the POP3 protocol but this may be more related, check to see if there is a command to see what is supported, it could be possible that the 'list' command is not supported by the server. Link to comment https://forums.phpfreaks.com/topic/117785-solved-pop3-and-fsockopen/#findComment-605949 Share on other sites More sharing options...
Stooney Posted August 2, 2008 Author Share Posted August 2, 2008 Hopefully this gives more information. In this example I am attempting to replicate the telnet connection i list with php. Telnet (works great): +OK POP server ready H mius1 user [email protected] +OK password required for user "[email protected]" pass mypassword +OK mailbox "[email protected]" has 1 messages (1025 octets) H mius1 list +OK 1 1025 . php script <?php $fp=fsockopen("pop.1and1.com", 110, $errno, $errstr, 30); if($fp){ echo 'Connected!<br>'; $username="USER [email protected]\r\n"; $password="PASS mypassword\r\n"; $us=fwrite($fp, $username, strlen($username)); $ur=fgets($fp); echo 'Username sent, server response: '.$ur.'<br>'; $ps=fwrite($fp, $password, strlen($password)); $pr=fgets($fp); echo 'Password sent, server response: '.$pr.'<br>'; $res=fgets($fp); $parts=explode(" ", $res); echo $parts[4].' messages on server<br><br>'; //Everything up to here works as expected, the following is where my problem lies //I used stream_get_contents here because I need to get more than 1 line of data //and php.net said it's better than fread() in this case. $get=fwrite($fp, "list\r\n"); //The following line is where the browser sits and times out. As far as I know //I should be getting a response here. $msg=stream_get_contents($fp); echo $msg; } else{ echo 'Failed Connecting!<br>'; } ?> Here is the output from the php script. This is with the problem line (stream_get_contents) commented out to prevent the page timeout. Connected! Username sent, server response: +OK POP server ready H mius9 Password sent, server response: +OK password required for user "[email protected]" 1 messages on server Link to comment https://forums.phpfreaks.com/topic/117785-solved-pop3-and-fsockopen/#findComment-606375 Share on other sites More sharing options...
Stooney Posted August 3, 2008 Author Share Posted August 3, 2008 Any chance a moderator could edit out my email address in the very tops code tags? Thought I edited it all out but I guess not, too much time passed so I can't. (change to [email protected] if you would) Link to comment https://forums.phpfreaks.com/topic/117785-solved-pop3-and-fsockopen/#findComment-606430 Share on other sites More sharing options...
Stooney Posted August 3, 2008 Author Share Posted August 3, 2008 Ok. The above problems have been worked out. Now I have yet another question. In telnet the command 'RETR 1' properly retrieves the email and displays the message. The same command in the script only returns '+OK 1027 octets' (which is only the first line of the server response) What function should I use to retrieve the entire message rather than just the first line? (assuming this is my problem) Here's the current code: <?php $fp=fsockopen("pop.1and1.com", 110, $errno, $errstr, 30); if($fp){ echo 'Connected!<br>'; $username="USER [email protected]\r\n"; $password="PASS mypassword\r\n"; $us=fwrite($fp, $username, strlen($username)); $ur=fgets($fp); echo 'Username sent, server response: '.$ur.'<br>'; $ps=fwrite($fp, $password, strlen($password)); $pr=fgets($fp); echo 'Password sent, server response: '.$pr.'<br>'; $res=fgets($fp); $parts=explode(" ", $res); echo $parts[4].' messages on server<br><br>'; $cmd="LIST\r\n"; $get=fwrite($fp, $cmd, strlen($cmd)); $msg=fread($fp, 8192); echo '<pre>'.$msg.'</pre>'; $cmd="RETR 1\r\n"; $get=fwrite($fp, $cmd, strlen($cmd)); $msg=fread($fp, 8192); //This is only retrieving the first line it seems. The server 'should' have sent an entire message worth of data. echo '<pre>'.$msg.'</pre>'; } else{ echo 'Failed Connecting!<br>'; } fclose($fp); ?> and it's output: Connected! Username sent, server response: +OK POP server ready H mius9 Password sent, server response: +OK password required for user "[email protected]" 1 messages on server +OK 1 1027 . +OK 1027 octets Link to comment https://forums.phpfreaks.com/topic/117785-solved-pop3-and-fsockopen/#findComment-606450 Share on other sites More sharing options...
treeleaf20 Posted October 13, 2009 Share Posted October 13, 2009 All, I used the same code and put in my username/password but it doesn't seem to be connecting for me, any ideas why this would be the case? I put in some echo's after it tries to send the username and password and both don't seem to send to the server but it says it's connected. Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/117785-solved-pop3-and-fsockopen/#findComment-936144 Share on other sites More sharing options...
treeleaf20 Posted October 13, 2009 Share Posted October 13, 2009 Can this be marked as not Solved please so people take a look at it? Thanks. Link to comment https://forums.phpfreaks.com/topic/117785-solved-pop3-and-fsockopen/#findComment-936146 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.