首页 > 开发 > PHP > 正文

pop3邮件收取一例

2024-05-04 23:01:54
字体:
来源:转载
供稿:网友
test_pop3.php

<html>
<head>
<title>test for manuel lemos's php pop3 class</title>
</head>
<body>
<?
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 "<pre>connected to the pop3 server &quot;$pop3_connection->hostname&quot;.</pre>/n";
   if(($error=$pop3_connection->login($user,$password,$apop))=="")
   {
      echo "<pre>user &quot;$user&quot; logged in.</pre>/n";
      if(($error=$pop3_connection->statistics(&$messages,&$size))=="")
      {
         echo "<pre>there are <b>$messages</b> messages in the mail box with a total of <b>$size</b> bytes.</pre>/n";
         $result=$pop3_connection->listmessages("",0);
         if(gettype($result)=="array")
         {
            for(reset($result),$message=0;$message<count($result);next($result),$message++)
               echo "<pre>message ",key($result)," - ",$result[key($result)]," bytes.</pre>/n";
             if($messages>0)
            {
                if(($error=$pop3_connection->retrievemessage(1,&$headers,&$body,-1))=="")
                {
                  echo "<pre>message 1:/n---message headers starts below---</pre>/n";
                  for($line=0;$line<count($headers);$line++)
                     echo "<pre>",htmlspecialchars($headers[$line]),"</pre>/n";
                  echo "<pre>---message headers ends above---/n---message body starts below---</pre>/n";
                  for($line=0;$line<count($body);$line++)
                     echo "<pre>",htmlspecialchars($body[$line]),"</pre>/n";
                  echo "<pre>---message body ends above---</pre>/n";
                     
                  }
               }
             if($error==""&&($error=$pop3_connection->close())=="")
                  echo "<pre>disconnected from the pop3 server &quot;$pop3_connection->hostname&quot;.</pre>/n";
         }
        else
           $error=$result;
      }
   }
}
if($error!="")
   echo "<h2>error: ",htmlspecialchars($error),"</h2>";
?>
</body>
</html>


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,"<br>/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)=="/r/n")
                        {
                                $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,"$line/r/n"));
        }

        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("/r/n");
                $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("/r/n"));
                }
                $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("/r/n"));
                }
                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("/r/n"));
                        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("/r/n"));
                }
                $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("/r/n"));
                $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("/r/n"));
                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("/r/n"));
                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("");
        }

};

?>


收集最实用的网页特效代码!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表