Heim >Backend-Entwicklung >PHP-Tutorial >PHP Socket实现的POP3类

PHP Socket实现的POP3类

WBOY
WBOYOriginal
2016-07-25 08:57:481362Durchsuche
  1. /**

  2. * PHP Socket 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 = trim(strtolower($strLoginEmail));
  29. $this->strPasswd = trim($strLoginPasswd);
  30. $this->strHost = trim(strtolower($strPopHost));
  31. if ($this->strEmail=='' || $this->strPasswd=='')
  32. {
  33. $this->setMessage('Email address or Passwd is empty', 1001);
  34. return false;
  35. }
  36. if (!PReg_match("/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/i", $this->strEmail))
  37. {
  38. $this->setMessage('Email address invalid', 1002);
  39. return 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." ...\r\n";
  57. }
  58. if (!$this->getIsConnect())
  59. {
  60. if ($this->strHost=='' || $this->intPort=='')
  61. {
  62. $this->setMessage('POP3 host or Port is empty', 1003);
  63. return false;
  64. }
  65. $this->resHandler = @fsockopen($this->strHost, $this->intPort, &$this->intErrorNum, &$this->strMessage, $this->intConnSecond);
  66. if (!$this->resHandler)
  67. {
  68. $strErrMsg = 'Connection POP3 host: '.$this->strHost.' failed';
  69. $intErrNum = 2001;
  70. $this->setMessage($strErrMsg, $intErrNum);
  71. return false;
  72. }
  73. $this->getLineResponse();
  74. if (!$this->getRestIsSucceed())
  75. {
  76. return false;
  77. }
  78. }
  79. return true;
  80. }
  81. //关闭连接
  82. function closeHost()
  83. {
  84. if ($this->resHandler)
  85. {
  86. fclose($this->resHandler);
  87. }
  88. return true;
  89. }
  90. //发送指令
  91. function sendCommand($strCommand)
  92. {
  93. if ($this->bolDebug)
  94. {
  95. if (!preg_match("/PASS/", $strCommand))
  96. {
  97. echo "Send Command: ".$strCommand."\r\n";
  98. }
  99. else
  100. {
  101. echo "Send Command: PASS ******\r\n";
  102. }
  103. }
  104. if (!$this->getIsConnect())
  105. {
  106. return false;
  107. }
  108. if (trim($strCommand)=='')
  109. {
  110. $this->setMessage('Request command is empty', 1004);
  111. return false;
  112. }
  113. $this->strRequest = $strCommand."\r\n";
  114. $this->arrRequest[] = $strCommand;
  115. fputs($this->resHandler, $this->strRequest);
  116. return 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. return 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. continue;
  145. }
  146. if (trim($strLineResponse)=='.')
  147. {
  148. break;
  149. }
  150. $strAllResponse .= $strLineResponse;
  151. }
  152. return $strAllResponse;
  153. }
  154. else
  155. {
  156. $arrAllResponse = array();
  157. while(!feof($this->resHandler))
  158. {
  159. $strLineResponse = $this->getLineResponse();
  160. if (preg_match("/^\+OK/", $strLineResponse))
  161. {
  162. continue;
  163. }
  164. if (trim($strLineResponse)=='.')
  165. {
  166. break;
  167. }
  168. $arrAllResponse[] = $strLineResponse;
  169. }
  170. return $arrAllResponse;
  171. }
  172. }
  173. //提取请求是否成功
  174. function getRestIsSucceed($strRespMessage='')
  175. {
  176. if (trim($responseMessage)=='')
  177. {
  178. if ($this->strResponse=='')
  179. {
  180. $this->getLineResponse();
  181. }
  182. $strRespMessage = $this->strResponse;
  183. }
  184. if (trim($strRespMessage)=='')
  185. {
  186. $this->setMessage('Response message is empty', 2003);
  187. return false;
  188. }
  189. if (!preg_match("/^\+OK/", $strRespMessage))
  190. {
  191. $this->setMessage($strRespMessage, 2000);
  192. return false;
  193. }
  194. return true;
  195. }
  196. //获取是否已连接
  197. function getIsConnect()
  198. {
  199. if (!$this->resHandler)
  200. {
  201. $this->setMessage("Nonexistent availability connection handler", 2002);
  202. return false;
  203. }
  204. return true;
  205. }
  206. //设置消息

  207. function setMessage($strMessage, $intErrorNum)
  208. {
  209. if (trim($strMessage)=='' || $intErrorNum=='')
  210. {
  211. return false;
  212. }
  213. $this->strMessage = $strMessage;
  214. $this->intErrorNum = $intErrorNum;
  215. return true;
  216. }
  217. //获取消息
  218. function getMessage()
  219. {
  220. return $this->strMessage;
  221. }
  222. //获取错误号
  223. function getErrorNum()
  224. {
  225. return $this->intErrorNum;
  226. }
  227. //获取请求信息
  228. function getRequest()
  229. {
  230. return $this->strRequest;
  231. }
  232. //获取响应信息
  233. function getResponse()
  234. {
  235. return $this->strResponse;
  236. }
  237. //---------------

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

调用示例:

  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();
  13. */
  14. ?>
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn