-
-
/**
- * 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('郵件地址或密碼為空', 1001);
- 回傳 false;
- }
- if (!PReg_match("/^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+ $/i", $ this->strEmail))
- {
- $this->setMessage('電子郵件地址無效', 1002);
- 回傳 false;
- }
- if ($this->strHost=='')
- {
- $this->strHost = substr(strrchr($this->strEmail, "@ "), 1);
- }
- if ($intPort!='')
- {
- $this->intPort = $intPort;
- }
- $this->connectHost ();
- }
//連接伺服器
- function connectHost()
- {
- if ($this->bolDebug)
- {
- echo "連接".$this->strHost." ... rn」;
- }
- if (!$this->getIsConnect())
- {
- if ($this->strHost =='' || $this->intPort=='' )
- {
- $this->setMessage('POP3 主機或連接埠為空', 1003)
- 回傳false;
-
- $this->resHandler = @fsockopen($this->strHost, $this->intPort, &$this->intErrorNum, &$this->strMessage, $this->intConnSecond)
- if (!$ this->resHandler)
- {
- $strErrMsg = '連接POP3 主機:'.$this->strHost.'失敗的'
- $intErrNum = 2001
- $this- >setMessage($strEsgMessage($strEsgMessage($strEsgMessage($strEsgMessage($strEsg , $intErrNum);
- 回傳false;
- }
- $this->getLineResponse();
- if (!$this->getRestIsSucceed())
- {
- return false;
- }
- }
- 回傳true;
- }
- // 關閉連線
- function closeHost()
- {
- if ($this-> resHandler)
- {
- if ($this-> resHandler)
- {
- fclose($this->resHandler) ;
- }
- 回傳 true;
- }
- // 傳送指令
- function sendCommand($strCommand)
- {
- if ($this->bolDebug)
- {
- if (!preg_match("/ PASS/", $strCommand))
- {
- echo "發送指令: ".$strCommand."rn";
- }
- else
- {
- echo "發送指令:PASS ******rn";
- }
- }
- if (!$this->getIsConnect())
- {
- return false;
- }
- if (trim($strCommand)=='')
- {
- $this->setMessage('請求指令為空', 1004);
- 回傳 false;
- }
- $this->strRequest = $strCommand."rn";
- $this->arrRequest[] = $strCommand;
- fputs($this->resHandler, $this->strRequest);
- 回傳true;
- }
- // 擷取回應資訊第一行
- function getLineResponse()
- {
- if (!$this->getIsConnect())
- {
- 回傳false ;
- }
- $this->strResponse = fgets($this->resHandler, $this->intBuffSize);
- $this->arrResponse[] = $this->strResponse;
- return $this->strResponse; }
- //擷取若干回應訊息,$intReturnType是傳回值型別, 1為字串, 2為陣列
- 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> }
- $strAllResponse .= $strLineResponse }🎟 > return $strAllResponse;
- }
- else
- {
- $arrAllResponse = array();
- while(!feof($this->resHandler))
- {
- {
- $ strLineResponse = $this->getLineResponse();
- if (preg_match("/^+OK/", $strLineResponse))
- {
- continue;
- }
- if (trimue($ strLineResponse)=='.')
- {
- break;
- }
- $arrAllResponse[] = $strLineResponse;
- }
- return $arrAllResponse; }
- //提取請求是否成功
- function getRestIsSucceed($strRespMessage='')
- {
- if (trim($responseMessage)=='')
- {
- if (> if ( $this->strResponse=='')
- {
- $this->getLineResponse();
- }
- $strRespMessage = $this->strResponse;
- }
- if (> if trim($strRespMessage)=='')
- {
- $this->setMessage('Response message is empty', 2003);
- return false;
- }
- if ( ("/^+OK/", $strRespMessage))
- {
- $this->setMessage($strRespMessage, 2000);
- return false;
- }
- return true; > }
- //取得是否已連接
- function getIsConnect()
- {
- if (!$this->resHandler)
- {
- $this->setMessage("Nonexistent availability connection handler", 2002);
- return false;
- }
- return true;
- }
//設定訊息
- function setMessage($strMessage,$str $intErrorNum)
- {
- if (trim($strMessage)=='' || $intErrorNum=='')
- {
- return false;
- }
- $this- >strMessage = $strMessage;
- $this->intErrorNum = $intErrorNum;
- return true;
- }
- //取得訊息
- function getMessage()
- return
- return> $this->strMessage;
- }
- //取得錯誤號碼
- function getErrorNum()
- {
- return $this->intErrorNum;
- }
- //取得請求訊息
- function getRequest()
- {
- return $this->strRequest;
- }
- //取得回應資訊
- function getResponse()
- {
- {
- return $ this->strResponse;
- }
//---------------
- // 郵件原子操作
- //- --------------
- //登入信箱
- function popLogin()
- {
- if (!$this->getIsConnect())
- {
- return false;
- }
- $this->sendCommand("USER ".$this->strEmail);
- $this->getLineResponse();
- $bolUserRight = $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;
- }
- //退出登入
- function popLogout()
- {
- if (!$this->getIsConnect() & $& $ this->bolIsLogin)
- {
- return false;
- }
- $this->sendCommand("QUIT");
- $this->getLineResponse();
- if (! $this->getRestIsSucceed())
- {
- return false;
- } return true; }
- //取得是否在線
- function getIsOnline()
- {
- if (!$this->getIsConnect() && $this->bolIsLogin)
- {
- return false;
- }
- $this->sendCommand("NOOP");
- $this->getLineResponse();
- if (!$this->getRestIsSucceed())
- {
- return false;
- }
- return true;
- }
- //取得郵件數量與位元組數(回傳陣列)
- 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) {
- $this->setMessage('STAT command response message is error', 2006);
- return falsese;
- }
- return array($arrResponse[1], $arrResponse[2]);
- }
- }
- //取得指定郵件得到session Id
- function getMailSessId($intint, $intReturnType, $intReturnType, $intReturnType. )
- {
- 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;
- }
- {
- return false;
- }
- Typeif ($intReturnTypeif ($intReturnType == 1)
- {
- return $this->strResponse;
- }
- else
- {
- $arrResponse = explode(" ", $this->strResponse); > if (!is_array($arrResponse) || count($arrResponse) {
- $this->setMessage('UIDL command response message is error', 2006); return false ;
- }
- return array($arrResponse[1], $arrResponse[2]);
- }
- }
- //取得某個郵件的大小
- 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(' ', $explode(' ', $explode(' ', $explode(' ', $explode('' this->strResponse);
- return array($arrMessage[1], $arrMessage[2]);
- }
- }
- //取得郵件基本清單陣列
- function getMailBaseList($ intReturnType=2)
- {
- if (!$this->getIsConnect() && $this->bolIsLogin)
- {
- return false;
- }
- $this->s> return false;
- }
- $this->s> return false;
- }
- $this->s> return false;
- }
- $this->s> return false;
- }
- $this->s> return false;
- }
- $this->s> 返回>s ("LIST");
- $this->getLineResponse();
- if (!$this->getRestIsSucceed())
- {
- return false;
- }
- {
- return false;
- }
- return $ this->getRespMessage($intReturnType);
- }
- //取得指定郵件所有訊息,intReturnType是傳回值類型,1是字串,2是陣列
- 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 false; } return $this ->getRespMessage($intReturnType); }
- //取得某郵件前指定行, $intReturnType 回傳值類型,1是字串,2是數組
- 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);
- }
- //刪除郵件
- 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;
- }
- }
- //重做置被刪除得郵件標記為未刪除
- function resetDeleMail()
- {
- if (!$this->getIsConnect() && $this->bolIsLogin)
- {
- return false;
- }
- $this->sendCommand("RSET");
- $this->getLineResponse();
- if (!$this->getRestIsSucceed())
- {
- return false;
- }
- return true;
- }
- //---------------
- // 偵錯操作
- //- --------------
- //輸出物件資訊
- function printObject()
- {
- print_r($this);
- exit;
- }
- //輸出錯誤訊息
- function printError()
- {
- echo "[Error Msg] : $strMessage
n";
- echo "[Error Num] : $intErrorNum $intErrorNum
n";
- exit;
- }
- //輸出主機資訊
- function printHost()
- {
- echo "[Host] : $this->strHost
n";
- echo "[Port] : $this->intPort
n";
- echo "[Email] : $this->strEmail
n";
- echo "[ Passwd] : ********
n";
- exit;
- }
- //輸出連線資訊
- function printConnect()
- {
- echo " [Connect] : $this->resHandler
n";
- echo "[Request] : $this->strRequest
n";
- echo "[Response] : $this->strResponse n";
- exit;
- }
- }
?>
複製代碼
呼叫範例:
-
-
-
-
- //測試代碼
- //例如:$o = SocketPOP3Client('信箱位址', '密碼', 'POP3伺服器', 'POP3連接埠')
- /*
- 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();
*/ ?>
複製程式碼 |