PHP socket implementation of the POP3 class

Source: Internet
Author: User
  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 function
  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. Connecting to a 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 connection
  82. function Closehost ()
  83. {
  84. if ($this->reshandler)
  85. {
  86. Fclose ($this->reshandler);
  87. }
  88. return true;
  89. }
  90. Send instructions
  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 response information First line
  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. Extracts several response information, $intReturnType is a 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->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. Whether the fetch 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. Gets whether the connection 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 message
  218. function GetMessage ()
  219. {
  220. return $this->strmessage;
  221. }
  222. Get 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. Mail Atomic operations
  239. //---------------
  240. Login Mailbox
  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. Sign 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. Gets 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 Message count and bytes (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. Gets the session Id of the specified message.
  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 message
  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 message base 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. Gets all information for the specified message, Intreturntype is the return value type, 1 is a string, and 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. Gets a message before the specified row, $intReturnType return a value type, 1 is a string, and 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 messages 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. Debugging operations
  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 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

Invocation 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
  • 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.