首頁  >  文章  >  後端開發  >  PHP Socket實作的POP3類

PHP Socket實作的POP3類

WBOY
WBOY原創
2016-07-25 08:57:481275瀏覽
  1. /**

  2. * PHP Socket POP3 類別
  3. * by bbs.it-home.org
  4. */
  5. class SocketPOPClient
  6. { var $strMessage = '';
  7. var $intErrorNum = 0;
  8. var $bolDebug = false;
  9. var $strEmail = '';

  10. var $strPasswd = '';
  11. var $strHost = '';
  12. var $intPort = 110;
  13. var $intConnSecond = 30;
  14. var $intBuffSize = 8192;
  15. var $resHandler = NULL;
  16. var $bolIsLogin = false;
  17. var $strRequest = '';
  18. var $strResponse = '';
  19. var $arrRequest = array();
  20. var $arrResponse = array();
  21. //----------------

  22. // 基礎操作
  23. / /----------------
  24. //建構子
  25. function SocketPOP3Client($strLoginEmail, $strLoginPasswd, $strPopHost='', $intPort='')
  26. {
  27. $this->strEmail = trim(strtolower($strLoginEmail));
  28. $this->strPasswd = trim($strLoginPasswd);
  29. $this->strHost = trim(strtolower($strPopHost));
  30. if ($this->strEmail=='' || $this->strPasswd=='')
  31. {
  32. $this->setMessage('郵件地址或密碼為空', 1001);
  33. 回傳 false;
  34. }
  35. if (!PReg_match("/^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+ $/i", $ this->strEmail))
  36. {
  37. $this->setMessage('電子郵件地址無效', 1002);
  38. 回傳 false;
  39. }
  40. if ($this->strHost=='')
  41. {
  42. $this->strHost = substr(strrchr($this->strEmail, "@ "), 1);
  43. }
  44. if ($intPort!='')
  45. {
  46. $this->intPort = $intPort;
  47. }
  48. $this->connectHost ();
  49. }
  50. //連接伺服器

  51. function connectHost()
  52. {
  53. if ($this->bolDebug)
  54. {
  55. echo "連接".$this->strHost." ... rn」;
  56. }
  57. if (!$this->getIsConnect())
  58. {
  59. if ($this->strHost =='' || $this->intPort=='' )
  60. {
  61. $this->setMessage('POP3 主機或連接埠為空', 1003)
  62. 回傳false;
  63. $this->resHandler = @fsockopen($this->strHost, $this->intPort, &$this->intErrorNum, &$this->strMessage, $this->intConnSecond)
  64. if (!$ this->resHandler)
  65. {
  66. $strErrMsg = '連接POP3 主機:'.$this->strHost.'失敗的'
  67. $intErrNum = 2001
  68. $this- >setMessage($strEsgMessage($strEsgMessage($strEsgMessage($strEsgMessage($strEsg , $intErrNum);
  69. 回傳false;
  70. }
  71. $this->getLineResponse();
  72. if (!$this->getRestIsSucceed())
  73. {
  74. return false;
  75. }
  76. }
  77. 回傳true;
  78. }
  79. // 關閉連線
  80. function closeHost()
  81. {
  82. if ($this-> resHandler)
  83. {
  84. if ($this-> resHandler)
  85. {
  86. fclose($this->resHandler) ;
  87. }
  88. 回傳 true;
  89. }
  90. // 傳送指令
  91. function sendCommand($strCommand)
  92. {
  93. if ($this->bolDebug)
  94. {
  95. if (!preg_match("/ PASS/", $strCommand))
  96. {
  97. echo "發送指令: ".$strCommand."rn";
  98. }
  99. else
  100. {
  101. echo "發送指令:PASS ******rn";
  102. }
  103. }
  104. if (!$this->getIsConnect())
  105. {
  106. return false;
  107. }
  108. if (trim($strCommand)=='')
  109. {
  110. $this->setMessage('請求指令為空', 1004);
  111. 回傳 false;
  112. }
  113. $this->strRequest = $strCommand."rn";
  114. $this->arrRequest[] = $strCommand;
  115. fputs($this->resHandler, $this->strRequest);
  116. 回傳true;
  117. }
  118. // 擷取回應資訊第一行
  119. function getLineResponse()
  120. {
  121. if (!$this->getIsConnect())
  122. {
  123. 回傳false ;
  124. }
  125. $this->strResponse = fgets($this->resHandler, $this->intBuffSize);
  126. $this->arrResponse[] = $this->strResponse;
  127. return $this->strResponse; }
  128. //擷取若干回應訊息,$intReturnType是傳回值型別, 1為字串, 2為陣列
  129. function getRespMessage($intReturnType)
  130. {
  131. if (!$this->getIsConnect( ))
  132. {
  133. return false;
  134. }
  135. if ($intReturnType == 1)
  136. {
  137. $strAllResponse = '';
  138. while(!feof($this(! ->resHandler))
  139. {
  140. $strLineResponse = $this->getLineResponse();
  141. if (preg_match("/^+OK/", $strLineResponse))
  142. {
  143. {
  144. continue ;
  145. }
  146. if (trim($strLineResponse)=='.')
  147. {
  148. break;
  149. }
  150. $strAllResponse .= $strLineResponse> }
  151. $strAllResponse .= $strLineResponse }🎟 > return $strAllResponse;
  152. }
  153. else
  154. {
  155. $arrAllResponse = array();
  156. while(!feof($this->resHandler))
  157. {
  158. {
  159. $ strLineResponse = $this->getLineResponse();
  160. if (preg_match("/^+OK/", $strLineResponse))
  161. {
  162. continue;
  163. }
  164. if (trimue($ strLineResponse)=='.')
  165. {
  166. break;
  167. }
  168. $arrAllResponse[] = $strLineResponse;
  169. }
  170. return $arrAllResponse; }
  171. //提取請求是否成功
  172. function getRestIsSucceed($strRespMessage='')
  173. {
  174. if (trim($responseMessage)=='')
  175. {
  176. if (> if ( $this->strResponse=='')
  177. {
  178. $this->getLineResponse();
  179. }
  180. $strRespMessage = $this->strResponse;
  181. }
  182. if (> if trim($strRespMessage)=='')
  183. {
  184. $this->setMessage('Response message is empty', 2003);
  185. return false;
  186. }
  187. if ( ("/^+OK/", $strRespMessage))
  188. {
  189. $this->setMessage($strRespMessage, 2000);
  190. return false;
  191. }
  192. return true; > }
  193. //取得是否已連接
  194. function getIsConnect()
  195. {
  196. if (!$this->resHandler)
  197. {
  198. $this->setMessage("Nonexistent availability connection handler", 2002);
  199. return false;
  200. }
  201. return true;
  202. }
  203. //設定訊息

  204. function setMessage($strMessage,$str $intErrorNum)
  205. {
  206. if (trim($strMessage)=='' || $intErrorNum=='')
  207. {
  208. return false;
  209. }
  210. $this- >strMessage = $strMessage;
  211. $this->intErrorNum = $intErrorNum;
  212. return true;
  213. }
  214. //取得訊息
  215. function getMessage()
  216. return
  217. return> $this->strMessage;
  218. }
  219. //取得錯誤號碼
  220. function getErrorNum()
  221. {
  222. return $this->intErrorNum;
  223. }
  224. //取得請求訊息
  225. function getRequest()
  226. {
  227. return $this->strRequest;
  228. }
  229. //取得回應資訊
  230. function getResponse()
  231. {
  232. {
  233. return $ this->strResponse;
  234. }
  235. //---------------

  236. // 郵件原子操作
  237. //- --------------
  238. //登入信箱
  239. function popLogin()
  240. {
  241. if (!$this->getIsConnect())
  242. {
  243. return false;
  244. }
  245. $this->sendCommand("USER ".$this->strEmail);
  246. $this->getLineResponse();
  247. $bolUserRight = $this->getLineResponse();
  248. $bolUserRight = $this- >getRestIsSucceed();
  249. $this->sendCommand("PASS ".$this->strPasswd);
  250. $this->getLineResponse();
  251. $bolPassRight = $this->getRestIsSucceed();
  252. if (!$bolUserRight || !$bolPassRight)
  253. {
  254. $this->setMessage($this->strResponse, 2004);
  255. return false;
  256. }
  257. }
  258. $ this->bolIsLogin = true;
  259. return true;
  260. }
  261. //退出登入
  262. function popLogout()
  263. {
  264. if (!$this->getIsConnect() & $& $ this->bolIsLogin)
  265. {
  266. return false;
  267. }
  268. $this->sendCommand("QUIT");
  269. $this->getLineResponse();
  270. if (! $this->getRestIsSucceed())
  271. {
  272. return false;
  273. } return true; }
  274. //取得是否在線
  275. function getIsOnline()
  276. {
  277. if (!$this->getIsConnect() && $this->bolIsLogin)
  278. {
  279. return false;
  280. }
  281. $this->sendCommand("NOOP");
  282. $this->getLineResponse();
  283. if (!$this->getRestIsSucceed())
  284. {
  285. return false;
  286. }
  287. return true;
  288. }
  289. //取得郵件數量與位元組數(回傳陣列)
  290. function getMailSum($intReturnType=2)
  291. {
  292. if (!$this->getIsConnect() && $this->bolIsLogin)
  293. {
  294. return false;
  295. }
  296. $this->sendCommand("STAT");
  297. $strLineResponse = $this->getLineResponse();
  298. if (!$this->getRestIsSucceed())
  299. {
  300. return false;
  301. }
  302. if ($intReturnType==1)
  303. {
  304. return $this->strResponse;
  305. }
  306. else
  307. {
  308. $arrResponse = explode(" ", $this->strResponse);
  309. if (!is_array( $arrResponse) || count($arrResponse) {
  310. $this->setMessage('STAT command response message is error', 2006);
  311. return falsese;
  312. }
  313. return array($arrResponse[1], $arrResponse[2]);
  314. }
  315. }
  316. //取得指定郵件得到session Id
  317. function getMailSessId($intint, $intReturnType, $intReturnType, $intReturnType. )
  318. {
  319. if (!$this->getIsConnect() && $this->bolIsLogin)
  320. {
  321. return false;
  322. }
  323. if (!$intMailId = intval( $intMailId))
  324. {
  325. $this->setMessage('Mail message id invalid', 1005);
  326. return false;
  327. }
  328. $this->sendCommand("UIDL ". $intMailId);
  329. $this->getLineResponse();
  330. if (!$this->getRestIsSucceed())
  331. {
  332. return false;
  333. }
  334. {
  335. return false;
  336. }
  337. Typeif ($intReturnTypeif ($intReturnType == 1)
  338. {
  339. return $this->strResponse;
  340. }
  341. else
  342. {
  343. $arrResponse = explode(" ", $this->strResponse); > if (!is_array($arrResponse) || count($arrResponse) {
  344. $this->setMessage('UIDL command response message is error', 2006); return false ;
  345. }
  346. return array($arrResponse[1], $arrResponse[2]);
  347. }
  348. }
  349. //取得某個郵件的大小
  350. function getMailSize($ intMailId, $intReturnType=2)
  351. {
  352. if (!$this->getIsConnect() && $this->bolIsLogin)
  353. {
  354. return false;
  355. }
  356. $this ->sendCommand("LIST ".$intMailId);
  357. $this->getLineResponse();
  358. if (!$this->getRestIsSucceed())
  359. {
  360. return false
  361. ; }
  362. if ($intReturnType == 1)
  363. {
  364. return $this->strResponse;
  365. }
  366. else
  367. {
  368. $arrMessage = explode(' ', $explode(' ', $explode(' ', $explode(' ', $explode('' this->strResponse);
  369. return array($arrMessage[1], $arrMessage[2]);
  370. }
  371. }
  372. //取得郵件基本清單陣列
  373. function getMailBaseList($ intReturnType=2)
  374. {
  375. if (!$this->getIsConnect() && $this->bolIsLogin)
  376. {
  377. return false;
  378. }
  379. $this->s> return false;
  380. }
  381. $this->s> return false;
  382. }
  383. $this->s> return false;
  384. }
  385. $this->s> return false;
  386. }
  387. $this->s> return false;
  388. }
  389. $this->s> 返回>s ("LIST");
  390. $this->getLineResponse();
  391. if (!$this->getRestIsSucceed())
  392. {
  393. return false;
  394. }
  395. {
  396. return false;
  397. }
  398. return $ this->getRespMessage($intReturnType);
  399. }
  400. //取得指定郵件所有訊息,intReturnType是傳回值類型,1是字串,2是陣列
  401. function getMailMessage($intMailId, $intReturnType= 1)
  402. {
  403. if (!$this->getIsConnect() && $this->bolIsLogin)
  404. { 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); }
  405. //取得某郵件前指定行, $intReturnType 回傳值類型,1是字串,2是數組
  406. function getMailTopMessage($intMailId, $intTopLines=10, $intReturnType=1)
  407. {
  408. if (!$this->getIsConnect() && $this->bolIsLogin)
  409. {
  410. return false;
  411. }
  412. if (!$intMailId=intval($intMailId) || ! $intTopLines=int($intTopLines))
  413. {
  414. $this->setMessage('Mail message id or Top lines number invalid', 1005);
  415. return false;
  416. }
  417. $🎜> $ this->sendCommand("TOP ". $intMailId ." ". $intTopLines);
  418. $this->getLineResponse();
  419. if (!$this->getRestIsSucceed())
  420. {
  421. return false;
  422. }
  423. return $this->getRespMessage($intReturnType);
  424. }
  425. //刪除郵件
  426. function delMail($intMailId)
  427. {
  428. {
  429. if (!$this->getIsConnect() && $this->bolIsLogin)
  430. {
  431. return false;
  432. }
  433. if (!$intMailId=intval($intMailId))
  434. {
  435. $this->setMessage('Mail message id invalid', 1005);
  436. return false;
  437. }
  438. $this->sendCommand("DELE ".$intMailId);
  439. $this ->getLineResponse();
  440. if (!$this->getRestIsSucceed())
  441. {
  442. return false;
  443. }
  444. return true;
  445. }
  446. }
  447. //重做置被刪除得郵件標記為未刪除
  448. function resetDeleMail()
  449. {
  450. if (!$this->getIsConnect() && $this->bolIsLogin)
  451. {
  452. return false;
  453. }
  454. $this->sendCommand("RSET");
  455. $this->getLineResponse();
  456. if (!$this->getRestIsSucceed())
  457. {
  458. return false;
  459. }
  460. return true;
  461. }
  462. //---------------
  463. // 偵錯操作
  464. //- --------------
  465. //輸出物件資訊
  466. function printObject()
  467. {
  468. print_r($this);
  469. exit;
  470. }
  471. //輸出錯誤訊息
  472. function printError()
  473. {
  474. echo "[Error Msg] : $strMessage
    n";
  475. echo "[Error Num] : $intErrorNum $intErrorNum
    n";
  476. exit;
  477. }
  478. //輸出主機資訊
  479. function printHost()
  480. {
  481. echo "[Host] : $this->strHost
    n";
  482. echo "[Port] : $this->intPort
    n";
  483. echo "[Email] : $this->strEmail
    n";
  484. echo "[ Passwd] : ********
    n";
  485. exit;
  486. }
  487. //輸出連線資訊
  488. function printConnect()
  489. {
  490. echo " [Connect] : $this->resHandler
    n";
  491. echo "[Request] : $this->strRequest
    n";
  492. echo "[Response] : $this->strResponse n";
  493. exit;
  494. }
  495. }
?>
複製代碼

呼叫範例:
  1. //測試代碼
  2. //例如:$o = SocketPOP3Client('信箱位址', '密碼', 'POP3伺服器', 'POP3連接埠')
  3. /*
  4. set_time_limit(0);
  5. $o = new SocketPOPClient('abc@126.com', '123456', 'pop.126.com', '110');
  6. $o->popLogin();
  7. print_r($o->getMailBaseList());
  8. print_r($o->getMailSum(1));
  9. print_r( $o->getMailTopMessage(2, 2, 2));
  10. $o->popLogout();
  11. $o->closeHost();
  12. $o->printObject();
*/
?>
複製程式碼


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn