POP3 class implemented by PHPSocket

Source: Internet
Author: User
POP3 class implemented by PHPSocket

  1. /**

  2. * PHP Socket POP3 class
  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. // Basic operations
  24. //---------------
  25. // Constructor
  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. // Connect to the server

  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. // Close the connection
  82. Function closeHost ()
  83. {
  84. If ($ this-> resHandler)
  85. {
  86. Fclose ($ this-> resHandler );
  87. }
  88. Return true;
  89. }
  90. // Send command
  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. // Extract the first line of response information
  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 response information. $ intReturnType is the return value type. 1 is a string and 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-> 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. // Indicates whether the request is successfully extracted.
  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. // Obtain whether the connection is established
  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 the 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. // Obtain the message
  218. Function getMessage ()
  219. {
  220. Return $ this-> strMessage;
  221. }
  222. // Get error code
  223. Function getErrorNum ()
  224. {
  225. Return $ this-> intErrorNum;
  226. }
  227. // Obtain request information
  228. Function getRequest ()
  229. {
  230. Return $ this-> strRequest;
  231. }
  232. // Obtain response information
  233. Function getResponse ()
  234. {
  235. Return $ this-> strResponse;
  236. }

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

  238. // Mail atomic operation
  239. //---------------
  240. // Logon 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. // Obtain whether or not it is 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. // Obtain the number of mails and number of cells (returned 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. // Obtain 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. // Obtain the size of an 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. // Obtain the basic list of emails
  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. // Obtain all the information of the specified Email. intReturnType indicates the return value type, 1 indicates the string, and 2 indicates the 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 row before an 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 the email
  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 the deleted email to undeleted
  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 the operation
  467. //---------------
  468. // Output object information
  469. Function printObject ()
  470. {
  471. Print_r ($ this );
  472. Exit;
  473. }
  474. // Output error message
  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 the 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. ?>

Call example:

  1. // Test code
  2. // Example: $ o = SocketPOP3Client ('email address', 'password', 'pop3 server', 'pop3 Port ')
  3. /*
  4. Set_time_limit (0 );
  5. $ O = new SocketPOPClient ('ABC @ 126.com ', '000000', 'pop .126.com', '000000 ');
  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. ?>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.