Home  >  Article  >  Backend Development  >  POP3 class implemented by PHP Socket

POP3 class implemented by PHP Socket

WBOY
WBOYOriginal
2016-07-25 08:57:481275browse
  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." ...rn";
  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."rn";
  98. }
  99. else
  100. {
  101. echo "Send Command: PASS ******rn";
  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."rn";
  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. //Extract some response information, $intReturnType is the return value type, 1 is a string, 2 is an array
  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->getLineRes ponder ();
  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. //Whether the extraction request was successful
  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. //Get whether it is connected
  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. //Set message

  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. //Get the message
  218. function getMessage()
  219. {
  220. return $this->strMessage;
  221. }
  222. //Get the error number
  223. function getErrorNum ()
  224. {
  225. return $this->intErrorNum;
  226. }
  227. //Get request information
  228. function getRequest()
  229. {
  230. return $this->strRequest;
  231. }
  232. //Get response information
  233. function getResponse()
  234. {
  235. return $this->strResponse;
  236. }

  237. //---------------

  238. // Email atomic operation
  239. //- --------------
  240. //Log in to email
  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. //Log out
  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. //Get whether online
  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. //Get the number and bytes of emails Number (return array)
  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) <=0)
  312. {
  313. $this->setMessage('STAT command response message is error', 2006);
  314. return false;
  315. }
  316. return array($arrResponse[1], $arrResponse[2]);
  317. }
  318. }
  319. //Get the session Id of the specified email
  320. function getMailSessId($intMailId, $intReturnType=2)
  321. {
  322. if (!$this->getIsConnect() && $this->bolIsLogin)
  323. {
  324. return false;
  325. }
  326. if (!$intMailId = intval($intMailId))
  327. {
  328. $this->setMessage('Mail message id invalid', 1005);
  329. return false;
  330. }
  331. $this-> sendCommand("UIDL ". $intMailId);
  332. $this->getLineResponse();
  333. if (!$this->getRestIsSucceed())
  334. {
  335. return false;
  336. }
  337. if ($intReturnType == 1)
  338. {
  339. return $this->strResponse;
  340. }
  341. else
  342. {
  343. $arrResponse = explode(" ", $this->strResponse);
  344. if (!is_array($arrResponse) || count($arrResponse) <=0)
  345. {
  346. $this->setMessage('UIDL command response message is error', 2006);
  347. return false;
  348. }
  349. return array($arrResponse[1], $arrResponse[2]);
  350. }
  351. }
  352. //Get the size of a certain email
  353. function getMailSize($intMailId, $intReturnType=2)
  354. {
  355. if (!$this->getIsConnect() && $this->bolIsLogin)
  356. {
  357. return false;
  358. }
  359. $this->sendCommand("LIST ".$intMailId);
  360. $this->getLineResponse();
  361. if (!$this->getRestIsSucceed())
  362. {
  363. return false;
  364. }
  365. if ($intReturnType == 1)
  366. {
  367. return $this->strResponse;
  368. }
  369. else
  370. {
  371. $arrMessage = explode(' ', $this->strResponse);
  372. return array($ arrMessage[1], $arrMessage[2]);
  373. }
  374. }
  375. //Get the basic mail list array
  376. function getMailBaseList($intReturnType=2)
  377. {
  378. if (!$this->getIsConnect() && $this ->bolIsLogin)
  379. {
  380. return false;
  381. }
  382. $this->sendCommand("LIST");
  383. $this->getLineResponse();
  384. if (!$this->getRestIsSucceed())
  385. {
  386. return false;
  387. }
  388. return $this->getRespMessage($intReturnType);
  389. }
  390. //Get all information of the specified email, intReturnType is the return value type, 1 is a string, 2 is an array
  391. function getMailMessage($ intMailId, $intReturnType=1)
  392. {
  393. if (!$this->getIsConnect() && $this->bolIsLogin)
  394. {
  395. return false;
  396. }
  397. if (!$intMailId = intval($intMailId))
  398. {
  399. $this->setMessage('Mail message id invalid', 1005);
  400. return false;
  401. }
  402. $this->sendCommand("RETR ". $intMailId);
  403. $this->getLineResponse( );
  404. if (!$this->getRestIsSucceed())
  405. {
  406. return false;
  407. }
  408. return $this->getRespMessage($intReturnType);
  409. }
  410. //Get the specified line before a certain email, $intReturnType return value type, 1 is a string, 2 is an array
  411. function getMailTopMessage($intMailId, $intTopLines=10, $intReturnType=1)
  412. {
  413. if (!$this- >getIsConnect() && $this->bolIsLogin)
  414. {
  415. return false;
  416. }
  417. if (!$intMailId=intval($intMailId) || !$intTopLines=int($intTopLines))
  418. {
  419. $this ->setMessage('Mail message id or Top lines number invalid', 1005);
  420. return false;
  421. }
  422. $this->sendCommand("TOP ". $intMailId ." ". $intTopLines);
  423. $this ->getLineResponse();
  424. if (!$this->getRestIsSucceed())
  425. {
  426. return false;
  427. }
  428. return $this->getRespMessage($intReturnType);
  429. }
  430. //Delete message
  431. function delMail($intMailId)
  432. {
  433. if (!$this->getIsConnect() && $this->bolIsLogin)
  434. {
  435. return false;
  436. }
  437. if (!$intMailId=intval($intMailId))
  438. {
  439. $this->setMessage('Mail message id invalid', 1005);
  440. return false;
  441. }
  442. $this->sendCommand("DELE ".$intMailId);
  443. $this->getLineResponse();
  444. if (!$this->getRestIsSucceed())
  445. {
  446. return false;
  447. }
  448. return true;
  449. }
  450. //Reset deleted emails marked as not deleted
  451. function resetDeleMail()
  452. {
  453. if ( !$this->getIsConnect() && $this->bolIsLogin)
  454. {
  455. return false;
  456. }
  457. $this->sendCommand("RSET");
  458. $this->getLineResponse();
  459. if (!$this->getRestIsSucceed())
  460. {
  461. return false;
  462. }
  463. return true;
  464. }
  465. //---------------
  466. // Debug operation
  467. / /---------------
  468. //Output object information
  469. function printObject()
  470. {
  471. print_r($this);
  472. exit;
  473. }
  474. //Output error information
  475. function printError ()
  476. {
  477. echo "[Error Msg] : $strMessage
    n";
  478. echo "[Error Num] : $intErrorNum
    n";
  479. exit;
  480. }
  481. //Output host information
  482. function printHost()
  483. {
  484. echo "[Host] : $this->strHost
    n";
  485. echo "[Port] : $this->intPort
    n";
  486. echo "[ Email] : $this->strEmail
    n";
  487. echo "[Passwd] : ********
    n";
  488. exit;
  489. }
  490. //Output connection information
  491. function printConnect()
  492. {
  493. echo "[Connect] : $this->resHandler
    n";
  494. echo "[Request] : $this->strRequest
    n";
  495. echo "[ Response] : $this->strResponse
    n";
  496. exit;
  497. }
  498. }
  499. ?>

Copy code

Call example:

  1. //Test code
  2. //For example: $o = SocketPOP3Client('Email address', 'Password', 'POP3 server', 'POP3 port')
  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. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn