您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

用PHP实现POP3邮件的收取(二)_PHP

2025/10/18 9:32:08发布15次查看
pop3
用php实现pop3收取邮件的类
(作者:陈俊清 2000年10月18日 11:56)
现在让我们来用php实现一个通过pop3协议收取信件的类吧,这个类中所用到的一些sock操作的函数,不另做特殊说明,请参考php的有关资料。通过这个实例,相信你也会和我一样,感觉到php中对于sock操作的灵活、方便和功能的强大。
首先,我们来说明一下这个类中需要用到的一些内部成员变量:(这些变量应该都是对外封闭的,可是由于php对类的成员变量没有private与publice之类的分别,只好就这么直接定义了。这是php的一个令人遗憾的地方。)
1.成员变量说明
class pop3
{
var $hostname=; // pop主机名
var $port=110; // 主机的pop3端口,一般是110号端口
var $timeout=5;// 连接主机的最大超时时间
var $connection=0; // 保存与主机的连接
var $state=disconnected; // 保存当前的状态
var $debug=0;// 做为标识,是否在调试状态,是的话,输出调试信息
var $err_str=';// 如果出错,这里保存错误信息
var $err_no; //如果出错,这里保存错误号码
var $resp; // 临时保存服务器的响应信息
var $apop; // 指示需要使用加密方式进行密码验证,一般服务器不需要
var $messages; // 邮件数
var $size; //各邮件的总大小
var $mail_list; // 一个数组,保存各个邮件的大小及其在邮件服务器上序号
var $head=array(); // 邮件头的内容,数组
var $body=array(); // 邮件体的内容,数组;
2.当然,这其中的有些变量,仅通过这样一个简单的说明并不能完全了解如何使用,下面我就逐个来说明这个类实现中的一些主要方法:
function pop3($server=192.100.100.1,$port=110,$time_out=5)
{$this->hostname=$server;
$this->port=$port;
$this->timeout=$time_out;
return true;
}
熟悉面向对象编程的朋友一看就会知道,这是这个类的构造函数,在初始化这个类时,可以给出这几个最基本的参数:pop3服务器的地址,端口号,及连接服务器时的最大超时时间。一般来说,只需要给出pop3服务器的地址就行了。
function open()
{
if($this->hostname==)
{$this->err_str=无效的主机名!!;
return false;
}
if ($this->debug) echo 正在打开 $this->hostname,$this->port,&$err_no, &$err_str, $this->timeout
;
if (!$this->connection=fsockopen($this->hostname,$this->port,&$err_no, &$err_str, $this->timeout))
{
$this->err_str=连接到pop服务器失败,错误信息:.$err_str.错误号:.$err_no;
return false;
}
else
{
$this->getresp();
if($this->debug)
$this->outdebug($this->resp);
if (substr($this->resp,0,3)!=+ok)
{$this->err_str=服务器返回无效的信息:.$this->resp.请检查pop服务器是否正确;
return false;
}
$this->state=authorization;
return true;
}
}
该方法不需要任何参数就可建立与pop3服务器的sock连接。该方法又用到了另一个类中的方法$this->getresp();下面是这个方法的声明:
function getresp()
{
for($this->resp=;;)
{
if(feof($this->connection))
return false;
$this->resp.=fgets($this->connection,100);
$length=strlen($this->resp);
if($length>=2 && substr($this->resp,$length-2,2)==\r\n)
{
$this->resp=strtok($this->resp,\r\n);
return true;
}
}
}
这个方法取得服务器端的返回信息并进行简单的处理:去掉最后的回车换行符,将返回信息保存在resp这个内部变量中。这个方法在后面的多个操作中都将用到。另外,还有个小方法也在后面的多个操作中用到:
function outdebug($message)
{
echo htmlspecialchars($message).
\n;
}
它的作用就是把调试信息$message显示出来,并把一些特殊字符进行转换以及在行尾加上
标签,这样是为了使其输出的调试信息便于阅读和分析。
建立起与服务器的sock连接之后,就要给服务器发送相关的命令了(请参见上面的与服务器对话的过程)从上面对 pop对话的分析可以看到,每次都是发送一条命令,然后服务器给予一定的回应,如果命令的执行是对的,回应一般是以+ok开头,后面是一些描述信息,所以,我们可以做一个通过发送命令的方法:
function command($command,$return_lenth=1,$return_code='+')
{
if ($this->connection==0)
{
$this->err_str=没有连接到任何服务器,请检查网络连接;
return false;
}
if ($this->debug)
$this->outdebug(>>> $command);
if (!fputs($this->connection,$command\r\n))
{
$this->err_str=无法发送命令.$command;
return false;
}
else
{
$this->getresp();
if($this->debug)
$this->outdebug($this->resp);
if (substr($this->resp,0,$return_lenth)!=$return_code)
{
$this->err_str=$command. 命令服务器返回无效:.$this->resp;
return false;
}
else
return true;
}
}
这个方法可以接受三个参数: $command--> 发送给服务器的命令; $return_lenth,$return_code ,指定从服务器的返回中取多长的值做为命令返回的标识以及这个标识的正确值是什么。对于一般的pop操作来说,如果服务器的返回第一个字符为+,则可以认为命令是正确执行了。也可以用前面提到过的三个字符+ok做为判断的标识。
下面介绍的几个方法则可以按照前述收取信件的对话去理解,因为有关的内容已经在前面做了说明,因此下面的方法不做详细的说明,请参考其中的注释:
function login($user,$password) //发送用户名及密码,登录到服务器
{
if($this->state!=authorization)
{
$this->err_str=还没有连接到服务器或状态不对;
return false;
}
if (!$this->apop) //服务器是否采用apop用户认证
{
if (!$this->command(user $user,3,+ok)) return false;
if (!$this->command(pass $password,3,+ok)) return false;
}
else
{
//echo $this->resp=strtok($this->resp,\r\n);
if (!$this->command(apop $user .md5($this->greeting.$password),3,+ok)) return false;
}
$this->state=transaction; // 用户认证通过,进入传送模式
return true;
}
function stat() // 对应着stat命令,取得总的邮件数与总的大小
{
if($this->state!=transaction)
{
$this->err_str=还没有连接到服务器或没有成功登录;
return false;
}
if (!$this->command(stat,3,+ok))
return false;
else
{
$this->resp=strtok($this->resp, );
$this->messages=strtok( ); // 取得邮件总数
$this->size=strtok( ); //取得总的字节大小
return true;
}
}
function listmail($mess=null,$uni_id=null) //对应的是list命令,取得每个邮件的大小及序号。一般来说用到的是list命令,如果指定了$uni_id ,则使用uidl命令,返回的是每个邮件的标识符,事实上,这个标识符一般是没有什么用的。取得的各个邮件的大小返回到类的内部变量mail_list这个二维数组里。
{
if($this->state!=transaction)
{
$this->err_str=还没有连接到服务器或没有成功登录;
return false;
}
if ($uni_id)
$command=uidl ;
else
$command=list ;
if ($mess)
$command.=$mess;
if (!$this->command($command,3,+ok))
{
//echo $this->err_str;
return false;
}
else
{
$i=0;
$this->mail_list=array();
$this->getresp();
while ($this->resp!=.)
{ $i++;
if ($this->debug)
{
$this->outdebug($this->resp);
}
if ($uni_id)
{
$this->mail_list[$i][num]=strtok($this->resp, );
$this->mail_list[$i][size]=strtok( );
}
else
{
$this->mail_list[$i][num]=intval(strtok($this->resp, ));
$this->mail_list[$i][size]=intval(strtok( ));
}
$this->getresp();
}
return true;
}
}
function getmail($num=1,$line=-1) // 取得邮件的内容,$num是邮件的序号,$line是指定共取得正文的多少行。有些时候,如邮件比较大而我们只想先查看邮件的主题时是必须指定行数的。默认值$line=-1,即取回所有的邮件内容,取得的内容存放到内部变量$head,$body两个数组里,数组里的每一个元素对应的是邮件源代码的一行。
{
if($this->state!=transaction)
{
$this->err_str=不能收取信件,还没有连接到服务器或没有成功登录;
return false;
}
if ($line
$command=retr $num;
else
$command=top $num $line;
if (!$this->command($command,3,+ok))
return false;
else
{
$this->getresp();
$is_head=true;
while ($this->resp!=.) // . 号是邮件结束的标识
{
if ($this->debug)
$this->outdebug($this->resp);
if (substr($this->resp,0,1)==.)
$this->resp=substr($this->resp,1,strlen($this->resp)-1);
if (trim($this->resp)==) // 邮件头与正文部分的是一个空行
$is_head=false;
if ($is_head)
$this->head[]=$this->resp;
else
$this->body[]=$this->resp;
$this->getresp();
}
return true;
}
} // end function
function dele($num) // 删除指定序号的邮件,$num 是服务器上的邮件序号
{
if($this->state!=transaction)
{
$this->err_str=不能删除远程信件,还没有连接到服务器或没有成功登录;
return false;
}
if (!$num)
{
$this->err_str=删除的参数不对;
return false;
}
if ($this->command(dele $num ,3,+ok))
return true;
else
return false;
}
通过以上几个方法,我们已经可以实现邮件的查看、收取、删除的操作,不过别忘了最后要退出,并关闭与服务器的连接,调用下面的这个方法:
function close()
{
if($this->connection!=0)
{
if($this->state==transaction)
$this->command(quit,3,+ok);
fclose($this->connection);
$this->connection=0;
$this->state=disconnected;
}
}
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product