连接pop3邮件服务器的类
<?php

/*
这是一个连接pop3邮件服务器的类。以RFC1939标准为基础开发。
后面有一个例子说明其用法
*/

class POP3 {
    var 
$server;    // pop3服务器地址
    
var $port 110;// pop3服务器端口
    
var $sockfd;    // Socket 描述
    
var $user;      // pop3邮箱用户名
    
var $passwd;    // pop3邮箱口令
    
var $answer;    // pop3服务器应答
    
var $mailsize;  // 邮件大小
    
var $mailno;    // 邮件编号
    
var $mailboxno// 邮件数量
    
var $mailboxsize// 邮箱已用容量
    
var $debug true// Debug标志
    
    /*
        bool cmdOK(): Return true if the server validates the cmd, else return false;
    */
    
function cmdOK() {
        
$this->answer fgets($this->sockfd2048);
        if(
$this->debug) echo "nS: $this->answer";
        if(
ereg("^+OK"$this->answer))
            return 
true;
        else
            return 
false;
    }
    
    function 
sendCmd($cmd$param1 ''$param2 '') {
        if(
$this->debug) echo "nC:$cmd $param1 $param2";
        
fwrite($this->sockfdtrim("$cmd $param1 $param2") . "rn");
    }
    
    function 
nextAnswer() {
        
$line fgets($this->sockfd2048);
        return (
ereg("^.rn$"$line)?false:$line);
    }
            
    
/*
        bool pop3_connect(blocking mode): Connects to POP3 server and return true if succesful, else return false
    */
    
function pop3_connect($block 'true') {
        if(
$this->sockfd fsockopen($this->server$this->port)) {
            if(
$this->cmdOK()) {
                
//set_socket_blocking($this->sockfd, $block);
                
return true;
            }
        }
        return 
false;
    }
    
    function 
pop3_disconnect() {
        
fwrite($this->sockfd"QUITrn");
        
$this->cmdOK();
        
fclose($this->sockfd);
    }
    
    function 
pop3_login() {
        
$this->sendCmd("USER"$this->user);
        if(
$this->cmdOK()) {
            
$this->sendCmd("PASS"$this->passwd);
            if(
$this->cmdOK()) {
                return 
true;
            }
        }
        return 
false;
    }
    
    function 
pop3_stat() {
        
$this->sendCmd("STAT");
        
$state $this->cmdOK();
        if(
ereg("^+OK ([0-9]+) ([0-9]+)"$this->answer$aux)) {
            
$this->mailboxno $aux[1];
            
$this->mailboxsize $aux[2];
        }
        return 
$state;
    }
    
    function 
pop3_list($mailno '') {
        
$this->sendCmd("LIST"$mailno);
        
$state $this->cmdOK();
        if(
ereg("^+OK ([0-9]+) ([0-9]+)"$this->answer$aux)) {
            
$this->mailno $aux[1];
            
$this->mailsize $aux[2];
        }
        return 
$state;
    }
    
    function 
pop3_top($mailno 1$lines 0) {
        
$this->sendCmd("TOP"$mailno$lines);
        return 
$this->cmdOK();
    }
    
    function 
pop3_retr($mailno 1) {
        
$this->sendCmd("RETR"$mailno);
        return 
$this->cmdOK();
    }
    
    function 
pop3_dele($mailno 1) {
        
$this->sendCmd("DELE"$mailno);
        return 
$this->cmdOK();
    }
    
    function 
pop3_rset() {
        
$this->sendCmd("RSET");
        return 
$this->cmdOK();
    }
    
    function 
pop3_noop() {
        
$this->sendCmd("NOOP");
        return 
$this->cmdOK();
    }
    
    function 
pop3_uidl($mailno '') {
        
$this->sendCmd("UIDL"$mailno);
        return 
$this->cmdOK();
    }
}

//例子
/*
include('./pop3.class.inc');

$pop3 = new POP3;
$pop3->server = "your.mail.server";
$pop3->user   = "you";
$pop3->passwd = "youknow";
$pop3->debug = true; // 可查看命令和服务器应答内容

header("Content-type: text/plain");
if($pop3->pop3_connect()) {
    $pop3->pop3_login();
    $pop3->pop3_stat();
    if($pop3->pop3_list())
        while($line = $pop3->nextAnswer())
            echo $line;
    $pop3->pop3_list(1);
    if($pop3->pop3_top(1,0))
        while($line = $pop3->nextAnswer())
            echo $line;
    if($pop3->pop3_retr(1))
        while($line = $pop3->nextAnswer())
            echo $line;
    $pop3->pop3_disconnect();
}
*/
?>