• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    一個簡單的IMAP類和應用

    發布: 2007-7-14 19:53 | 作者: 佚名    | 來源: 網絡轉載     | 查看: 11次 | 進入軟件測試論壇討論

    領測軟件測試網 <?php
    //by Alpha.Z
    //05/21/2000

    class myimap
    {
            var    $username="";
            var    $userpwd="";
            var    $hostname="";
            var    $port=0;
            var    $connection=0;            //是否連接
            var    $state="DISCONNECTED";        //連接狀態
            var    $greeting="";            
            var    $must_update=0;
            var    $inStream=0;

        Function open()
        {
            if ($this->port==110)
                $this->inStream=imap_open("{$this->hostname/pop3:110}inbox",$this->username,$this->userpwd);
            else
                $this->inStream=imap_open("{$this->hostname/imap:143}inbox",$this->username,$this->userpwd);

            if ($this->inStream)
            {
                echo "用戶:$this->username 的信箱連接成功。<br>";
                return $inStream;
            }
            else
            {
                echo "用戶:$this->username 的信箱連接失敗。<br>";
                return 0;
            }
        }
        
        Function close()
        {
            if(imap_close($this->inStream))
            {
                echo "<hr>已經與服務器 $this->hostname 斷開連接。";
                return 1;
            }
            else
            {
                echo "<hr>與服務器 $this->hostname 斷開連接失敗。";
                return 0;
            }
        }
        
        Function CheckMailbox()
        {
            $mboxinfo=@imap_mailboxmsginfo($this->inStream);
            //$mboxinfo=imap_check($this->inStream);
            if ($mboxinfo)
                if ($mboxinfo->Nmsgs>0)
                {
                    echo "您的收件箱里共有郵件數:".$mboxinfo->Nmsgs."<br>";
                    echo "未讀郵件數:".$mboxinfo->Unread."<br>";
                    //echo "新郵件數:".$mboxinfo->Rescent."<br>";
                    echo "總共占用空間:".$mboxinfo->Size."字節<br>";
                    echo "最新郵件日期:".$mboxinfo->Date."<br><hr>";
                }
                else
                {
                    echo "您的信箱里沒有郵件。<br><hr>";
                }
            else
            {
                echo '<font color="RED">錯誤:無法獲取收件箱的信息。</font>';
            }

            echo '<table border="1">';
            $sortby="SORTDATE";
            $sort_reverse=1;
            $sorted = imap_sort($this->inStream, $sortby, $sort_reverse, SE_UID);

            for ($i=1;$i<=$mboxinfo->Nmsgs;$i++)
            {
                $msgHeader = @imap_header($this->inStream, imap_msgno($this->inStream, $sorted[$i-1]));
                //日期
                if (isset($msgHeader->date))
                {
                    $date = $msgHeader->date;
                    if (ord($date) > 64)
                        $date = substr($date, 5);
                    while (strstr('  ', $date))
                    {
                        $date = str_replace('  ', ' ', $date);
                    }
                }

                if (isset($msgHeader->from[0]))
                {
                    $from = $msgHeader->from[0];
                        if (isset($from->personal))
                        {
                            $frm = trim($from->personal);
                        }
                        else
                            if (isset($from->mailbox) && isset($from->host))
                            {
                                $frm = $from->mailbox . '@' . $from->host;
                            }
                            else
                                if (isset($msgHeader->fromaddress))
                                    $frm = trim($h->fromaddress);
                }
                else
                    if (isset($msgHeader->fromaddress))
                        $frm = trim($msgHeader->fromaddress);

                if (isset($msgHeader->toaddress))
                    $to = trim($msgHeader->toaddress);
                else
                    $to = "未知";
                               

                if (isset($msgHeader->subject))
                    $sub = trim($this->decode_mime_string($msgHeader->subject));
                if (isset($msgHeader->Size))
                    $msg_size = ($msgHeader->Size > 1024) ? sprintf("%.0f kb", $msgHeader->Size / 1024) : $msgHeader->Size;
                if (strlen($frm) > 50)
                    $frm = substr($frm, 0, 50) . '...';
                if (strlen($sub) > 50)
                    $sub = substr($sub, 0, 50) . '...';

                echo '<tr>';
                echo '<td align="center"><input type="checkbox" name="check"></td><td>'.$frm.'</td><td><a href="showbody_imap.php?usr='.$this->username.'&pwd='.$this->userpwd.'&msg='.$i.'">'.$sub.'</a></td><td>'.$date.'</td><td>'.$msg_size.'</td>';
                echo '</tr>';
            }
            echo "</table>";
        }
        
        Function decode_mime_string ($string)
        {
            $pos = strpos($string, '=?');
            if (!is_int($pos))
            {
                return $string;
            }

            $preceding = substr($string, 0, $pos); // save any preceding text

            $search = substr($string, $pos+2, 75); // the mime header spec says this is the longest a single encoded word can be
            $d1 = strpos($search, '?');
            if (!is_int($d1))
            {
                return $string;
            }

            $charset = substr($string, $pos+2, $d1);
            $search = substr($search, $d1+1);

            $d2 = strpos($search, '?');
            if (!is_int($d2))
            {
                return $string;
            }

            $encoding = substr($search, 0, $d2);
            $search = substr($search, $d2+1);

            $end = strpos($search, '?=');
            if (!is_int($end))
            {
                return $string;
            }

            $encoded_text = substr($search, 0, $end);
            $rest = substr($string, (strlen($preceding . $charset . $encoding . $encoded_text)+6));

            switch ($encoding)
            {
                case 'Q':
                case 'q':
                    $encoded_text = str_replace('_', '%20', $encoded_text);
                    $encoded_text = str_replace('=', '%', $encoded_text);
                    $decoded = urldecode($encoded_text);
                    break;

                case 'B':
                case 'b':
                    $decoded = urldecode(base64_decode($encoded_text));
                    break;

                default:
                    $decoded = '=?' . $charset . '?' . $encoding . '?' . $encoded_text . '?=';
                    break;
            }

            return $preceding . $decoded . decode_mime_string($rest);
        }


        Function display_toaddress ($user, $server, $from)
        {
            return is_int(strpos($from, $this->get_barefrom($user, $server)));
        }
            
        Function get_barefrom($user, $server)
        {
            $barefrom = "$user@$real_server";

            return $barefrom;
        }        
        
        Function get_structure($msg_num)
        {
            $structure=imap_fetchstructure($this->inStream,$msg_num);
            //echo gettype($structure);
            return $structure;
        }

        Function proc_structure($msg_part, $part_no, $msg_num)
        {
            if ($msg_part->ifdisposition)
            {
            // See if it has a disposition
            // The only thing I know of that this
            // would be used for would be an attachment
            // Lets check anyway
                if ($msg_part->disposition == "ATTACHMENT")
                {
                    // If it is an attachment, then we let people download it
                    // First see if they sent a filename
                    $att_name = "unknown";
                    for ($lcv = 0; $lcv < count($msg_part->parameters); $lcv++)
                    {
                        $param = $msg_part->parameters[$lcv];

                        if ($param->attribute == "NAME")
                        {
                            $att_name = $param->value;
                                        break;
                        }
                    }

                    // You could give a link to download the attachment here....
                    echo '<a href="'.$att_name.'">'.$att_name.'</a><br>';
                    $fp=fopen(".\\$att_name","w+");
                    fputs($fp,imap_base64(imap_fetchbody($this->inStream,$msg_num,$part_no)));
                    fclose($fp);
                }
                else
                {
                    // I guess it is used for something besides attachments????
                }
            }
            else
            {
                // Not an attachment, lets see what this part is...
                switch ($msg_part->type)
                {
                    case TYPETEXT:
                        $mime_type = "text";
                        break;
                    case TYPEMULTIPART:
                        $mime_type = "multipart";
                        // Hey, why not use this function to deal with all the parts
                        // of this multipart part :)
                        for ($i = 0; $i < count($msg_part->parts); $i++)
                        {
                            if ($part_no != "")
                            {
                                $part_no = $part_no.".";
                            }
                            for ($i = 0; $i < count($msg_part->parts); $i++)
                            {
                                $this->proc_structure($msg_part->parts[$i], $part_no.($i + 1), $msg_num);
                            }
                        }
                        break;
                    case TYPEMESSAGE:
                        $mime_type = "message";
                        break;
                    case TYPEAPPLICATION:
                        $mime_type = "application";
                        break;
                    case TYPEAUDIO:
                        $mime_type = "audio";
                        break;
                    case TYPEIMAGE:
                        $mime_type = "image";
                        break;
                    case TYPEVIDEO:
                        $mime_type = "video";
                        break;
                    case TYPEMODEL:
                        $mime_type = "model";
                        break;
                    default:
                        $mime_type = "unknown";
                        // hmmm....
                }

                $full_mime_type = $mime_type."/".$msg_part->subtype;
                //echo $full_mime_type.'<hr>';

                // Decide what you what to do with this part
                // If you want to show it, figure out the encoding and echo away
                switch ($msg_part->encoding)
                {
                    case ENCBASE64:
                    // use imap_base64 to decode
                        $fp=fopen(".\\$att_name","w+");
                        fputs($fp,imap_base64(imap_fetchbody($this->inStream,$msg_num,$part_no)));
                        fclose($fp);
                        break;
                    case ENCQUOTEDPRINTABLE:
                    // use imap_qprint to decode
                        //echo ereg_replace("\n","<br>",imap_qprint(imap_fetchbody($this->inStream,$msg_num,$part_no)));
                        echo '<pre>'.imap_qprint(imap_fetchbody($this->inStream,$msg_num,$part_no)).'</pre>';
                        break;
                    case ENCOTHER:
                    // not sure if this needs decoding at all
                        break;
                    default:
                }
            }
        }


    };

    ?>

    use_imap.php (前面那個應該是 imap_new.php)

    <HTML>
    <HEAD>
    <style type="text/css">
    <!--
    td { font-size:9pt}
    a {font-szie:9pt}
    body {font-size:9pt}
    input {font-size:9pt}
    select {font-size:9pt}
    -->
    </style>
    <title>PHP WebMail System</title>
    </HEAD>
    <BODY bgcolor="#FFFFFF">

    <?php
    include("imap_new.php");
    $imap=new myimap;
    $imap->hostname="192.168.100.13";
    $imap->port=110;
    $imap->username="Alpha";
    $imap->userpwd="enter";
    $imap->open();
    $imap->CheckMailBox();
    //$imap->GetMailStructure($imap->inStream,1);
    @$imap->close();

    ?>

    </body>
    </html>

    showbody_imap.php

    <HTML>
    <HEAD>
    <TITLE>Biz2Biz WebMail System</TITLE>
    <style type="text/css">
    <!--
    td { font-size:9pt}
    a {font-szie:9pt}
    body {font-size:9pt}
    input {font-size:9pt}
    select {font-size:9pt}
    -->
    </style>
    </HEAD>
    <BODY>
    <?php
    include("imap_new.php");
    $imap=new myimap;
    $imap->hostname="192.168.100.13";
    $imap->port=110;
    $imap->username=$usr;
    $imap->userpwd=$pwd;
    $imap->open();
    $mail_structure=$imap->get_structure($msg);
    //echo gettype($mail_structure->parts);
    //echo count($mail_structure->parts);
    $imap->proc_structure($mail_structure,"",$msg);

    @$imap->close();
    ?>
    </body>
    </html>

    文章來源于領測軟件測試網 http://www.kjueaiud.com/


    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備10010545號-5
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>