电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

使用PHP的Socket寫的POP3類

瀏覽:9日期:2023-12-27 10:26:52

查看 POP3/SMTP 協(xié)議的時候想嘗試一下自己寫一個操作類,核心沒啥,就是使用 fsockopen ,然后寫入/接收數(shù)據(jù),只實現(xiàn)了最核心的部分功能,當作是學習 Socket 操作的練手。其中參考了 RFC 2449和一個國外的簡單Web郵件系統(tǒng) Uebimiau 的部分代碼,不過絕對沒有抄他滴,HOHO,絕對原創(chuàng)。如果你喜歡,請收藏,隨便修改,嗯,但是記得不要刪除偶類里的聲名,畢竟偶也是辛辛苦苦寫了好幾天吶。另外,歡迎自由發(fā)揮,改善或者修正這個類,希望能夠為你所用。代碼沒有認真仔細的調(diào)試,發(fā)現(xiàn)bug請自己修正,HOHO!

<?php /** * 類名:SocketPOPClient * 功能:POP3 協(xié)議客戶端的基本操作類 * 作者:heiyeluren <http://blog.csdn.net/heiyeshuwu> * 時間:2006-7-3 * 參考:RFC 2449, Uebimiau * 授權(quán):BSD License */

class SocketPOPClient { var $strMessage;;;;;= ''; var $intErrorNum;= 0; var $bolDebug;;;;;= false; var $strEmail;;;;;= ''; var $strPasswd;;;;;= ''; var $strHost;;;;;= ''; var $intPort;;;;;= 110; var $intConnSecond;= 30; var $intBuffSize;= 8192;

var $resHandler;;;;;= NULL; var $bolIsLogin;;;;;= false; var $strRequest;;;;;= ''; var $strResponse;= ''; var $arrRequest;;;;;= array(); var $arrResponse;= array();

//--------------- // 基礎(chǔ)操作 //---------------

//構(gòu)造函數(shù) function SocketPOP3Client($strLoginEmail, $strLoginPasswd, $strPopHost='', $intPort='') { $this->strEmail;;;;;= trim(strtolower($strLoginEmail)); $this->strPasswd;= trim($strLoginPasswd); $this->strHost;;;;;= trim(strtolower($strPopHost));

if ($this->strEmail=='' || $this->strPasswd=='') { $this->setMessage('Email address or Passwd is empty', 1001); return false; } if (!preg_match('/^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+$/i', $this->strEmail)) { $this->setMessage('Email address invalid', 1002); return false; } if ($this->strHost=='') { $this->strHost = substr(strrchr($this->strEmail, '@'), 1); } if ($intPort!='') { $this->intPort = $intPort; } $this->connectHost(); } //連接服務器 function connectHost() { if ($this->bolDebug) { echo 'Connection '.$this->strHost.' ...rn'; } if (!$this->getIsConnect()) { if ($this->strHost=='' || $this->intPort=='') { $this->setMessage('POP3 host or Port is empty', 1003); return false;;; } $this->resHandler = @fsockopen($this->strHost, $this->intPort, &$this->intErrorNum, &$this->strMessage, $this->intConnSecond); if (!$this->resHandler) { $strErrMsg = 'Connection POP3 host: '.$this->strHost.' failed'; $intErrNum = 2001; $this->setMessage($strErrMsg, $intErrNum); return false; } $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } } return true; }

//關(guān)閉連接 function closeHost() { if ($this->resHandler) { fclose($this->resHandler); } return true; }

//發(fā)送指令 function sendCommand($strCommand) { if ($this->bolDebug) { if (!preg_match('/PASS/', $strCommand)) { echo 'Send Command: '.$strCommand.'rn'; } else { echo 'Send Command: PASS ******rn'; }

} if (!$this->getIsConnect()) { return false; } if (trim($strCommand)=='') { $this->setMessage('Request command is empty', 1004); return false; } $this->strRequest = $strCommand.'rn'; $this->arrRequest[] = $strCommand; fputs($this->resHandler, $this->strRequest); return true; }

//提取響應信息第一行 function getLineResponse() { if (!$this->getIsConnect()) { return false; } $this->strResponse = fgets($this->resHandler, $this->intBuffSize); $this->arrResponse[] = $this->strResponse;

