色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

使用PHP的Socket寫的POP3類

瀏覽:2日期:2023-12-27 10:26:52

查看 POP3/SMTP 協議的時候想嘗試一下自己寫一個操作類,核心沒啥,就是使用 fsockopen ,然后寫入/接收數據,只實現了最核心的部分功能,當作是學習 Socket 操作的練手。其中參考了 RFC 2449和一個國外的簡單Web郵件系統 Uebimiau 的部分代碼,不過絕對沒有抄他滴,HOHO,絕對原創。如果你喜歡,請收藏,隨便修改,嗯,但是記得不要刪除偶類里的聲名,畢竟偶也是辛辛苦苦寫了好幾天吶。另外,歡迎自由發揮,改善或者修正這個類,希望能夠為你所用。代碼沒有認真仔細的調試,發現bug請自己修正,HOHO!

<?php /** * 類名:SocketPOPClient * 功能:POP3 協議客戶端的基本操作類 * 作者:heiyeluren <http://blog.csdn.net/heiyeshuwu> * 時間:2006-7-3 * 參考:RFC 2449, Uebimiau * 授權:BSD License */

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; }

//提取若干響應信息,$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; } 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;;; } }

//提取請求是否成功 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; }

//獲取是否已連接 function getIsConnect() { if (!$this->resHandler) { $this->setMessage('Nonexistent availability connection handler', 2002); return false; } return true; }

//設置消息 function setMessage($strMessage, $intErrorNum) { if (trim($strMessage)=='' || $intErrorNum=='') { return false; } $this->strMessage;= $strMessage; $this->intErrorNum;= $intErrorNum; return true; }

//獲取消息 function getMessage() { 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->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)<=0) { $this->setMessage('STAT command response message is error', 2006); return false; } return array($arrResponse[1], $arrResponse[2]); } }

//獲取指定郵件得Session Id 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]); } }

//取得某個郵件的大小 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]); } }

//獲取郵件基本列表數組 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); }

//獲取指定郵件所有信息,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 $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;;<br>n'; echo '[Error Num] : $intErrorNum <br>n'; exit; }

//輸出主機信息 function printHost() { echo '[Host]; : $this->strHost <br>n'; echo '[Port]; : $this->intPort <br>n'; echo '[Email] : $this->strEmail <br>n'; echo '[Passwd] : ******** <br>n'; exit; }

//輸出連接信息 function printConnect() { echo '[Connect] : $this->resHandler <br>n'; echo '[Request] : $this->strRequest <br>n'; echo '[Response] : $this->strResponse <br>n'; exit; } }

?>

<? //測試代碼 //例如:$o = SocketPOP3Client('郵箱地址', '密碼', 'POP3服務器', 'POP3端口')

/* set_time_limit(0); $o = new SocketPOPClient('[email protected]', '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(); */ ?>

http://dev.csdn.net/author/heiyeshuwu/b0fd8972a5e942ba81d6d9f5a10ec267.html

標簽: PHP
主站蜘蛛池模板: 亚洲国产欧美精品一区二区三区 | 欧美日韩高清性色生活片 | 亚欧色视频在线观看免费 | 亚洲午夜久久久久影院 | 精品欧美小视频在线观看 | 国产精品一区二区久久精品 | 久久婷五月天 | 日本特黄a级高清免费酷网 日本特黄特色 | 国产人成午夜免视频网站 | 免费播放美女一级毛片 | 美国一级毛片片aa久久综合 | 在线高清一级欧美精品 | 欧美人成在线观看ccc36 | 国产高清厕所盗摄视频 | 亚洲国内精品自在线影视 | 久久精品一区二区三区日韩 | 性xxx69xxx视频在线观看 | 国产区一区二区三 | 2021国产精品系列一区二区 | caoporen在线视频入口 | 黄色美女一级片 | 成人性欧美丨区二区三区 | 欧美另类孕交免费观看 | 亚洲精品高清视频 | 美女超爽久久久久网站 | 亚洲综合综合在线 | 中文无码日韩欧免费视频 | 亚洲特级毛片 | 亚洲国产精品成人久久 | 欧美最大成人毛片视频网站 | 国产一级做a爰片久久毛片男 | 中国一级毛片特级毛片 | 精品成人免费视频 | 亚洲精品网址 | 日产乱码精品一二三区 | 91国在线观看 | 免费国产一级 | 国产亚洲一区二区三区 | 美女操穴视频 | 91在线亚洲| 日本在线看小视频网址 |