设为首页 收藏本站
查看: 1204|回复: 0

[经验分享] 使用PHP写的POP3操作类

[复制链接]

尚未签到

发表于 2017-4-4 12:23:04 | 显示全部楼层 |阅读模式
  

使用PHP的Socket写的POP3类
  查看 POP3/SMTP 协议的时候想尝试一下自己写一个操作类,核心没啥,就是使用 fsockopen ,然后写入/接收数据,只实现了最核心的部分功能,当作是学习 Socket 操作的练手。其中参考了 RFC 2449和一个国外的简单Web邮件系统 Uebimiau 的部分代码,不过绝对没有抄他滴,HOHO,绝对原创。如果你喜欢,请收藏,随便修改,嗯,但是记得不要删除偶类里的声名,毕竟偶也是辛辛苦苦写了好几天呐。
另外,欢迎自由发挥,改善或者修正这个类,希望能够为你所用。代码没有认真仔细的调试,发现bug请自己修正,HOHO!

  <?php
/**
*类名:SocketPOPClient
*功能:POP3协议客户端的基本操作类
*作者:heiyeluren<http://blog.csdn.net/heiyeshuwu>
*时间:2006-7-3
*参考:RFC2449,Uebimiau
*授权:BSDLicense
*/

classSocketPOPClient
{
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();


//---------------
//基础操作
//---------------

//构造函数
functionSocketPOP3Client($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('EmailaddressorPasswdisempty',1001);
return
false;
}
if(!
preg_match("/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/i",$this->strEmail))
{
$this->setMessage('Emailaddressinvalid',1002);
return
false;
}
if(
$this->strHost=='')
{
$this->strHost=substr(strrchr($this->strEmail,"@"),1);
}
if(
$intPort!='')
{
$this->intPort=$intPort;
}
$this->connectHost();
}

//连接服务器
functionconnectHost()
{
if(
$this->bolDebug)
{
echo
"Connection".$this->strHost."...\r\n";
}
if(!
$this->getIsConnect())
{
if(
$this->strHost==''||$this->intPort=='')
{
$this->setMessage('POP3hostorPortisempty',1003);
return
false;
}
$this->resHandler=@fsockopen($this->strHost,$this->intPort,&$this->intErrorNum,&$this->strMessage,$this->intConnSecond);
if(!
$this->resHandler)
{
$strErrMsg='ConnectionPOP3host:'.$this->strHost.'failed';
$intErrNum=2001;
$this->setMessage($strErrMsg,$intErrNum);
return
false;
}
$this->getLineResponse();
if(!
$this->getRestIsSucceed())
{
return
false;
}
}
return
true;
}

//关闭连接
functioncloseHost()
{
if(
$this->resHandler)
{
fclose($this->resHandler);
}
return
true;
}

//发送指令
functionsendCommand($strCommand)
{
if(
$this->bolDebug)
{
if(!
preg_match("/PASS/",$strCommand))
{
echo
"SendCommand:".$strCommand."\r\n";
}
else
{
echo
"SendCommand:PASS******\r\n";
}

}
if(!
$this->getIsConnect())
{
return
false;
}
if(
trim($strCommand)=='')
{
$this->setMessage('Requestcommandisempty',1004);
return
false;
}
$this->strRequest=$strCommand."\r\n";
$this->arrRequest[]=$strCommand;
fputs($this->resHandler,$this->strRequest);
return
true;
}

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

return
$this->strResponse;
}

//提取若干响应信息,$intReturnType是返回值类型,1为字符串,2为数组
functiongetRespMessage($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;
}
}

//提取请求是否成功
functiongetRestIsSucceed($strRespMessage='')
{
if(
trim($responseMessage)=='')
{
if(
$this->strResponse=='')
{
$this->getLineResponse();
}
$strRespMessage=$this->strResponse;
}
if(
trim($strRespMessage)=='')
{
$this->setMessage('Responsemessageisempty',2003);
return
false;
}
if(!
preg_match("/^\+OK/",$strRespMessage))
{
$this->setMessage($strRespMessage,2000);
return
false;
}
return
true;
}

//获取是否已连接
functiongetIsConnect()
{
if(!
$this->resHandler)
{
$this->setMessage("Nonexistentavailabilityconnectionhandler",2002);
return
false;
}
return
true;
}


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

//获取消息
functiongetMessage()
{
return
$this->strMessage;
}

//获取错误号
functiongetErrorNum()
{
return
$this->intErrorNum;
}