return $this->strResponse; }

//提取若干響應信息,$intReturnType是返回值類型, 1為字符串, 2為數(shù)組 function getRespMessage($intReturnType) { if (!$this->getIsConnect()) { return false; } if ($intReturnType == 1) { $strAllResponse = ''; while(!feof($this->resHandler)) { $strLineResponse = $this->getLineResponse(); if (preg_match('/^+OK/', $strLineResponse)) { continue; } if (trim($strLineResponse)=='.') { break; } $strAllResponse .= $strLineResponse; } return $strAllResponse; } else { $arrAllResponse = array(); while(!feof($this->resHandler)) { $strLineResponse = $this->getLineResponse(); if (preg_match('/^+OK/', $strLineResponse)) { continue; } if (trim($strLineResponse)=='.') { break; } $arrAllResponse[] = $strLineResponse; } return $arrAllResponse;;; } }

//提取請求是否成功 function getRestIsSucceed($strRespMessage='') { if (trim($responseMessage)=='') { if ($this->strResponse=='') { $this->getLineResponse(); } $strRespMessage = $this->strResponse; } if (trim($strRespMessage)=='') { $this->setMessage('Response message is empty', 2003); return false; } if (!preg_match('/^+OK/', $strRespMessage)) { $this->setMessage($strRespMessage, 2000); return false; } return true; }

//獲取是否已連接 function getIsConnect() { if (!$this->resHandler) { $this->setMessage('Nonexistent availability connection handler', 2002); return false; } return true; }

//設置消息 function setMessage($strMessage, $intErrorNum) { if (trim($strMessage)=='' || $intErrorNum=='') { return false; } $this->strMessage;= $strMessage; $this->intErrorNum;= $intErrorNum; return true; }

//獲取消息 function getMessage() { return $this->strMessage; }

//獲取錯誤號 function getErrorNum() { return $this->intErrorNum; }

//獲取請求信息 function getRequest() { return $this->strRequest; }

//獲取響應信息 function getResponse() { return $this->strResponse; }

//--------------- // 郵件原子操作 //---------------

//登錄郵箱 function popLogin() { if (!$this->getIsConnect()) { return false; } $this->sendCommand('USER '.$this->strEmail); $this->getLineResponse(); $bolUserRight = $this->getRestIsSucceed();

$this->sendCommand('PASS '.$this->strPasswd); $this->getLineResponse(); $bolPassRight = $this->getRestIsSucceed();

if (!$bolUserRight || !$bolPassRight) { $this->setMessage($this->strResponse, 2004); return false; } $this->bolIsLogin = true; return true; }

//退出登錄 function popLogout() { if (!$this->getIsConnect() && $this->bolIsLogin) { return false; } $this->sendCommand('QUIT'); $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } return true; }

//獲取是否在線 function getIsOnline() { if (!$this->getIsConnect() && $this->bolIsLogin) { return false; } $this->sendCommand('NOOP'); $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } return true; }

//獲取郵件數(shù)量和字節(jié)數(shù)(返回數(shù)組) function getMailSum($intReturnType=2) { if (!$this->getIsConnect() && $this->bolIsLogin) { return false; } $this->sendCommand('STAT'); $strLineResponse = $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } if ($intReturnType==1) { return;;$this->strResponse; } else { $arrResponse = explode(' ', $this->strResponse); if (!is_array($arrResponse) || count($arrResponse)<=0) { $this->setMessage('STAT command response message is error', 2006); return false; } return array($arrResponse[1], $arrResponse[2]); } }

//獲取指定郵件得Session Id function getMailSessId($intMailId, $intReturnType=2) { if (!$this->getIsConnect() && $this->bolIsLogin) { return false; } if (!$intMailId = intval($intMailId)) { $this->setMessage('Mail message id invalid', 1005); return false; } $this->sendCommand('UIDL '. $intMailId); $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } if ($intReturnType == 1) { return;;$this->strResponse; } else { $arrResponse = explode(' ', $this->strResponse); if (!is_array($arrResponse) || count($arrResponse)<=0) { $this->setMessage('UIDL command response message is error', 2006); return false; } return array($arrResponse[1], $arrResponse[2]); } }

