Home Backend Development PHP Tutorial 一个简单的(也可以说是不完全的)IMAP类和应用_PHP

一个简单的(也可以说是不完全的)IMAP类和应用_PHP

Jun 01, 2016 pm 12:28 PM
echo if Can application Simple

IMAP

//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 的信箱连接成功。
";
            return $inStream;
        }
        else
        {
            echo "用户:$this->username 的信箱连接失败。
";
            return 0;
        }
    }
    
    Function close()
    {
        if(imap_close($this->inStream))
        {
            echo "
已经与服务器 $this->hostname 断开连接。";
            return 1;
        }
        else
        {
            echo "
与服务器 $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."
";
                echo "未读邮件数:".$mboxinfo->Unread."
";
                //echo "新邮件数:".$mboxinfo->Rescent."
";
                echo "总共占用空间:".$mboxinfo->Size."字节
";
                echo "最新邮件日期:".$mboxinfo->Date."

";
            }
            else
            {
                echo "您的信箱里没有邮件。

";
            }
        else
        {
            echo '错误:无法获取收件箱的信息。';
        }

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

        for ($i=1;$iNmsgs;$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 '';
            echo '';
            echo '';
        }
        echo "
'.$frm.' '.$sub.' '.$date.' '.$msg_size.'
";
    }
    
    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 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 ''.$att_name.'
';
                $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 parts); $i++)
                    {
                        if ($part_no != "")
                        {
                            $part_no = $part_no.".";
                        }
                        for ($i = 0; $i 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.'
';

            // 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","
",imap_qprint(imap_fetchbody($this->inStream,$msg_num,$part_no)));
                    echo '
'.imap_qprint(imap_fetchbody($this->inStream,$msg_num,$part_no)).'
Copy after login
';
                    break;
                case ENCOTHER:
                // not sure if this needs decoding at all
                    break;
                default:
            }
        }
    }


};

?>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
How to write if in c language to judge multiple conditions How to write if in c language to judge multiple conditions Mar 25, 2024 pm 03:24 PM

In C language, if statement is usually used to execute a specific block of code based on a single condition. However, multiple conditions can be combined to make a determination using logical operators such as &&, ||, and !. Including using logical AND (&&) to judge multiple conditions, using logical OR (||) to judge at least one condition, using logical NOT (!) to judge the negation of a single condition, as well as nesting if statements and using parentheses to clarify priority.

How to Undo Delete from Home Screen in iPhone How to Undo Delete from Home Screen in iPhone Apr 17, 2024 pm 07:37 PM

Deleted something important from your home screen and trying to get it back? You can put app icons back on the screen in a variety of ways. We have discussed all the methods you can follow and put the app icon back on the home screen. How to Undo Remove from Home Screen in iPhone As we mentioned before, there are several ways to restore this change on iPhone. Method 1 – Replace App Icon in App Library You can place an app icon on your home screen directly from the App Library. Step 1 – Swipe sideways to find all apps in the app library. Step 2 – Find the app icon you deleted earlier. Step 3 – Simply drag the app icon from the main library to the correct location on the home screen. This is the application diagram

The role and practical application of arrow symbols in PHP The role and practical application of arrow symbols in PHP Mar 22, 2024 am 11:30 AM

The role and practical application of arrow symbols in PHP In PHP, the arrow symbol (->) is usually used to access the properties and methods of objects. Objects are one of the basic concepts of object-oriented programming (OOP) in PHP. In actual development, arrow symbols play an important role in operating objects. This article will introduce the role and practical application of arrow symbols, and provide specific code examples to help readers better understand. 1. The role of the arrow symbol to access the properties of an object. The arrow symbol can be used to access the properties of an object. When we instantiate a pair

From beginner to proficient: Explore various application scenarios of Linux tee command From beginner to proficient: Explore various application scenarios of Linux tee command Mar 20, 2024 am 10:00 AM

The Linuxtee command is a very useful command line tool that can write output to a file or send output to another command without affecting existing output. In this article, we will explore in depth the various application scenarios of the Linuxtee command, from entry to proficiency. 1. Basic usage First, let’s take a look at the basic usage of the tee command. The syntax of tee command is as follows: tee[OPTION]...[FILE]...This command will read data from standard input and save the data to

Explore the advantages and application scenarios of Go language Explore the advantages and application scenarios of Go language Mar 27, 2024 pm 03:48 PM

The Go language is an open source programming language developed by Google and first released in 2007. It is designed to be a simple, easy-to-learn, efficient, and highly concurrency language, and is favored by more and more developers. This article will explore the advantages of Go language, introduce some application scenarios suitable for Go language, and give specific code examples. Advantages: Strong concurrency: Go language has built-in support for lightweight threads-goroutine, which can easily implement concurrent programming. Goroutin can be started by using the go keyword

Understanding MySQL timestamps: functions, features and application scenarios Understanding MySQL timestamps: functions, features and application scenarios Mar 15, 2024 pm 04:36 PM

MySQL timestamp is a very important data type, which can store date, time or date plus time. In the actual development process, rational use of timestamps can improve the efficiency of database operations and facilitate time-related queries and calculations. This article will discuss the functions, features, and application scenarios of MySQL timestamps, and explain them with specific code examples. 1. Functions and characteristics of MySQL timestamps There are two types of timestamps in MySQL, one is TIMESTAMP

The wide application of Linux in the field of cloud computing The wide application of Linux in the field of cloud computing Mar 20, 2024 pm 04:51 PM

The wide application of Linux in the field of cloud computing With the continuous development and popularization of cloud computing technology, Linux, as an open source operating system, plays an important role in the field of cloud computing. Due to its stability, security and flexibility, Linux systems are widely used in various cloud computing platforms and services, providing a solid foundation for the development of cloud computing technology. This article will introduce the wide range of applications of Linux in the field of cloud computing and give specific code examples. 1. Application virtualization technology of Linux in cloud computing platform Virtualization technology

What are the most popular golang frameworks on the market? What are the most popular golang frameworks on the market? Jun 01, 2024 pm 08:05 PM

The most popular Go frameworks at present are: Gin: lightweight, high-performance web framework, simple and easy to use. Echo: A fast, highly customizable web framework that provides high-performance routing and middleware. GorillaMux: A fast and flexible multiplexer that provides advanced routing configuration options. Fiber: A performance-optimized, high-performance web framework that handles high concurrent requests. Martini: A modular web framework with object-oriented design that provides a rich feature set.

See all articles