回首页 ◎ 设为首页  
◎ 收藏本站  
◎ 联系我们  
  首 页  网络资讯  教程资料  免费资源  建站指南  休闲娱乐  经典整站  访客留言  
  当前位置:首 页 >> 教程资料 >> 网站编程 >> 收pop3邮件的PHP类
最 新 推 荐
PHP入门速成推荐
ASP错误代码推荐
热 门 排 行
PHP站内搜索代码
远程调用163网易相册..
万能播放器源代码[php..
5分钟编写一个ASP论坛
记录蜘蛛爬行的代码 ASP
CMS内容管理系统概述
通过PHP程序知道蜘蛛..
ASP常用数据库连接及..
UTF8下的中文PHP编程
谈网页编程PHP语言的..
值得收藏的一些ASP代码
网站生成静态页面,及..
用IP地址来统计访问人..
用PHP和MySQL保存和输..
PHP生成静态页面详解
ASP中FSO的神奇功能
最 近 更 新
正则表达式
GB2312转换UTF-8的文件
用PHP自动把纯文本转..
asp中可以保存参数值..
PHP对战ASP:这还值得..
怎样才能成为PHP高手..
用ASP实现在线文章翻..
禁用页面缓存的几种方..
关于Zend Optimizer
什么是Session?
广 告 位 置
站 内 搜 索
关键词

搜索方式

搜索范围

精确匹配
收pop3邮件的PHP类

来源:盛绿设计 等级:默认等级
发布于2006-12-23 11:17 被读49次 【字体:

pop3.php
class pop3_class
{
var  $hostname="";
var  $port=110;
var  $connection=0;
var  $state="DISCONNECTED";
var  $greeting="";
var  $must_update=0;
var  $debug=0;
Function OutputDebug( $message)
{
echo  $message,"
n";
}
Function GetLine()
{
for( $line="";;)
{
if(feof( $this->connection))
return(0);
 $line.=fgets( $this->connection,100);
 $length=strlen( $line);
if( $length>=2 && substr( $line, $length-2,2)=="rn")
{
 $line=substr( $line,0, $length-2);
if( $this->debug)
 $this->OutputDebug("<  $line");
return( $line);
}
}
}
Function PutLine( $line)
{
if( $this->debug)
 $this->OutputDebug(">  $line");
return(fputs( $this->connection," $linern"));
}
Function OpenConnection()
{
if( $this->hostname=="")
return("2 it was not specified a valid hostname");
switch(( $this->connection=fsockopen( $this->hostname, $this->port)))
{
case -3:
return("-3 socket could not be created");
case -4:
return("-4 dns lookup on hostname " $hostname" failed");
case -5:
return("-5 connection refused or timed out");
case -6:
return("-6 fdopen() call failed");
case -7:
return("-7 setvbuf() call failed");
default:
return("");
}
}
Function CloseConnection()
{
if( $this->connection!=0)
{
fclose( $this->connection);
 $this->connection=0;
}
}
Function Open()
{
if( $this->state!="DISCONNECTED")
return("1 a connection is already opened");
if(( $error= $this->OpenConnection())!="")
return( $error);
 $this->greeting= $this->GetLine();
if(GetType( $this->greeting)!="string"
|| strtok( $this->greeting," ")!="+OK")
{
 $this->CloseConnection();
return("3 POP3 server greeting was not found");
}
 $this->greeting=strtok("rn");
 $this->must_update=0;
 $this->state="AUTHORIZATION";
return("");
}
Function Close()
{
if( $this->state=="DISCONNECTED")
return("no connection was opened");
if( $this->must_update)
{
if( $this->PutLine("QUIT")==0)
return("Could not send the QUIT command");
 $response= $this->GetLine();
if(GetType( $response)!="string")
return("Could not get quit command response");
if(strtok( $response," ")!="+OK")
return("Could not quit the connection: ".strtok("rn"));
}
 $this->CloseConnection();
 $this->state="DISCONNECTED";
return("");
}
Function Login( $user, $password, $apop)
{
if( $this->state!="AUTHORIZATION")
return("connection is not in AUTHORIZATION state");
if( $apop)
{
if( $this->PutLine("APOP  $user ".md5( $this->greeting. $password))==0)
return("Could not send the APOP command");
 $response= $this->GetLine();
if(GetType( $response)!="string")
return("Could not get APOP login command response");
if(strtok( $response," ")!="+OK")
return("APOP login failed: ".strtok("rn"));
}
else
{
if( $this->PutLine("USER  $user")==0)
return("Could not send the USER command");
 $response= $this->GetLine();
if(GetType( $response)!="string")
return("Could not get user login entry response");
if(strtok( $response," ")!="+OK")
return("User error: ".strtok("rn"));
if( $this->PutLine("PASS  $password")==0)
return("Could not send the PASS command");
 $response= $this->GetLine();
if(GetType( $response)!="string")
return("Could not get login password entry response");
if(strtok( $response," ")!="+OK")
return("Password error: ".strtok("rn"));
}
 $this->state="TRANSACTION";
return("");
}
/* Statistics method - pass references to variables to hold the number of
messages in the mail box and the size that they take in bytes. */
Function Statistics( $messages, $size)
{
if( $this->state!="TRANSACTION")
return("connection is not in TRANSACTION state");
if( $this->PutLine("STAT")==0)
return("Could not send the STAT command");
 $response= $this->GetLine();
if(GetType( $response)!="string")
return("Could not get the statistics command response");
if(strtok( $response," ")!="+OK")
return("Could not get the statistics: ".strtok("rn"));
 $messages=strtok(" ");
 $size=strtok(" ");
return("");
}
Function ListMessages( $message, $unique_id)
{
if( $this->state!="TRANSACTION")
return("connection is not in TRANSACTION state");
if( $unique_id)
 $list_command="UIDL";
else
 $list_command="LIST";
if( $this->PutLine(" $list_command  $message")==0)
return("Could not send the  $list_command command");
 $response= $this->GetLine();
if(GetType( $response)!="string")
return("Could not get message list command response");
if(strtok( $response," ")!="+OK")
return("Could not get the message listing: ".strtok("rn"));
if( $message=="")
{
for( $messages=array();;)
{
 $response= $this->GetLine();
if(GetType( $response)!="string")
return("Could not get message list response");
if( $response==".")
break;
 $message=intval(strtok( $response," "));
if( $unique_id)
 $messages[ $message]=strtok(" ");
else
 $messages[ $message]=intval(strtok(" "));
}
return( $messages);
}
else
{
 $message=intval(strtok(" "));
return(intval(strtok(" ")));
}
}
Function RetrieveMessage( $message, $headers, $body, $lines)
{
if( $this->state!="TRANSACTION")
return("connection is not in TRANSACTION state");
if( $lines<0)
{
 $command="RETR";
 $arguments=" $message";
}
else
{
 $command="TOP";
 $arguments=" $message  $lines";
}
if( $this->PutLine(" $command  $arguments")==0)
return("Could not send the  $command command");
 $response= $this->GetLine();
if(GetType( $response)!="string")
return("Could not get message retrieval command response");
if(strtok( $response," ")!="+OK")
return("Could not retrieve the message: ".strtok("rn"));
for( $headers= $body=array(), $line=0;; $line++)
{
 $response= $this->GetLine();
if(GetType( $response)!="string")
return("Could not retrieve the message");
switch( $response)
{
case ".":
return("");
case "":
break 2;
default:
if(substr( $response,0,1)==".")
 $response=substr( $response,1,strlen( $response)-1);
break;
}
 $headers[ $line]= $response;
}
for( $line=0;; $line++)
{
 $response= $this->GetLine();
if(GetType( $response)!="string")
return("Could not retrieve the message");
switch( $response)
{
case ".":
return("");
default:
if(substr( $response,0,1)==".")
 $response=substr( $response,1,strlen( $response)-1);
break;
}
 $body[ $line]= $response;
}
return("");
}
};
?>
例子:
require("pop3.php");
 $user="user";
 $password="passwd";
 $apop=0;
 $pop3_connection=new pop3_class;
 $pop3_connection->hostname="mail.xiaocui.com";
if(( $error= $pop3_connection->Open())=="")
{
echo "
Connected to the POP3 server " $pop3_connection->hostname".
n";
if(( $error= $pop3_connection->Login( $user, $password, $apop))=="")
{
echo "
User " $user" logged in.
n";
if(( $error= $pop3_connection->Statistics(& $messages,& $size))=="")
{
echo "
There are  $messages messages in the mail box with a total of  $size bytes.
n";
 $result= $pop3_connection->ListMessages("",0);
if(GetType( $result)=="array")
{
for(Reset( $result), $message=0; $message echo "
Message ",Key( $result)," - ", $result[Key( $result)]," bytes.
n";
if( $messages>0)
{
if(( $error= $pop3_connection->RetrieveMessage(1,& $headers,& $body,-1))=="")
{
echo "
Message 1:n---Message headers starts below---
n";
for( $line=0; $line echo "
",HtmlSpecialChars( $headers[ $line]),"
n";
echo "
---Message headers ends above---n---Message body starts below---
n";
for( $line=0; $line echo "
",HtmlSpecialChars( $body[ $line]),"
n";
echo "
---Message body ends above---
n";
}
}
if( $error==""&&( $error= $pop3_connection->Close())=="")
echo "
Disconnected from the POP3 server " $pop3_connection->hostname".
n";
}
else
 $error= $result;
}
}
}
if( $error!="")
echo "
Error: ",HtmlSpecialChars( $error),"
";
?>


相关专题:暂无相关专题

上一篇:如何去掉文章里的 html 语法
下一篇:php.ini中文解释

共有评论 0 条 网友评分 0分 查看全部

【发表评论】 评分:1分 2分 3分 4分 5分


Powered By Www.Xydw.COM Ver1.14 管理
Copyright © 2004-2005 盛绿设计 All Right Reserved. XCMS
冀ICP备06026128号