ホームページ  >  記事  >  バックエンド開発  >  PHP Socket で実装された POP3 クラス

PHP Socket で実装された POP3 クラス

WBOY
WBOYオリジナル
2016-07-25 08:57:481275ブラウズ
  1. /**

  2. * PHP ソケット POP3类
  3. * by bbs.it-home.org
  4. */
  5. class SocketPOPClient
  6. {
  7. var $strMessage = '';
  8. var $intErrorNum = 0;
  9. var $bolDebug = false;

  10. var $strEmail = '';

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

  22. //---------------

  23. // 基础操作
  24. //--------------
  25. //构造関数数
  26. function SocketPOP3Client($ strLoginEmail, $strLoginPasswd, $strPopHost='', $intPort='')
  27. {
  28. $this->strEmail = トリム(strto lower($strLoginEmail));
  29. $this->strPasswd = トリム($strLoginPasswd);
  30. $this->strHost = トリム(strto lower($strPopHost));
  31. if ($this->strEmail=='' || $this->strPasswd=='')
  32. {
  33. $this-> ;setMessage('メールアドレスまたはパスワードが空です', 1001);
  34. false を返します。
  35. }
  36. if (!PReg_match("/^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+$/i", $this-> strEmail))
  37. {
  38. $this->setMessage('メール アドレスが無効です', 1002);
  39. false を返します。
  40. }
  41. if ($this->strHost=='')
  42. {
  43. $this->strHost = substr(strrchr($this->strEmail, "@"), 1);
  44. }
  45. if ($intPort!='')
  46. {
  47. $this->intPort = $intPort;
  48. }
  49. $this->connectHost();
  50. }

  51. //接続サービス务器

  52. function connectHost()
  53. {
  54. if ($this->bolDebug)
  55. {
  56. echo "Connection ".$this->strHost." ...rn";
  57. }
  58. if (!$this->getIsConnect())
  59. {
  60. if ($this->strHost=='' || $this->intPort=='')
  61. {
  62. $this-> ;setMessage('POP3 ホストまたはポートが空です', 1003);
  63. false を返します。
  64. }
  65. $this->resHandler = @fsockopen($this->strHost, $this->intPort, &$this->intErrorNum, &$this->strMessage, $this->intConnSecond) ;
  66. if (!$this->resHandler)
  67. {
  68. $strErrMsg = '接続 POP3 ホスト: '.$this->strHost.'失敗した';
  69. $intErrNum = 2001;
  70. $this->setMessage($strErrMsg, $intErrNum);
  71. false を返します。
  72. }
  73. $this->getLineResponse();
  74. if (!$this->getRestIsSucceed())
  75. {
  76. return false;
  77. }
  78. }
  79. true を返します。
  80. }
  81. //关闭接続
  82. function closeHost()
  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. return false;
  124. }
  125. $this->strResponse = fgets($this->resHandler, $this->intBuffSize);
  126. $this->arrResponse[] = $this->strResponse;
  127. return $this->strResponse;
  128. }
  129. //応答情報を抽出します。$intReturnType は戻り値の型、1 は文字列、2 は配列です
  130. function getRespMessage($intReturnType)
  131. {
  132. if (!$this->getIsConnect())
  133. {
  134. false を返す ;
  135. }
  136. if ($intReturnType == 1)
  137. {
  138. $strAllResponse = '';
  139. while(!feof($this->resHandler))
  140. {
  141. $strLineResponse = $this->getLineResponse( );
  142. if (preg_match("/^+OK/", $strLineResponse))
  143. {
  144. 続行;
  145. if (trim($strLineResponse)=='.')
  146. {
  147. }
  148. $strAllResponse .= $strLineResponse; }
  149. return $strAllResponse; }
  150. else
  151. {
  152. $arrAllResponse = array();
  153. {
  154. $strLineResponse = $this-> ;getLineRes 熟考 ();
  155. if (preg_match("/^+OK/", $strLineResponse))
  156. {
  157. 続行;
  158. }
  159. if (trim($strLineResponse)=='.')
  160. {
  161. ブレーク; }
  162. $ arrAllResponse[] = $strLineResponse; }
  163. return $arrAllResponse; }
  164. }
  165. //抽出リクエストが成功したかどうか
  166. function getRestIsSucceed($strRespMessage='')
  167. {
  168. if (trim($responseMessage) =='')
  169. {
  170. if ($this->strResponse=='')
  171. {
  172. $this->getLineResponse(); }
  173. $strRespMessage = $this->strResponse;
  174. if (trim($strRespMessage )=='')
  175. {
  176. $this->setMessage('応答メッセージは空です', 2003);
  177. return false;
  178. }
  179. if (!preg_match("/^+OK/") , $strRespMessage))
  180. {
  181. $this->setMessage($strRespMessage, 2000);
  182. return false; }
  183. return true
  184. function getIsConnect()
  185. {
  186. if (!$this->resHandler)
  187. {
  188. $this->setMessage("存在しない可用性接続ハンドラー", 2002);
  189. return false;
  190. }
  191. return true; ; // メッセージを設定します
  192. function setMessage($strMessage, $intErrorNum)
  193. {
  194. if (trim($strMessage)=='' || $intErrorNum=='')
  195. {
  196. return false;
  197. $this- >strMessage = $strMessage ;
  198. $this->intErrorNum = $intErrorNum;
  199. return true;
  200. function getMessage()
  201. {
  202. return $this->strMessage; /エラー番号を取得
  203. function getErrorNum ()
  204. {
  205. return $this->intErrorNum;
  206. }
  207. // リクエスト情報を取得
  208. function getRequest()
  209. {
  210. return $this->strRequest
  211. }
  212. //レスポンス情報の取得
  213. function getResponse()
  214. {
  215. return $this->strResponse
  216. }

  217. //---------------

  218. / / メールのアトミック操作
  219. //- --------------
  220. //メールにログイン
  221. function PopLogin()
  222. {
  223. if (!$this->getIsConnect())
  224. {
  225. return false
  226. }
  227. $this ->sendCommand("USER ".$this->getLineResponse();
  228. $bolUserRight = $this->getRestIsSucceed();
  229. $this->sendCommand("PASS ".$this->getLineResponse();
  230. $bolPassRight = $this->getRestIsSucceed();
  231. if (!$bolUserRight | | !$bolPassRight)
  232. {
  233. $this ->setMessage($this->strResponse, 2004);
  234. $this->bolIsLogin = true;
  235. }
  236. //ログアウト
  237. function PopLogout()
  238. {
  239. if (!$this->getIsConnect() && $this->bolIsLogin)
  240. {
  241. return false; }
  242. $this->sendCommand("QUIT");
  243. $this->getLineResponse();
  244. if (!$this->getRestIsSucceed())
  245. {
  246. 戻り値
  247. }
  248. 戻り値
  249. }
  250. //オンラインかどうかを取得
  251. function getIsOnline()
  252. {
  253. if (!$this->getIsConnect() && $this->bolIsLogin)
  254. {
  255. return false
  256. }
  257. $this->sendCommand(" NOOP");
  258. $this->getLineResponse();
  259. if (!$this->getRestIsSucceed())
  260. {
  261. return false;
  262. }
  263. return true;
  264. }
  265. // の数とバイトを取得しますメール番号 (配列を返す)
  266. function getMailSum($intReturnType=2)
  267. {
  268. if (!$this->getIsConnect() && $this->bolIsLogin)
  269. {
  270. return false;
  271. $this-> ; sendCommand("STAT");
  272. $strLineResponse = $this->getLineResponse();
  273. {
  274. return false }
  275. if ($intReturnType==1) )
  276. {
  277. return $this->strResponse; }
  278. else
  279. {
  280. $arrResponse =explode(" ", $this->strResponse)
  281. if (!is_array($arrResponse) || count($arrResponse) ) {
  282. $this->setMessage('STAT コマンド応答メッセージがエラーです', 2006);
  283. return false;
  284. }
  285. return array($arrResponse[1], $arrResponse[2]) ;
  286. }
  287. }
  288. //指定されたメールのセッションIDを取得する
  289. function getMailSessId($intMailId, $intReturnType=2)
  290. {
  291. if (!$this->getIsConnect() && $this->bolIsLogin)
  292. {
  293. return false;
  294. if (!$intMailId = intval($intMailId))
  295. {
  296. $this->setMessage('メール メッセージ ID が無効です', 1005);
  297. return false; -> sendCommand("UIDL ".$intMailId);
  298. $this->getLineResponse();
  299. {
  300. return false }
  301. if ($intReturnType = = 1)
  302. {
  303. return $this->strResponse; }
  304. else
  305. {
  306. $arrResponse =explode(" ", $this->strResponse)
  307. if (!is_array($arrResponse) || count( $arrResponse) <=0)
  308. {
  309. $this->setMessage('UIDL コマンド応答メッセージがエラーです', 2006);
  310. return false;
  311. }
  312. return array($arrResponse[1], $arrResponse[2] ]);
  313. }
  314. }
  315. //特定のメールのサイズを取得する
  316. function getMailSize($intMailId, $intReturnType=2)
  317. {
  318. if (!$this->getIsConnect() && $this->bolIsLogin )
  319. {
  320. return false;
  321. }
  322. $this->sendCommand("LIST ".$intMailId);
  323. if (!$this->getRestIsSucceed());
  324. return false;
  325. }
  326. if ($intReturnType == 1)
  327. {
  328. return $this->strResponse; }
  329. else
  330. {
  331. $arrMessage =explode(' ', $this->strResponse); return array($ arrMessage[1], $arrMessage[2]);
  332. }
  333. }
  334. //基本的なメールリスト配列を取得します
  335. function getMailBaseList($intReturnType=2)
  336. {
  337. if (!$this->getIsConnect () && $this ->bolIsLogin)
  338. {
  339. return false; }
  340. $this->getLineResponse();
  341. if (!$this-> getRestIsSucceed())
  342. {
  343. return false;
  344. }
  345. return $this->getRespMessage($intReturnType);
  346. }
  347. // 指定されたメールのすべての情報を取得します。 intReturnType は戻り値の型で、1 は文字列です。 2 は配列
  348. 関数 getMailMessage($ intMailId, $intReturnType=1)
  349. {
  350. if (!$this->getIsConnect() && $this->bolIsLogin)
  351. {
  352. return false }
  353. if (! $intMailId = intval($intMailId))
  354. {
  355. $this->setMessage('メール メッセージ ID が無効です', 1005);
  356. return false; }
  357. $this->sendCommand("RETR ". $intMailId) ;
  358. $this->getLineResponse();
  359. if (!$this->getRestIsSucceed())
  360. return $this->getRespMessage($intReturnType);
  361. //特定のメールの前の指定された行を取得します。$intReturnType 戻り値の型、1 は文字列、2 は配列
  362. function getMailTopMessage($intMailId, $intTopLines=10, $intReturnType=1)
  363. {
  364. if (! $this->getIsConnect() && $this->bolIsLogin)
  365. {
  366. return false; }
  367. if (!$intMailId=intval($intMailId) || !$intTopLines=int($intTopLines))
  368. {
  369. $this ->setMessage('メール メッセージ ID または先頭行番号が無効です', 1005);
  370. return false; }
  371. $this->sendCommand("TOP ". $intMailId ." ". $intTopLines);
  372. $this ->getLineResponse();
  373. if (!$this->getRestIsSucceed())
  374. {
  375. return false; }
  376. return $this->getRespMessage($intReturnType);メッセージ
  377. 関数 delMail($intMailId)
  378. {
  379. if (!$this->getIsConnect() && $this->bolIsLogin)
  380. {
  381. return false
  382. }
  383. if (!$intMailId=intval($intMailId) )
  384. {
  385. $this->setMessage('メール メッセージ ID が無効です', 1005);
  386. $this->sendCommand("DELE ".$intMailId); ();
  387. if (!$this->getRestIsSucceed())
  388. {
  389. return false; }
  390. return true;
  391. function replaceDeleMail()
  392. if ( !$this->getIsConnect() && $this->bolIsLogin)
  393. {
  394. return false; }
  395. $this->sendCommand("RSET")
  396. if (!$this->getRestIsSucceed())
  397. {
  398. return false; }
  399. return true;
  400. //---------------
  401. // デバッグ操作
  402. / /--------------
  403. //オブジェクト情報を出力
  404. function printObject()
  405. {
  406. print_r($this)
  407. exit
  408. }
  409. // エラー情報を出力
  410. function printError; ()
  411. {
  412. echo "[Error Msg] : $strMessage
    n";
  413. echo "[Error Num] : $intErrorNum
    n";
  414. }
  415. // ホスト情報を出力します。 function printHost()
  416. {
  417. echo "[ホスト] : $this->strHost
    n";
  418. echo "[ポート] : $this->intPort
    n";
  419. echo "[ Email] : $this->strEmail
    n";
  420. echo "[Passwd] : ********
    n";
  421. exit;
  422. }
  423. //接続情報を出力します
  424. function printConnect()
  425. {
  426. echo "[接続] : $this->resHandler
    n";
  427. echo "[リクエスト] : $this->strRequest
    n";
  428. echo "[ Response] : $this->strResponse
    n";
  429. exit;
  430. }
  431. }
  432. ?

  433. コードをコピー
  434. 呼び出し例:
  435. //テストコード
  436. //例: $o = SocketPOP3Client('メールアドレス', 'パスワード', 'POP3サーバー', 'POP3ポート')
  437. /*
  438. set_time_limit( 0 );
  439. $o = 新しい SocketPOPClient('abc@126.com', 'pop.126.com', '110'); - >getMailBaseList());
  440. print_r($o->getMailSum(1));
$o->getMailTopMessage(2, 2, 2);
$o->closeHost(); $o->printObject() ?>

コードをコピーします
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。