Jump to content

[SOLVED] pop3 and fsockopen


Stooney

Recommended Posts

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@edited.com\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
Share on other sites

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@edited.com
+OK password required for user "chris@stooney.com"
pass mypassword
+OK mailbox "email@edited.com" 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@edited.com\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@edited.com"
1 messages on server

Link to comment
Share on other sites

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@stooney.com\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@stooney.com"
1 messages on server

+OK
1 1027
.

+OK 1027 octets

Link to comment
Share on other sites

  • 1 year later...

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.