Using PHP to decode POP3 messages two _php tutorial

Source: Internet
Author: User
Introduction to MIME encoding method (Author: Chen Junqing October 24, 2000 15:09) Introduction to MIME encoding method Subject: =?gb2312? B?xoo6w6oh?= here is the subject of the mail, but because of the code, we do not see what the content, its original text is: "Hello!"   "Let's look at the two methods of MIME encoding first." The initial reason for encoding a message is because many gateways on the Internet are not able to properly transmit 8 bit-encoded characters, such as Chinese characters.   The principle of encoding is to convert the contents of the 8 bit into a 7 bit form in order to transmit correctly, after receiving the receiver, and then restore it to 8 bit of content. MIME is a "Multipurpose Internet Mail Extension Protocol" abbreviation, before the MIME protocol, the message encoding has been UUENCODE and other encoding methods, but because the MIME protocol algorithm is simple, and easy to expand, has now become the mainstream of the message encoding, not only for the transmission of 8 bit characters, but also can Used to transmit binary files, such as images, audio, and other information in email attachments, and to extend many MIME-based applications. In terms of encoding, MIME defines two encoding methods, Base64 and QP (quote-printable): Base 64 is a common method, and the principle is simple, that is, three byte data is represented by 4 bytes, so that four bytes, Only the first 6 bits are actually used, so there is no problem of transmitting only 7bit characters.   The abbreviation for Base 64 is generally "B", as the subject in this letter is the BASE64 code. Another method is the QP (quote-printable) method, which is usually abbreviated as the "Q" method, which is the principle of using a 8 bit character as a two 16 binary value, and then adding "=" in front.   So we see that the QP encoded file usually looks like this: =B3=C2=BF=A1=C7=E5=A3=AC=C4=FA=BA=C3=A3=A1.   In PHP, the system has two functions that can be easily decoded: Base64_decode () and Quoted_printable_decode (), the former can be used for Base64 encoding decoding, the latter is used for QP encoding method decoding. Now let's take a look at subject: =?gb2312? B?xoo6w6oh?= the content of this topic, this is not a complete coding, only part of the code, this part with =?? = two tags, =? This is the article that followsThe character set is GB2312, and then one? One of the following B represents the BASE64 encoding used. With this analysis, let's take a look at this MIME decoding function: (This function is provided by phpx.com Webmaster Sadly, I put it into a class, and made a few changes, in this acknowledgment) function Decode_mime ($string) {$pos    = Strpos ($string, =?);    if (!is_int ($pos)) {return $string; } $preceding = substr ($string, 0, $pos); Save any preceding text $search = substr ($string, $pos +2);    /* The MIME header spec says the longest a single encoded word can */$d 1 = Strpos ($search,?);    if (!is_int ($d 1)) {return $string; } $charset = substr ($string, $pos +2, $d 1); Remove the definition part of the character set $search = substr ($search, $d);    The character set defines the later part and the $search;    $d 2 = Strpos ($search,?);    if (!is_int ($d 2)) {return $string; } $encoding = substr ($search, 0, $d 2); Two?    Part of the encoding between: Q or b $search = substr ($search, $d 2+1); $end = Strpos ($search,? =);    $d between the 2+1 and the $end is encoded content:=> $endcoded _text;    if (!is_int ($end)) {return $string;    } $encoded _text = substr ($search, 0, $end); $rest = substr($string, (strlen ($preceding. $charset. $encoding. $encoded _text) +6)); +6 is the front removed =????      = Six characters switch ($encoding) {case Q:case Q://$encoded _text = Str_replace (_,%20, $encoded _text);      $encoded _text = str_replace (=,%, $encoded _text);    $decoded = UrlDecode ($encoded _text);      $decoded =quoted_printable_decode ($encoded _text);      if (Strtolower ($charset) = = windows-1251) {$decoded = Convert_cyr_string ($decoded, W, K);    } break;      Case B:case B: $decoded = Base64_decode ($encoded _text);      if (Strtolower ($charset) = = windows-1251) {$decoded = Convert_cyr_string ($decoded, W, K);    } break; Default: $decoded = =?. $charset. ? . $encoding. ? . $encoded _text.      ?=;    Break } return $preceding. $decoded.   $this->decode_mime ($rest); This function uses a recursive method to implement decoding of a character containing a Subject segment. Comments have been added to the program. People who believe a bit of PHP programming basics can see it. This function is also decoded by the invocation of the Base64_decode () and Quoted_printable_decode () two system functions, but requires a large number of string parsing of the message source file. However, the characters of PHPString manipulation can be considered the most convenient and free in all languages. The last return $preceding of the function. $decoded. $this->decode_mime ($rest);   Recursive decoding is implemented because this function is actually placed in a mime-decoded class to be introduced later, so the $this->decode_mime ($rest) is used in this form of invocation method. Let's look at the text below.   Here are some of the MIME's header information, we do a simple introduction (if the reader is interested in learning more about the content, please refer to the official MIME document).   mime-version:1.0 indicates the version number of the MIME used, which is typically 1.0; Content-type: Defines the type of body, we actually use this identity to know what type of file in the body, such as: Text/plain represents the unformatted text body, text/html represents the HTML document, Image/gif is the GIF   Format of the picture and so on. In particular, the compound types that are commonly used in messages are described in this article. The multipart type indicates that the body is made up of multiple parts, followed by a subtype that describes the relationship between these parts, the three types that are used in the message, Multipart/alternative: The body consists of two parts, and you can choose any of them. The main function is that when the essay has both text format and HTML format, you can select one of the two bodies to display, the HTML-enabled mail client software will generally display its HTML body, but not support will display its text text; Multipart/mixed: Represents the document's multiple The part is mixed, which refers to the relationship between the body and the attachment.   If the MIME type of the message is multipart/mixed, that means the message has an attachment; Multipart/related: Indicates that multiple parts of the document are relevant and are generally used to describe the Html body associated with the picture. These composite types can also be nested use, such as a message with an attachment, as well as HTML and text two format body, the structure of the message is: content-type:multipart/mixed Part one: Content type:mu Ltipart/alternative:text body Part Two: Attachment message Terminator; Because the compound type consists of multiple parts,Therefore, a delimiter is required to separate the multiple sections, which is described in the boundary= "----=_nextpart_000_0007_01c03166.5b1e9510" in the message source file above, for each of the Contect type: multipart/* content, there will be a description of the separation between multiple parts, this delimiter is not possible in the body of a series of antiquity characters, in the document, with "--" plus this boundary to represent the beginning of a part, at the end of the document to "--" Add boundary and finally add "--" to indicate the end of the document.   Because composite types can be nested, multiple boundary can be used in messages. There is also one of the most important MIME header tags: content-transfer-encoding:base64 it represents the encoding of this part of the document, which is the Base64 or QP (quote-printable) described above.   Only by identifying this description can we decode it using the correct decoding method. Confined to space, the introduction to MIME is only here. I'll give you a class that decodes the MIME message and gives a brief description of it.

http://www.bkjia.com/PHPjc/531920.html www.bkjia.com true http://www.bkjia.com/PHPjc/531920.html techarticle Introduction to MIME encoding method (Author: Chen Junqing October 24, 2000 15:09) Introduction to MIME encoding method Subject: =?gb2312? B?xoo6w6oh?= here is the subject of the mail, but because of the code, I ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.