MIME encoding methods
(Author: Chen Junqing October 24, 2000)
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 method of the mail was uencode and so on. However, the MIME protocol algorithm is simple and easy to expand, nowadays, 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 images and audios in the email attachments, it also expands many MIME-based applications. 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;
}
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.