//取得某個郵件的大小 function getMailSize($intMailId, $intReturnType=2) { if (!$this->getIsConnect() && $this->bolIsLogin) { return false; } $this->sendCommand('LIST '.$intMailId); $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } if ($intReturnType == 1) { return $this->strResponse; } else { $arrMessage = explode(' ', $this->strResponse); return array($arrMessage[1], $arrMessage[2]); } }

//獲取郵件基本列表數(shù)組 function getMailBaseList($intReturnType=2) { if (!$this->getIsConnect() && $this->bolIsLogin) { return false; } $this->sendCommand('LIST'); $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } return $this->getRespMessage($intReturnType); }

//獲取指定郵件所有信息,intReturnType是返回值類型,1是字符串,2是數(shù)組 function getMailMessage($intMailId, $intReturnType=1) { if (!$this->getIsConnect() && $this->bolIsLogin) { return false; } if (!$intMailId = intval($intMailId)) { $this->setMessage('Mail message id invalid', 1005); return false; } $this->sendCommand('RETR '. $intMailId); $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } return $this->getRespMessage($intReturnType); }

//獲取某郵件前指定行, $intReturnType 返回值類型,1是字符串,2是數(shù)組 function getMailTopMessage($intMailId, $intTopLines=10, $intReturnType=1) { if (!$this->getIsConnect() && $this->bolIsLogin) { return false; } if (!$intMailId=intval($intMailId) || !$intTopLines=int($intTopLines)) { $this->setMessage('Mail message id or Top lines number invalid', 1005); return false; } $this->sendCommand('TOP '. $intMailId .' '. $intTopLines); $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } return $this->getRespMessage($intReturnType); }

//刪除郵件 function delMail($intMailId) { if (!$this->getIsConnect() && $this->bolIsLogin) { return false; } if (!$intMailId=intval($intMailId)) { $this->setMessage('Mail message id invalid', 1005); return false; } $this->sendCommand('DELE '.$intMailId); $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } return true; }

//重置被刪除得郵件標記為未刪除 function resetDeleMail() { if (!$this->getIsConnect() && $this->bolIsLogin) { return false; } $this->sendCommand('RSET'); $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } return true; }

//--------------- // 調(diào)試操作 //---------------

//輸出對象信息 function printObject() { print_r($this); exit; }

//輸出錯誤信息 function printError() { echo '[Error Msg] : $strMessage;;<br>n'; echo '[Error Num] : $intErrorNum <br>n'; exit; }

//輸出主機信息 function printHost() { echo '[Host]; : $this->strHost <br>n'; echo '[Port]; : $this->intPort <br>n'; echo '[Email] : $this->strEmail <br>n'; echo '[Passwd] : ******** <br>n'; exit; }

//輸出連接信息 function printConnect() { echo '[Connect] : $this->resHandler <br>n'; echo '[Request] : $this->strRequest <br>n'; echo '[Response] : $this->strResponse <br>n'; exit; } }

?>

<? //測試代碼 //例如:$o = SocketPOP3Client('郵箱地址', '密碼', 'POP3服務器', 'POP3端口')

/* set_time_limit(0); $o = new SocketPOPClient('abc@126.com', '123456', 'pop.126.com', '110'); $o->popLogin(); print_r($o->getMailBaseList()); print_r($o->getMailSum(1)); print_r($o->getMailTopMessage(2, 2, 2)); $o->popLogout(); $o->closeHost(); $o->printObject(); */ ?>

http://dev.csdn.net/author/heiyeshuwu/b0fd8972a5e942ba81d6d9f5a10ec267.html

