仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2067|回复: 19
打印 上一主题 下一主题

[学习教程] PHP网站制作之pop3邮件收取一例

[复制链接]
深爱那片海 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-4 00:04:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
到现在,对排版还是不很熟练,经常会排不好。   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 "$pop3_connection->hostname".</PRE>\n";
   if(($error=$pop3_connection->Login($user,$password,$apop))=="")
   {
      echo "<PRE>User "$user" 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 "$pop3_connection->hostname".</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("");
        }

};

?>
兴趣可能会慢慢消亡,所以适当培养兴趣会激发自己无线的乐趣,有了乐趣,编程有啥难的。
乐观 该用户已被删除
沙发
发表于 2015-2-4 08:41:12 | 只看该作者
最后介绍一个代码出错,但是老找不到错误方法,就是 go to wc (囧),出去换换气没准回来就找到错误啦。
小女巫 该用户已被删除
板凳
发表于 2015-2-9 20:18:40 | 只看该作者
开发工具也会慢慢的更专业,每个公司的可能不一样,但是zend studio是个大伙都会用的。
再见西城 该用户已被删除
地板
发表于 2015-2-13 21:57:52 | 只看该作者
最后介绍一个代码出错,但是老找不到错误方法,就是 go to wc (囧),出去换换气没准回来就找到错误啦。
活着的死人 该用户已被删除
5#
发表于 2015-3-2 10:10:08 | 只看该作者
最后祝愿,php会给你带来快乐的同时 你也会给他带来快乐。
第二个灵魂 该用户已被删除
6#
发表于 2015-3-7 20:29:21 | 只看该作者
再就是混迹于论坛啦,咱们的phpchina的论坛就很强大,提出的问题一般都是有达人去解答的,以前的帖子也要多看看也能学到不少前辈们的经验。别的不错的论坛例如php100,javaeye也是很不错的。
不帅 该用户已被删除
7#
发表于 2015-3-15 13:08:20 | 只看该作者
这些中手常用的知识,当你把我说的这些关键字都可以熟练运用的时候,你可以选择自己
谁可相欹 该用户已被删除
8#
发表于 2015-3-18 00:21:28 | 只看该作者
这些都是最基本最常用功能,我们这些菜鸟在系统学习后,可以先对这些功能深入研究。
兰色精灵 该用户已被删除
9#
发表于 2015-3-24 18:21:25 | 只看该作者
使用 jquery 等js框架的时候,要随时注意浏览器的更新情况,不然很容易发生框架不能使用。
若相依 该用户已被删除
10#
发表于 2015-4-12 22:33:03 | 只看该作者
多看优秀程序员编写的代码,仔细理解他们解决问题的方法,对自身有很大的帮助。
柔情似水 该用户已被删除
11#
发表于 2015-4-15 01:28:02 | 只看该作者
基础有没有对学习php没有太大区别,关键是兴趣。
简单生活 该用户已被删除
12#
发表于 2015-4-24 09:54:04 | 只看该作者
首先我是坚决反对新手上来就用框架的,因为对底层的东西一点都不了解,造成知识上的真空,会对以后的发展不利。我的观点上手了解下框架就好,代码还是手写。当然啦如果是位别的编程语言的高手的话,这个就另当别论啦。
深爱那片海 该用户已被删除
13#
 楼主| 发表于 2015-5-3 15:07:45 | 只看该作者
微软最近出的新字体“微软雅黑”,虽然是挺漂亮的,不过firefox  支持的不是很好,所以能少用还是少用的好。
愤怒的大鸟 该用户已被删除
14#
发表于 2015-5-6 10:10:44 | 只看该作者
我还是强烈建议自己搭建php环境。因为在搭建的过程中你会遇到一些问题,通过搜索或是看php手册解决问题后,你会更加深刻的理解它们的工作原理,了解到php配置文件中的一些选项设置。
透明 该用户已被删除
15#
发表于 2015-5-6 12:09:39 | 只看该作者
开发工具也会慢慢的更专业,每个公司的可能不一样,但是zend studio是个大伙都会用的。
精灵巫婆 该用户已被删除
16#
发表于 2015-6-4 02:06:34 | 只看该作者
多看优秀程序员编写的代码,仔细理解他们解决问题的方法,对自身有很大的帮助。
只想知道 该用户已被删除
17#
发表于 2015-6-16 20:55:07 | 只看该作者
这些中手常用的知识,当你把我说的这些关键字都可以熟练运用的时候,你可以选择自己
因胸联盟 该用户已被删除
18#
发表于 2015-7-7 14:19:56 | 只看该作者
如果你可以写完像留言板这样的程序,那么你可以去一些别人的代码了,
小妖女 该用户已被删除
19#
发表于 2015-7-22 07:23:33 | 只看该作者
说php的话,首先得提一下数组,开始的时候我是最烦数组的,总是被弄的晕头转向,不过后来呢,我觉得数组里php里最强大的存储方法,所以建议新手们要学好数组。
若天明 该用户已被删除
20#
发表于 2015-10-2 08:41:57 | 只看该作者
写的比较杂,因为我也是个新手,不当至于大家多多指正。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-2 23:14

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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