//获取请求信息
functiongetRequest()
{
return
$this->strRequest;
}

//获取响应信息
functiongetResponse()
{
return
$this->strResponse;
}


//---------------
//邮件原子操作
//---------------

//登录邮箱
functionpopLogin()
{
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;
}

//退出登录
functionpopLogout()
{
if(!
$this->getIsConnect()&&$this->bolIsLogin)
{
return
false;
}
$this->sendCommand("QUIT");
$this->getLineResponse();
if(!
$this->getRestIsSucceed())
{
return
false;
}
return
true;
}

//获取是否在线
functiongetIsOnline()
{
if(!
$this->getIsConnect()&&$this->bolIsLogin)
{
return
false;
}
$this->sendCommand("NOOP");
$this->getLineResponse();
if(!
$this->getRestIsSucceed())
{
return
false;
}
return
true;
}

//获取邮件数量和字节数(返回数组)
functiongetMailSum($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('STATcommandresponsemessageiserror',2006);
return
false;
}
returnarray(
$arrResponse[1],$arrResponse[2]);
}
}

//获取指定邮件得SessionId
functiongetMailSessId($intMailId,$intReturnType=2)
{
if(!
$this->getIsConnect()&&$this->bolIsLogin)
{
return
false;
}
if(!
$intMailId=intval($intMailId))
{
$this->setMessage('Mailmessageidinvalid',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('UIDLcommandresponsemessageiserror',2006);
return
false;
}
returnarray(
$arrResponse[1],$arrResponse[2]);
}
}

//取得某个邮件的大小
functiongetMailSize($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);
returnarray(
$arrMessage[1],$arrMessage[2]);
}
}

//获取邮件基本列表数组
functiongetMailBaseList($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是数组
functiongetMailMessage($intMailId,$intReturnType=1)
{
if(!
$this->getIsConnect()&&$this->bolIsLogin)
{
return
false;
}
if(!
$intMailId=intval($intMailId))
{
$this->setMessage('Mailmessageidinvalid',1005);
return
false;
}
$this->sendCommand("RETR".$intMailId);
$this->getLineResponse();
if(!
$this->getRestIsSucceed())
{
return
false;
}
return
$this->getRespMessage($intReturnType);
}

//获取某邮件前指定行,$intReturnType返回值类型,1是字符串,2是数组
functiongetMailTopMessage($intMailId,$intTopLines=10,$intReturnType=1)
{
if(!
$this->getIsConnect()&&$this->bolIsLogin)
{
return
false;
}
if(!
$intMailId=intval($intMailId)||!$intTopLines=int($intTopLines))
{
$this->setMessage('MailmessageidorToplinesnumberinvalid',1005);
return
false;
}
$this->sendCommand("TOP".$intMailId."".$intTopLines);
$this->getLineResponse();
if(!
$this->getRestIsSucceed())
{
return
false;
}
return
$this->getRespMessage($intReturnType);
}

//删除邮件
functiondelMail($intMailId)
{
if(!
$this->getIsConnect()&&$this->bolIsLogin)
{
return
false;
}
if(!
$intMailId=intval($intMailId))
{
$this->setMessage('Mailmessageidinvalid',1005);
return
false;
}
$this->sendCommand("DELE".$intMailId);
$this->getLineResponse();
if(!
$this->getRestIsSucceed())
{
return
false;
}
return
true;
}

//重置被删除得邮件标记为未删除
functionresetDeleMail()
{
if(!
$this->getIsConnect()&&$this->bolIsLogin)
{
return
false;
}
$this->sendCommand("RSET");
$this->getLineResponse();
if(!
$this->getRestIsSucceed())
{
return
false;
}
return
true;
}



//---------------
//调试操作
//---------------

//输出对象信息
functionprintObject()
{
print_r($this);
exit;
}

//输出错误信息
functionprintError()
{
echo
"[ErrorMsg]:$strMessage<br>\n";
echo
"[ErrorNum]:$intErrorNum<br>\n";
exit;
}

//输出主机信息
functionprintHost()
{
echo
"[Host]:$this->strHost<br>\n";
echo
"[Port]:$this->intPort<br>\n";
echo
"[Email]:$this->strEmail<br>\n";
echo
"[Passwd]:********<br>\n";
exit;
}

//输出连接信息
functionprintConnect()
{
echo
"[Connect]:$this->resHandler<br>\n"

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-360070-1-1.html 上篇帖子: php防sql注入数据model类 下篇帖子: PHP中的字符串函数(String Functions)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表