標簽: PHP
主站蜘蛛池模板: 小型手持气象站-空气负氧离子监测站-多要素微气象传感器-山东天合环境科技有限公司 | 热处理温控箱,热处理控制箱厂家-吴江市兴达电热设备厂 | 并网柜,汇流箱,电控设备,中高低压开关柜,电气电力成套设备,PLC控制设备订制厂家,江苏昌伟业新能源科技有限公司 | MES系统-WMS系统-MES定制开发-制造执行MES解决方案-罗浮云计算 | 中图网(原中国图书网):网上书店,尾货特色书店,30万种特价书低至2折! | 番茄畅听邀请码怎么输入 - Dianw8.com | 矿用履带式平板车|探水钻机|气动架柱式钻机|架柱式液压回转钻机|履带式钻机-启睿探水钻机厂家 | 蒸汽热收缩机_蒸汽发生器_塑封机_包膜机_封切收缩机_热收缩包装机_真空机_全自动打包机_捆扎机_封箱机-东莞市中堡智能科技有限公司 | 阜阳在线-阜阳综合门户 | 专业的新乡振动筛厂家-振动筛品质保障-环保振动筛价格—新乡市德科筛分机械有限公司 | 千斤顶,液压千斤顶-力良企业,专业的液压千斤顶制造商,shliliang.com | 杭州可当科技有限公司—流量卡_随身WiFi_AI摄像头一站式解决方案 | 法兰连接型电磁流量计-蒸汽孔板节流装置流量计-北京凯安达仪器仪表有限公司 | 作文导航网_作文之家_满分作文_优秀作文_作文大全_作文素材_最新作文分享发布平台 | 大学食堂装修设计_公司餐厅效果图_工厂食堂改造_迈普装饰 | 海德莱电力(HYDELEY)-无功补偿元器件生产厂家-二十年专业从事电力电容器 | 铝镁锰板厂家_进口钛锌板_铝镁锰波浪板_铝镁锰墙面板_铝镁锰屋面-杭州军晟金属建筑材料 | 集菌仪厂家_全封闭_封闭式_智能智能集菌仪厂家-上海郓曹 | 皮带机_移动皮带机_大倾角皮带机_皮带机厂家 - 新乡市国盛机械设备有限公司 | 钢丝绳探伤仪-钢丝绳检测仪-钢丝绳探伤设备-洛阳泰斯特探伤技术有限公司 | 防爆电机_防爆电机型号_河南省南洋防爆电机有限公司 | 西门子伺服电机维修,西门子电源模块维修,西门子驱动模块维修-上海渠利 | 亿立分板机_曲线_锯片式_走刀_在线式全自动_铣刀_在线V槽分板机-杭州亿协智能装备有限公司 | 专注提供国外机电设备及配件-工业控制领域一站式服务商-深圳市华联欧国际贸易有限公司 | nalgene洗瓶,nalgene量筒,nalgene窄口瓶,nalgene放水口大瓶,浙江省nalgene代理-杭州雷琪实验器材有限公司 | 冷凝锅炉_燃气锅炉_工业燃气锅炉改造厂家-北京科诺锅炉 | 黑龙江京科脑康医院-哈尔滨精神病医院哪家好_哈尔滨精神科医院排名_黑龙江精神心理病专科医院 | 船用烟火信号弹-CCS防汛救生圈-船用救生抛绳器(海威救生设备) | 全自动烧卖机厂家_饺子机_烧麦机价格_小笼汤包机_宁波江北阜欣食品机械有限公司 | 拉伸膜,PE缠绕膜,打包带,封箱胶带,包装膜厂家-东莞宏展包装 | 电子元器件呆滞料_元器件临期库存清仓尾料_尾料优选现货采购处理交易商城 | 垃圾处理设备_餐厨垃圾处理设备_厨余垃圾处理设备_果蔬垃圾处理设备-深圳市三盛环保科技有限公司 | 厚壁钢管-厚壁无缝钢管-小口径厚壁钢管-大口径厚壁钢管 - 聊城宽达钢管有限公司 | 镀锌角钢_槽钢_扁钢_圆钢_方矩管厂家_镀锌花纹板-海邦钢铁(天津)有限公司 | 长沙印刷厂-包装印刷-画册印刷厂家-湖南省日大彩色印务有限公司 青州搬家公司电话_青州搬家公司哪家好「鸿喜」青州搬家 | 密集架|电动密集架|移动密集架|黑龙江档案密集架-大量现货厂家销售 | 校园气象站_超声波气象站_农业气象站_雨量监测站_风途科技 | 一体化污水处理设备-一体化净水设备-「山东梦之洁水处理」 | 北京律师咨询_知名专业北京律师事务所_免费法律咨询 | 蜘蛛车-高空作业平台-升降机-高空作业车租赁-臂式伸缩臂叉装车-登高车出租厂家 - 普雷斯特机械设备(北京)有限公司 | 电车线(用于供电给电车的输电线路)-百科 |