/**
- * PHP Socket POP3 class
- * By bbs.it-home.org
- */
- Class SocketPOPClient
- {
- Var $ strMessage = '';
- Var $ intErrorNum = 0;
- Var $ bolDebug = false;
Var $ strEmail = '';
- Var $ strPasswd = '';
- Var $ strHost = '';
- Var $ intPort = 110;
- Var $ intConnSecond = 30;
- Var $ intBuffSize = 8192;
- Var $ resHandler = NULL;
- Var $ bolIsLogin = false;
- Var $ strRequest = '';
- Var $ strResponse = '';
- Var $ arrRequest = array ();
- Var $ arrResponse = array ();
//---------------
- // Basic operations
- //---------------
- // Constructor
- Function SocketPOP3Client ($ strLoginEmail, $ strLoginPasswd, $ strPopHost = '', $ intPort = '')
- {
- $ This-> strEmail = trim (strtolower ($ strLoginEmail ));
- $ This-> strPasswd = trim ($ strLoginPasswd );
- $ This-> strHost = trim (strtolower ($ strPopHost ));
- If ($ this-> strEmail = ''| $ this-> strPasswd = '')
- {
- $ This-> setMessage ('email address or Passwd is empty', 1001 );
- Return false;
- }
- If (! PReg_match ("/^ [\ w-] + (\. [\ w-] +) * @ [\ w-] + (\. [\ w-] +) + $/I ", $ this-> strEmail ))
- {
- $ This-> setMessage ('email address invalid', 1002 );
- Return false;
- }
- If ($ this-> strHost = '')
- {
- $ This-> strHost = substr (strrchr ($ this-> strEmail, "@"), 1 );
- }
- If ($ intPort! = '')
- {
- $ This-> intPort = $ intPort;
- }
- $ This-> connectHost ();
- }
// Connect to the server
- Function connectHost ()
- {
- If ($ this-> bolDebug)
- {
- Echo "Connection". $ this-> strHost. "... \ r \ n ";
- }
- If (! $ This-> getIsConnect ())
- {
- If ($ this-> strHost = ''| $ this-> intPort = '')
- {
- $ This-> setMessage ('pop3 host or Port is empty', 1003 );
- Return false;
- }
- $ This-> resHandler = @ fsockopen ($ this-> strHost, $ this-> intPort, & $ this-> intErrorNum, & $ this-> strMessage, $ this-> intConnSecond );
- If (! $ This-> resHandler)
- {
- $ StrErrMsg = 'connection POP3 host: '. $ this-> strHost. 'failed ';
- $ IntErrNum = 2001;
- $ This-> setMessage ($ strErrMsg, $ intErrNum );
- Return false;
- }
- $ This-> getLineResponse ();
- If (! $ This-> getRestIsSucceed ())
- {
- Return false;
- }
- }
- Return true;
- }
- // Close the connection
- Function closeHost ()
- {
- If ($ this-> resHandler)
- {
- Fclose ($ this-> resHandler );
- }
- Return true;
- }
- // Send command
- Function sendCommand ($ strCommand)
- {
- If ($ this-> bolDebug)
- {
- If (! Preg_match ("/PASS/", $ strCommand ))
- {
- Echo "Send Command:". $ strCommand. "\ r \ n ";
- }
- Else
- {
- Echo "Send Command: PASS ****** \ r \ n ";
- }
- }
- If (! $ This-> getIsConnect ())
- {
- Return false;
- }
- If (trim ($ strCommand) = '')
- {
- $ This-> setMessage ('request command is empty', 1004 );
- Return false;
- }
- $ This-> strRequest = $ strCommand. "\ r \ n ";
- $ This-> arrRequest [] = $ strCommand;
- Fputs ($ this-> resHandler, $ this-> strRequest );
- Return true;
- }
- // Extract the first line of response information
- Function getLineResponse ()
- {
- If (! $ This-> getIsConnect ())
- {
- Return false;
- }
- $ This-> strResponse = fgets ($ this-> resHandler, $ this-> intBuffSize );
- $ This-> arrResponse [] = $ this-> strResponse;
- Return $ this-> strResponse;
- }
- // Extract response information. $ intReturnType is the return value type. 1 is a string and 2 is an array.
- Function getRespMessage ($ intReturnType)
- {
- If (! $ This-> getIsConnect ())
- {
- Return false;
- }
- If ($ intReturnType = 1)
- {
- $ StrAllResponse = '';
- While (! Feof ($ this-> resHandler ))
- {
- $ StrLineResponse = $ this-> getLineResponse ();
- If (preg_match ("/^ \ + OK/", $ strLineResponse ))
- {
- Continue;
- }
- If (trim ($ strLineResponse) = '.')
- {
- Break;
- }
- $ StrAllResponse. = $ strLineResponse;
- }
- Return $ strAllResponse;
- }
- Else
- {
- $ ArrAllResponse = array ();
- While (! Feof ($ this-> resHandler ))
- {
- $ StrLineResponse = $ this-> getLineResponse ();
- If (preg_match ("/^ \ + OK/", $ strLineResponse ))
- {
- Continue;
- }
- If (trim ($ strLineResponse) = '.')
- {
- Break;
- }
- $ ArrAllResponse [] = $ strLineResponse;
- }
- Return $ arrAllResponse;
- }
- }
- // Indicates whether the request is successfully extracted.
- Function getRestIsSucceed ($ strRespMessage = '')
- {
- If (trim ($ responseMessage) = '')
- {
- If ($ this-> strResponse = '')
- {
- $ This-> getLineResponse ();
- }
- $ StrRespMessage = $ this-> strResponse;
- }
- If (trim ($ strRespMessage) = '')
- {
- $ This-> setMessage ('response message is empty', 2003 );
- Return false;
- }
- If (! Preg_match ("/^ \ + OK/", $ strRespMessage ))
- {
- $ This-> setMessage ($ strRespMessage, 2000 );
- Return false;
- }
- Return true;
- }
- // Obtain whether the connection is established
- Function getIsConnect ()
- {
- If (! $ This-> resHandler)
- {
- $ This-> setMessage ("Nonexistent availability connection handler", 2002 );
- Return false;
- }
- Return true;
- }
// Set the message
- Function setMessage ($ strMessage, $ intErrorNum)
- {
- If (trim ($ strMessage) = ''| $ intErrorNum = '')
- {
- Return false;
- }
- $ This-> strMessage = $ strMessage;
- $ This-> intErrorNum = $ intErrorNum;
- Return true;
- }
- // Obtain the message
- Function getMessage ()
- {
- Return $ this-> strMessage;
- }
- // Get error code
- Function getErrorNum ()
- {
- Return $ this-> intErrorNum;
- }
- // Obtain request information
- Function getRequest ()
- {
- Return $ this-> strRequest;
- }
- // Obtain response information
- Function getResponse ()
- {
- Return $ this-> strResponse;
- }
//---------------
- // Mail atomic operation
- //---------------
- // Logon email
- Function popLogin ()
- {
- If (! $ This-> getIsConnect ())
- {
- Return false;
- }
- $ This-> sendCommand ("USER". $ this-> strEmail );
- $ This-> getLineResponse ();
- $ BolUserRight = $ this-> getRestIsSucceed ();
- $ This-> sendCommand ("PASS". $ this-> strPasswd );
- $ This-> getLineResponse ();
- $ BolPassRight = $ this-> getRestIsSucceed ();
- If (! $ BolUserRight |! $ BolPassRight)
- {
- $ This-> setMessage ($ this-> strResponse, 2004 );
- Return false;
- }
- $ This-> bolIsLogin = true;
- Return true;
- }
- // Log out
- Function popLogout ()
- {
- If (! $ This-> getIsConnect () & $ this-> bolIsLogin)
- {
- Return false;
- }
- $ This-> sendCommand ("QUIT ");
- $ This-> getLineResponse ();
- If (! $ This-> getRestIsSucceed ())
- {
- Return false;
- }
- Return true;
- }
- // Obtain whether or not it is online
- Function getIsOnline ()
- {
- If (! $ This-> getIsConnect () & $ this-> bolIsLogin)
- {
- Return false;
- }
- $ This-> sendCommand ("NOOP ");
- $ This-> getLineResponse ();
- If (! $ This-> getRestIsSucceed ())
- {
- Return false;
- }
- Return true;
- }
- // Obtain the number of mails and number of cells (returned array)
- Function getMailSum ($ intReturnType = 2)
- {
- If (! $ This-> getIsConnect () & $ this-> bolIsLogin)
- {
- Return false;
- }
- $ This-> sendCommand ("STAT ");
- $ StrLineResponse = $ this-> getLineResponse ();
- If (! $ This-> getRestIsSucceed ())
- {
- Return false;
- }
- If ($ intReturnType = 1)
- {
- Return $ this-> strResponse;
- }
- Else
- {
- $ ArrResponse = explode ("", $ this-> strResponse );
- If (! Is_array ($ arrResponse) | count ($ arrResponse) <= 0)
- {
- $ This-> setMessage ('stat command response message is error', 2006 );
- Return false;
- }
- Return array ($ arrResponse [1], $ arrResponse [2]);
- }
- }
- // Obtain the session Id of the specified email
- Function getMailSessId ($ intMailId, $ intReturnType = 2)
- {
- If (! $ This-> getIsConnect () & $ this-> bolIsLogin)
- {
- Return false;
- }
- If (! $ IntMailId = intval ($ intMailId ))
- {
- $ This-> setMessage ('mail message id invalid', 1005 );
- Return false;
- }
- $ This-> sendCommand ("UIDL". $ intMailId );
- $ This-> getLineResponse ();
- If (! $ This-> getRestIsSucceed ())
- {
- Return false;
- }
- If ($ intReturnType = 1)
- {
- Return $ this-> strResponse;
- }
- Else
- {
- $ ArrResponse = explode ("", $ this-> strResponse );
- If (! Is_array ($ arrResponse) | count ($ arrResponse) <= 0)
- {
- $ This-> setMessage ('uidl command response message is error', 2006 );
- Return false;
- }
- Return array ($ arrResponse [1], $ arrResponse [2]);
- }
- }
- // Obtain the size of an email.
- Function getMailSize ($ intMailId, $ intReturnType = 2)
- {
- If (! $ This-> getIsConnect () & $ this-> bolIsLogin)
- {
- Return false;
- }
- $ This-> sendCommand ("LIST". $ intMailId );
- $ This-> getLineResponse ();
- If (! $ This-> getRestIsSucceed ())
- {
- Return false;
- }
- If ($ intReturnType = 1)
- {
- Return $ this-> strResponse;
- }
- Else
- {
- $ ArrMessage = explode ('', $ this-> strResponse );
- Return array ($ arrMessage [1], $ arrMessage [2]);
- }
- }
- // Obtain the basic list of emails
- Function getMailBaseList ($ intReturnType = 2)
- {
- If (! $ This-> getIsConnect () & $ this-> bolIsLogin)
- {
- Return false;
- }
- $ This-> sendCommand ("LIST ");
- $ This-> getLineResponse ();
- If (! $ This-> getRestIsSucceed ())
- {
- Return false;
- }
- Return $ this-> getRespMessage ($ intReturnType );
- }
- // Obtain all the information of the specified Email. intReturnType indicates the return value type, 1 indicates the string, and 2 indicates the array.
- Function getMailMessage ($ intMailId, $ intReturnType = 1)
- {
- If (! $ This-> getIsConnect () & $ this-> bolIsLogin)
- {
- Return false;
- }
- If (! $ IntMailId = intval ($ intMailId ))
- {
- $ This-> setMessage ('mail message id invalid', 1005 );
- Return false;
- }
- $ This-> sendCommand ("RETR". $ intMailId );
- $ This-> getLineResponse ();
- If (! $ This-> getRestIsSucceed ())
- {
- Return false;
- }
- Return $ this-> getRespMessage ($ intReturnType );
- }
- // Get the specified row before an email, $ intReturnType return value type, 1 is a string, 2 is an array
- Function getMailTopMessage ($ intMailId, $ intTopLines = 10, $ intReturnType = 1)
- {
- If (! $ This-> getIsConnect () & $ this-> bolIsLogin)
- {
- Return false;
- }
- If (! $ IntMailId = intval ($ intMailId) |! $ IntTopLines = int ($ intTopLines ))
- {
- $ This-> setMessage ('mail message id or Top lines number invalid', 1005 );
- Return false;
- }
- $ This-> sendCommand ("TOP". $ intMailId. "". $ intTopLines );
- $ This-> getLineResponse ();
- If (! $ This-> getRestIsSucceed ())
- {
- Return false;
- }
- Return $ this-> getRespMessage ($ intReturnType );
- }
- // Delete the email
- Function delMail ($ intMailId)
- {
- If (! $ This-> getIsConnect () & $ this-> bolIsLogin)
- {
- Return false;
- }
- If (! $ IntMailId = intval ($ intMailId ))
- {
- $ This-> setMessage ('mail message id invalid', 1005 );
- Return false;
- }
- $ This-> sendCommand ("DELE". $ intMailId );
- $ This-> getLineResponse ();
- If (! $ This-> getRestIsSucceed ())
- {
- Return false;
- }
- Return true;
- }
- // Reset the deleted email to undeleted
- Function resetDeleMail ()
- {
- If (! $ This-> getIsConnect () & $ this-> bolIsLogin)
- {
- Return false;
- }
- $ This-> sendCommand ("RSET ");
- $ This-> getLineResponse ();
- If (! $ This-> getRestIsSucceed ())
- {
- Return false;
- }
- Return true;
- }
- //---------------
- // Debug the operation
- //---------------
- // Output object information
- Function printObject ()
- {
- Print_r ($ this );
- Exit;
- }
- // Output error message
- Function printError ()
- {
- Echo "[Error Msg]: $ strMessage
\ N ";
- Echo "[Error Num]: $ intErrorNum
\ N ";
- Exit;
- }
- // Output host information
- Function printHost ()
- {
- Echo "[Host]: $ this-> strHost
\ N ";
- Echo "[Port]: $ this-> intPort
\ N ";
- Echo "[Email]: $ this-> strEmail
\ N ";
- Echo "[Passwd]: ********
\ N ";
- Exit;
- }
- // Output the connection information
- Function printConnect ()
- {
- Echo "[Connect]: $ this-> resHandler
\ N ";
- Echo "[Request]: $ this-> strRequest
\ N ";
- Echo "[Response]: $ this-> strResponse
\ N ";
- Exit;
- }
- }
- ?>
Call example:
- // Test code
- // Example: $ o = SocketPOP3Client ('email address', 'password', 'pop3 server', 'pop3 Port ')
- /*
- Set_time_limit (0 );
- $ O = new SocketPOPClient ('ABC @ 126.com ', '000000', 'pop .126.com', '000000 ');
- $ O-> popLogin ();
- Print_r ($ o-> getMailBaseList ());
- Print_r ($ o-> getMailSum (1 ));
- Print_r ($ o-> getMailTopMessage (2, 2, 2 ));
- $ O-> popLogout ();
- $ O-> closeHost ();
- $ O-> printObject ();
- */
- ?>
|