Test_pop3.phpHTMLHEADTITLETestforManuelLemossPHPPOP3classTITLEHEADBODY? Require (pop3.php); $ useruser; $ passwordpasswd; $ apop0; $ pop3_connectionnewpop3_class; $ pop3_connection-hostnamemail.xiaocui.com; if ($ error $ pop3_connection-POP3
Test_pop3.php
Test for Manuel Lemos's PHP POP3 class
Require ("pop3.php ");
$ User = "user ";
$ Password = "passwd ";
$ Apop = 0;
$ Pop3_connection = new pop3_class;
$ Pop3_connection-> hostname = "mail.xiaocui.com ";
If ($ error = $ pop3_connection-> Open () = "")
{
Echo"
Connected to the POP3 server "$pop3_connection->hostname".
\ N ";
If ($ error = $ pop3_connection-> Login ($ user, $ password, $ apop) = "")
{
Echo"
User "$user" logged in.
\ N ";
If ($ error = $ pop3_connection-> Statistics (& $ messages, & $ size) = "")
{
Echo"
There are $messages messages in the mail box with a total of $size bytes.
\ N ";
$ Result = $ pop3_connection-> ListMessages ("", 0 );
If (GetType ($ result) = "array ")
{
For (Reset ($ result), $ message = 0; $ message Echo"
Message ",Key($result)," - ",$result[Key($result)]," bytes.
\ N ";
If ($ messages> 0)
{
If ($ error = $ pop3_connection-> RetrieveMessage (1, & $ headers, & $ body,-1) = "")
{
Echo"
Message 1:\n---Message headers starts below---
\ N ";
For ($ line = 0; $ line Echo"
",HtmlSpecialChars($headers[$line]),"
\ N ";
Echo"
---Message headers ends above---\n---Message body starts below---
\ N ";
For ($ line = 0; $ line Echo"
",HtmlSpecialChars($body[$line]),"
\ N ";
Echo"
---Message body ends above---
\ N ";
}
}
If ($ error = "" & ($ error = $ pop3_connection-> Close () = "")
Echo"
Disconnected from the POP3 server "$pop3_connection->hostname".
\ N ";
}
Else
$ Error = $ result;
}
}
}
If ($ error! = "")
Echo "Error:", HtmlSpecialChars ($ error ),"";
?>
Pop3.php
Class pop3_class
{
Var $ hostname = "";
Var $ port = 110;
Var $ connection = 0;
Var $ state = "DISCONNECTED ";
Var $ greeting = "";
Var $ must_update = 0;
Var $ debug = 0;
Function OutputDebug ($ message)
{
Echo $ message ,"
\ N ";
}
Function GetLine ()
{
For ($ line = "";;)
{
If (feof ($ this-> connection ))
Return (0 );
$ Line. = fgets ($ this-> connection, 100 );
$ Length = strlen ($ line );
If ($ length> = 2 & substr ($ line, $ length-2, 2) = "\ r \ n ")
{
$ Line = substr ($ line, 0, $ length-2 );
If ($ this-> debug)
$ This-> OutputDebug ("<$ line ");
Return ($ line );
}
}
}
Function PutLine ($ line)
{
If ($ this-> debug)
$ This-> OutputDebug ("> $ line ");
Return (fputs ($ this-> connection, "$ line \ r \ n "));
}
Function OpenConnection ()
{
If ($ this-> hostname = "")
Return ("2 it was not specified a valid hostname ");
Switch ($ this-> connection = fsockopen ($ this-> hostname, $ this-> port )))
{
Case-3:
Return ("-3 socket cocould not be created ");
Case-4:
Return ("-4 dns lookup on hostname \" $ hostname \ "failed ");
Case-5:
Return ("-5 connection refused or timed out ");
Case-6:
Return ("-6 fdopen () call failed ");
Case-7:
Return ("-7 setvbuf () call failed ");
Default:
Return ("");
}
}
Function CloseConnection ()
{
If ($ this-> connection! = 0)
{
Fclose ($ this-> connection );
$ This-> connection = 0;
}
}
Function Open ()
{
If ($ this-> state! = "DISCONNECTED ")
Return ("1 a connection is already opened ");
If ($ error = $ this-> OpenConnection ())! = "")
Return ($ error );
$ This-> greeting = $ this-> GetLine ();
If (GetType ($ this-> greeting )! = "String"
| Strtok ($ this-> greeting ,"")! = "OK ")
{
$ This-> CloseConnection ();
Return ("3 POP3 server greeting was not found ");
}
$ This-> greeting = strtok ("\ r \ n ");
$ This-> must_update = 0;
$ This-> state = "AUTHORIZATION ";
Return ("");
}
Function Close ()
{
If ($ this-> state = "DISCONNECTED ")
Return ("no connection was opened ");
If ($ this-> must_update)
{
If ($ this-> PutLine ("QUIT") = 0)
Return ("cocould not send the QUIT command ");
$ Response = $ this-> GetLine ();
If (GetType ($ response )! = "String ")
Return ("cocould not get quit command response ");
If (strtok ($ response ,"")! = "OK ")
Return ("cocould not quit the connection:". strtok ("\ r \ n "));
}
$ This-> CloseConnection ();
$ This-> state = "DISCONNECTED ";
Return ("");
}
Function Login ($ user, $ password, $ apop)
{
If ($ this-> state! = "AUTHORIZATION ")
Return ("connection is not in AUTHORIZATION state ");
If ($ apop)
{
If ($ this-> PutLine ("APOP $ user". md5 ($ this-> greeting. $ password) = 0)
Return ("cocould not send the APOP command ");
$ Response = $ this-> GetLine ();
If (GetType ($ response )! = "String ")
Return ("cocould not get APOP login command response ");
If (strtok ($ response ,"")! = "OK ")
Return ("APOP login failed:". strtok ("\ r \ n "));
}
Else
{
If ($ this-> PutLine ("USER $ user") = 0)
Return ("cocould not send the USER command ");
$ Response = $ this-> GetLine ();
If (GetType ($ response )! = "String ")
Return ("cocould not get user login entry response ");
If (strtok ($ response ,"")! = "OK ")
Return ("User error:". strtok ("\ r \ n "));
If ($ this-> PutLine ("PASS $ password") = 0)
Return ("cocould not send the PASS command ");
$ Response = $ this-> GetLine ();
If (GetType ($ response )! = "String ")
Return ("cocould not get login password entry response ");
If (strtok ($ response ,"")! = "OK ")
Return ("Password error:". strtok ("\ r \ n "));
}
$ This-> state = "TRANSACTION ";
Return ("");
}
/* Statistics method-pass references to variables to hold the number
Messages in the mail box and the size that they take in bytes .*/
Function Statistics ($ messages, $ size)
{
If ($ this-> state! = "TRANSACTION ")
Return ("connection is not in TRANSACTION state ");
If ($ this-> PutLine ("STAT") = 0)
Return ("cocould not send the STAT command ");
$ Response = $ this-> GetLine ();
If (GetType ($ response )! = "String ")
Return ("cocould not get the statistics command response ");
If (strtok ($ response ,"")! = "OK ")
Return ("cocould not get the statistics:". strtok ("\ r \ n "));
$ Messages = strtok ("");
$ Size = strtok ("");
Return ("");
}
Function ListMessages ($ message, $ unique_id)
{
If ($ this-> state! = "TRANSACTION ")
Return ("connection is not in TRANSACTION state ");
If ($ unique_id)
$ List_command = "UIDL ";
Else
$ List_command = "LIST ";
If ($ this-> PutLine ("$ list_command $ message") = 0)
Return ("cocould not send the $ list_command command ");
$ Response = $ this-> GetLine ();
If (GetType ($ response )! = "String ")
Return ("cocould not get message list command response ");
If (strtok ($ response ,"")! = "OK ")
Return ("cocould not get the message listing:". strtok ("\ r \ n "));
If ($ message = "")
{
For ($ messages = array ();;)
{
$ Response = $ this-> GetLine ();
If (GetType ($ response )! = "String ")
Return ("cocould not get message list response ");
If ($ response = ".")
Break;
$ Message = intval (strtok ($ response ,""));
If ($ unique_id)
$ Messages [$ message] = strtok ("");
Else
$ Messages [$ message] = intval (strtok (""));
}
Return ($ messages );
}
Else
{
$ Message = intval (strtok (""));
Return (intval (strtok ("")));
}
}
Function RetrieveMessage ($ message, $ headers, $ body, $ lines)
{
If ($ this-> state! = "TRANSACTION ")
Return ("connection is not in TRANSACTION state ");
If ($ lines <0)
{
$ Command = "RETR ";
$ Arguments = "$ message ";
}
Else
{
$ Command = "TOP ";
$ Arguments = "$ message $ lines ";
}
If ($ this-> PutLine ("$ command $ arguments") = 0)
Return ("cocould not send the $ command ");
$ Response = $ this-> GetLine ();
If (GetType ($ response )! = "String ")
Return ("cocould not get message retrieval command response ");
If (strtok ($ response ,"")! = "OK ")
Return ("cocould not retrieve the message:". strtok ("\ r \ n "));
For ($ headers = $ body = array (), $ line = 0; $ line)
{
$ Response = $ this-> GetLine ();
If (GetType ($ response )! = "String ")
Return ("cocould not retrieve the message ");
Switch ($ response)
{
Case ".":
Return ("");
Case "":
Break 2;
Default:
If (substr ($ response, 0, 1) = ".")
$ Response = substr ($ response, 1, strlen ($ response)-1 );
Break;
}
$ Headers [$ line] = $ response;
}
For ($ line = 0; $ line)
{
$ Response = $ this-> GetLine ();
If (GetType ($ response )! = "String ")
Return ("cocould not retrieve the message ");
Switch ($ response)
{
Case ".":
Return ("");
Default:
If (substr ($ response, 0, 1) = ".")
$ Response = substr ($ response, 1, strlen ($ response)-1 );
Break;
}
$ Body [$ line] = $ response;
}
Return ("");
}
};
?>