Decoding of POP3 messages with PHP (II.)

Source: Internet
Author: User
Tags base64 character set mail

Introduction to MIME Encoding method

Subject: =?gb2312? b?xoo6w6oh?=

Here is the subject of the message, but because the code, we do not see what is the content, its original text is: "Hello!" "Let's look at two ways of MIME coding first."

The first reason for encoding a message is that many gateways on the Internet do not correctly transmit characters in 8 bit encodings, such as Chinese characters. The principle of coding is to convert 8 bit content into 7 bit form to transmit correctly, and then revert it to 8 bit content after receiving.

MIME is the abbreviation for the Multipurpose Internet Mail Extension protocol, before the MIME protocol, message encoding used to encode UUENCODE, but because the MIME protocol algorithm is simple and easy to expand, it is now the mainstream of message encoding, not only for the transmission of 8 bit characters, but also Used to transmit binary files, such as images, audio, and other information in the mail attachment, and extended 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 in 4 byte, so that the four byte, the actual use of only the front 6 bit, so there is no only 7bit characters to transfer the problem. The abbreviation for Base 64 is generally "B", as the subject in this letter is the BASE64 encoding used.

Another method is the QP (quote-printable) method, usually abbreviated as a "Q" method, with the principle of representing a 8 bit character in two 16 numeric values and then adding "=" to the front. So we see the QP encoded file is usually this way: =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 (), which can be used for Base64 encoded decoding, which is used to decode the QP encoding method.

Now let's take a look at subject: =?gb2312? B?xoo6w6oh?= the content of this topic, this is not a complete code, only part is encoded, this part with =?? = two tags around, =? What follows is that the character set of this text is GB2312, and then one? A later B represents the BASE64 encoding used. Through this analysis, we 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 small amount of changes, thanks here)

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 are 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 section of the character set
$search = substr ($search, $d 1+1); The character set defines the later part of 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, '? = '); Between the $d 2+1 and the $end is the 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);
}

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.