• 技术文章 >后端开发 >php教程

    PHP Socket实现的POP3类

    2016-07-25 08:57:48原创403
    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)<=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. //获取指定邮件得session Id
    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. //取得某个邮件的大小
    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. //获取邮件基本列表数组
    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. //获取指定邮件所有信息,intReturnType是返回值类型,1是字符串,2是数组
    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. //获取某邮件前指定行, $intReturnType 返回值类型,1是字符串,2是数组
    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. //删除邮件
    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. //重置被删除得邮件标记为未删除
    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. // 调试操作
    467. //---------------
    468. //输出对象信息
    469. function printObject()
    470. {
    471. print_r($this);
    472. exit;
    473. }
    474. //输出错误信息
    475. function printError()
    476. {
    477. echo "[Error Msg] : $strMessage
      \n";
    478. echo "[Error Num] : $intErrorNum
      \n";
    479. exit;
    480. }
    481. //输出主机信息
    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. //输出连接信息
    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. ?>

    复制代码

    调用示例:

    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. ?>
    复制代码
    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:PHP Socket实现的POP3类
    上一篇:PHP查询网站PR值的实现代码 下一篇:Google PR值的PHP实现代码
    大前端线上培训班

    相关文章推荐

    • PHP中的命名空间定义与使用(实例详解)• 带你分清类中的构造函数与析构函数• PHP中clone关键字和__clone()方法的使用(实例详解)• 五分钟带你了解PHP中的魔术方法(实例详解)• 怎样去搞定PHP类的继承?(总结分享)

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网