-
-
/** - * PHP Socket POP3类
- * 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();
//---------------
- // 基础操作
- //---------------
- //构造函数
- 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();
- }
//连接服务器
- function connectHost()
- {
- if ($this->bolDebug)
- {
- echo "Connection ".$this->strHost." ...rn";
- }
- 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;
- }
- //关闭连接
- function closeHost()
- {
- if ($this->resHandler)
- {
- fclose($this->resHandler);
- }
- return true;
- }
- //发送指令
- function sendCommand($strCommand)
- {
- if ($this->bolDebug)
- {
- if (!preg_match("/PASS/", $strCommand))
- {
- echo "Send Command: ".$strCommand."rn";
- }
- else
- {
- echo "Send Command: PASS ******rn";
- }
- }
- if (!$this->getIsConnect())
- {
- return false;
- }
- if (trim($strCommand)=='')
- {
- $this->setMessage('Request command is empty', 1004);
- return false;
- }
- $this->strRequest = $strCommand."rn";
- $this->arrRequest[] = $strCommand;
- fputs($this->resHandler, $this->strRequest);
- return true;
- }
- //提取响应信息第一行
- function getLineResponse()
- {
- if (!$this->getIsConnect())
- {
- return false;
- }
- $this->strResponse = fgets($this->resHandler, $this->intBuffSize);
- $this->arrResponse[] = $this->strResponse;
- return $this->strResponse;
- }
- //Extract some response information, $intReturnType is the return value type, 1 is a string, 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->getLineRes ponder ();
- if (preg_match("/^+OK/", $strLineResponse))
- {
- continue;
- }
- if (trim($strLineResponse)=='.')
- {
- break;
- }
- $ arrAllResponse[] = $strLineResponse;
- }
- return $arrAllResponse;
- }
- }
- //Whether the extraction request was successful
- 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;
- }
- //Get whether it is connected
- function getIsConnect()
- {
- if (!$this-> ;resHandler)
- {
- $this->setMessage("Nonexistent availability connection handler", 2002);
- return false;
- }
- return true;
- }
//Set message
- function setMessage($strMessage, $intErrorNum)
- {
- if (trim($strMessage)=='' || $intErrorNum=='')
- {
- return false;
- }
- $this->strMessage = $strMessage ;
- $this->intErrorNum = $intErrorNum;
- return true;
- }
- //Get the message
- function getMessage()
- {
- return $this->strMessage;
- }
- //Get the error number
- function getErrorNum ()
- {
- return $this->intErrorNum;
- }
- //Get request information
- function getRequest()
- {
- return $this->strRequest;
- }
- //Get response information
- function getResponse()
- {
- return $this->strResponse;
- }
//---------------
- // Email atomic operation
- //- --------------
- //Log in to 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;
- }
- //Get whether online
- function getIsOnline()
- {
- if (!$this->getIsConnect() && $this->bolIsLogin)
- {
- return false;
- }
- $this->sendCommand(" NOOP");
- $this->getLineResponse();
- if (!$this->getRestIsSucceed())
- {
- return false;
- }
- return true;
- }
- //Get the number and bytes of emails Number (return 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]);
- }
- }
- //Get 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]);
- }
- }
- //Get the size of a certain 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]);
- }
- }
- //Get the basic mail list array
- 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);
- }
- //Get all information of the specified email, intReturnType is the return value type, 1 is a string, 2 is an 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 line before a certain 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 message
- 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 deleted emails marked as not deleted
- function resetDeleMail()
- {
- if ( !$this->getIsConnect() && $this->bolIsLogin)
- {
- return false;
- }
- $this->sendCommand("RSET");
- $this->getLineResponse();
- if (!$this->getRestIsSucceed())
- {
- return false;
- }
- return true;
- }
- //---------------
- // Debug operation
- / /---------------
- //Output object information
- function printObject()
- {
- print_r($this);
- exit;
- }
- //Output error information
- 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 connection information
- function printConnect()
- {
- echo "[Connect] : $this->resHandler
n";
- echo "[Request] : $this->strRequest
n";
- echo "[ Response] : $this->strResponse
n";
- exit;
- }
- }
- ?>
-
Copy code
Call example:
-
-
- //Test code
- //For example: $o = SocketPOP3Client('Email address', 'Password', 'POP3 server', 'POP3 port')
- /*
- set_time_limit(0 );
- $o = new SocketPOPClient('abc@126.com', '123456', 'pop.126.com', '110');
- $o->popLogin();
- print_r($o- >getMailBaseList());
- print_r($o->getMailSum(1));
- print_r($o->getMailTopMessage(2, 2, 2));
- $o->popLogout();
- $o->closeHost();
- $o->printObject();
- */
- ?>
Copy code
|