View Full Version : Get Email from POP

10-12-2009, 10:54 PM
I have the following script:

$fp = fsockopen('pop.secureserver.net', 995, $erno, $estr, 10); // Connect
echo 'Open: '.fgets($fp, 128);
fwrite($fp, "myemailaddress", 128); // Enter username
echo '<br />User: '.fgets($fp, 128);
fwrite($fp, "myemailpassword", 128); // Enter password
echo '<br />Pass: '.fgets($fp, 128);
fwrite($fp, "STAT\n", 128);
echo '<br />Stat: '.fgets($fp, 128); // Get some message information

fwrite($fp, "RETR 1\n", 32);

$octets = fgets($fp, 64); // read the first line
$parts = explode(' ', $octets); // split the first line by spaces
$octets = $parts[1]; // we want the data after the second space
settype($octets, 'int'); // make sure its a number

$bytesread = 0; // we havent read any bytes of the actual email yet
$data = 'Email Message Follows: <br /><br />';

while($bytesread < $octets){

$new_data = fgets($fp, 4096); // read the data
$data .= '<br />'.$new_data; // add it onto the existing data
$byteslength = strlen($new_data);
$bytesread += $byteslength;


// The final line was just a dot (which is the ending byte string for SMTP messages (.\015\012)
// so i decided just to read that and get rid of it, so that my quit
// status message would appear properly

fread($fp, 64);

echo $data;

fwrite($fp, "QUIT\n", 128);
echo 'Quit Status: '.fgets($fp, 128);



However, when I run this I don't get any emails showing to the screen even though I know I have one unread one in there. Any ideas?

Thanks in advance.

10-12-2009, 11:40 PM
Let me tell you what I'm trying to do. I'm trying to run a script that will query a mailbox I designate. If there is new mail I want it to parse out the subject and if there is an attachment. I'd like to store the attachment in a folder on the webserver and then do some SQL. I'm using a shared environment so IMAP isn't installed because it's a security issue as I'm told. Any ideas on how I could do this? I think using the fsockopen is the only way to check this but I've never used this before so need some help with it.


10-13-2009, 03:29 PM
Is there any other way to do this without using IMAP? So I can try and google those methods?

10-13-2009, 03:53 PM
Ok so I found this snippet:

$fp=fsockopen("pop.secureserver.net", 995, $errno, $errstr, 30);
echo 'Connected!<br>';

$username="USER email@email.com\r\n";
$password="PASS password\r\n";

$us=fwrite($fp, $username, strlen($username));
echo "The username didn't send>";
echo 'Username sent, server response: '.$ur.'<br>';
$ps=fwrite($fp, $password, strlen($password));
echo "The password didn't send<br>";
echo 'Password sent, server response: '.$pr.'<br>';

$parts=explode(" ", $res);
echo $parts[4].' messages on server<br><br>';

$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>';
echo 'Failed Connecting!<br>';

However, it won't send the username and password to the server. Any ideas?