Use PHP to decode POP3 emails (2)

Source: Internet
Author: User
Tags php source code

Introduction: This is a detailed page for PHP to implement POP3 mail decoding (2). It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 324423 'rolling = 'no'>

Mime encoding methods

Mime encoding methods

Subject: =? Gb2312? B? Xoo6w6oh? =

This is the subject of the email, but we cannot see what it is because of encoding. The original text is: "Hello !" Let's first look at the two mime encoding methods.

The initial cause of email encoding is that many gateways on the Internet cannot correctly transmit 8-bit characters, such as Chinese characters. The encoding principle is to convert 8-bit content into a 7-bit format for correct transmission. After receiving the content, the receiver restores it to 8-bit content.

Mime is the abbreviation of "multi-purpose Internet Mail extended protocol". Before the mime protocol, the encoding of the mail was used by uencode and Other encoding methods. However, due to the mime protocolAlgorithmIt is simple and easy to expand. It has become the mainstream of mail encoding methods. It is used not only to transmit 8-bit characters, but also to transmit binary files, such as the image and Audio Information in the attachment, and many mime-based applications are extended. In terms of the encoding method, mime defines two encoding methods: base64 and qP (Quote-printable ):

Base 64 is a general method, and its principle is very simple. It is to use four bytes to represent the data of three bytes. In this way, in the four bytes, actually, only the first 6 bits are used, so there is no problem that only 7 bits can be transmitted. The abbreviation of Base 64 is generally "B", as the subject in this letter uses base64 encoding.

Another method is the quote-printable (qP) method, which is abbreviated as "Q". The principle is to use two hexadecimal values to represent an 8-bit character, then add "=" to the front ". So we can see that the QP-encoded file is usually 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 decoding the QP encoding method.

Now let's take a look at subject: =? Gb2312? B? Xoo6w6oh? = The content of this topic, which is not a complete encoding, is only partially encoded. This part uses =? ? = Two tags are enclosed, =? The character set of this text is gb2312, and then? The following B indicates the base64 encoding. Through this analysis, let's look at the mime-decoded function: (this function is composed of phpx. com webmaster sadly provided, I put it into a class, and made a small number of modifications, here Thank you)

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 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); // retrieves the definition part of the character set.

$ Search = substr ($ search, $ D1 + 1); // The part after the character set is defined >$ search;

$ D2 = strpos ($ search ,'? ');

If (! Is_int ($ D2 )){

Return $ string;

}

$ Encoding = substr ($ search, 0, $ D2); // two? Part of the encoding method: Q or B

$ Search = substr ($ search, $ D2 + 1 );

$ End = strpos ($ search ,'? = '); // $ D2 + 1 and $ end are 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 previously 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 decode a section of characters that contain subject segments.ProgramAnnotations have been added. I believe that PHP programmers can understand the basics. This function is also called to decode base64_decode () and quoted_printable_decode (). However, you need to analyze a large number of strings in the mail source file. However, PHP string operations are the most convenient and free in all languages. Return $ preceding. $ decoded. $ this-> decode_mime ($ rest); implements recursive decoding, because this function is actually placed in a MIME Decoding class to be introduced later, therefore, the call method in the form of $ this-> decode_mime ($ rest) is used.

Let's look at the text below. Here is a brief introduction to some MIME header information. (For more information, see the official mime documentation ).

Mime-type: 1.0

Indicates the MIME Version used, which is generally 1.0;

Content-Type: defines the type of the body. We actually use this identifier to know what type of file is in the body. For example, text/plain indicates the unformatted text body, text/html: HTML documents. Image/GIF: Images in GIF format. In this article, it is particularly important to note the composite types commonly used in emails. The multipart type indicates that the body is composed of multiple parts. The subtypes below indicate the relationship between these parts. The three types used in the mail are multipart/alternative: the body is composed of two parts. You can select either of them. The main function is to select one of the two texts for display when both the text format and the HTML format are available. The mail client software that supports the HTML format usually displays the html body, if not, the text body is displayed. multipart/mixed indicates that multiple parts of the document are mixed, indicating the relationship between the body and the attachment. If the MIME type of an email is multipart/mixed, it indicates that the email carries an attachment; multipart/related: It indicates that multiple parts of the document are related and are generally used to describe the images related to the html body.

These composite types can be nested. For example, if an email with attachments and body in HTML and text formats exist, the mail structure is:

Content-Type: multipart/mixed

Part 1:

Content type: multipart/alternative:

Text text;

Body in HTML Format

Part 2:

Attachment

Email Terminator;

Because the composite type consists of multiple parts, a separator is required to separate these parts. This is described in the preceding mail source file boundary = "---- = _ nextpart_000_0007_01c01_6.5b1e9510, for each contect type: multipart/* content, there will be such a description, indicating the separation between multiple parts. This Delimiter is a combination of a string of ancient characters that cannot appear in the body, in the document, add the boundary to "--" to indicate the beginning of a part. At the end of the document, add "--" and boundary "--" at the end to indicate the end of the document. Because composite types can be nested, there may be multiple boundary in the mail.

There is also one of the most important MIME header labels:

Content-transfer-encoding: base64 indicates the encoding method of this part of the document, that is, the base64 or QP (Quote-printable) we mentioned above ). Only by recognizing this description can we use the correct decoding method to decode it.

Only the introduction of mime is mentioned here. Next I will give a class for decoding mime mail and give a brief description of it.

Author: Chen junqing
Reprinted: chinacnet

More articles about "using PHP to decode POP3 emails (2)"

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/324423.html pageno: 14

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.