search
HomeBackend DevelopmentPHP TutorialUse PHP to decode POP3 emails 2_PHP tutorial
Use PHP to decode POP3 emails 2_PHP tutorialJul 13, 2016 pm 05:26 PM
phppop3stwoauthoraccomplishWayuseofIntroductioncodingdecodingmail

Introduction to MIME encoding method (Author: Chen Junqing, October 24, 2000 15:09) Introduction to MIME encoding method Subject: =?gb2312?B?xOO6w6Oh?= This is the subject of the email, but because it is encoded, we cannot see what it is. Content, its original text is: "Hello!" Let's first look at the two methods of MIME encoding. The original reason for encoding emails is because many gateways on the Internet cannot correctly transmit 8-bit internal code characters, such as Chinese characters. The principle of encoding is to convert 8-bit content into 7-bit form so that it can be transmitted correctly, and then restore it to 8-bit content after the receiver receives it. MIME is the abbreviation of "Multipurpose Internet Mail Extensions". Before the MIME protocol, there were encoding methods such as UUENCODE for mail encoding. However, because the MIME protocol algorithm is simple and easy to expand, it has now become the mainstream mail encoding method. Not only It is used to transmit 8-bit characters, and can also be used to transmit binary files, such as images, audio and other information in email attachments, and has expanded many MIME-based applications. In terms of encoding methods, MIME defines two encoding methods: Base64 and QP (Quote-Printable): Base 64 is a universal method, and its principle is very simple, that is, three Byte data is represented by 4 Byte, so , among these four Bytes, only the first 6 bits are actually used, so there is no problem that only 7-bit characters can be transmitted. The abbreviation of Base 64 is usually "B". The Subject in this letter uses Base64 encoding. Another method is the QP (Quote-Printable) method, usually abbreviated as "Q" method. Its principle is to represent an 8-bit character with two hexadecimal values, and then add "=" in front.So we see that the file after QP encoding 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 easily implement decoding: base64_decode() and quoted_printable_decode(). The former can be used for decoding base64 encoding, and the latter is used for decoding QP encoding method. Now let's take a look at the content of Subject: =?gb2312?B?xOO6w6Oh?=. This is not a complete encoding, only part of it is encoded. This part is enclosed by two marks =? ?=, = ? What is explained later is that the character set of this text is GB2312, and a B after a ? represents the Base64 encoding. Through this analysis, let’s take a look at this MIME decoding function: (This function is provided by Sadly, the webmaster of PHPX.COM. I put it into a class and made a few modifications. I would like to 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); //Get the definition part of the character set $search = substr($search, $d1+1); / /The part after the character set definition =>$search;  $d2 = strpos($search, ?);  if (!is_int($d2)) {   return $string;  }  $encoding = substr($search, 0, $d2 ); ////Part of the encoding method between two?: q or b $search = substr($search, $d2+1); $end = strpos($search, ?=); //$d2+ Between 1 and $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 previous 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); (strtolower($charset) == windows-1251) { $decoded = convert_cyr_string($decoded, w, k); $decoded = convert_cyr_string($decoded, w, k); ?=; break; } Return $preceding . $decoded . $this->decode_mime($rest); } } This function uses a recursive method to decode a character containing the above Subject segment. Comments have been added to the program. I believe anyone with some basic knowledge of PHP programming can understand it. This function is also decoded by calling the two system functions base64_decode() and quoted_printable_decode(), but it requires a large amount of string analysis on the email source file. However, PHP's string operations can be regarded as the most convenient and free among all languages. The final return of the function $preceding . $decoded . $this->decode_mime($rest); implements recursive decoding. Because this function is actually placed in a MIME decoding class to be introduced later, $this- is used. >Decode_mime($rest) This form of calling method. Now let’s look at the text. This is related to some header information of MIME. Let’s give a brief introduction first (if readers are interested in learning more, please refer to the official documentation of MIME). MIME-Version: 1.0  Indicates the version number of MIME used, usually 1.0;  Content-Type: Defines the type of text. We actually use this identifier to know what type of file the text is, for example: text/plain means is the unformatted text body, text/html represents the Html document, image/gif represents the image in gif format, etc. What needs to be explained in this article is the compound types commonly used in emails. The multipart type indicates that the text is composed of multiple parts. The following subtypes describe the relationship between these parts. The three types used in emails are: multipart/alternative: indicates that the text is composed of two parts, which can be selected. Any of them. The main function is that when the essay has both text format and html format, you can choose one of the two bodies to display. Mail client software that supports the html format will generally display its HTML body, while those that do not support it will display its Text body. ; multipart/mixed: Indicates that multiple parts of the document are mixed, referring to the relationship between the text and attachments.If the MIME type of the email is multipart/mixed, it means that the email contains attachments; multipart/related: means that multiple parts of the document are related, generally used to describe the Html text and its related images. These composite types can be nested. For example, if an email contains an attachment and has body text in both HTML and text formats, the structure of the email is: Content-Type: multipart/mixed Part 1: Content Type: multipart/alternative: Text text; Text part two in Html format: Attachment email end character; Since the composite type is composed of multiple parts, a delimiter is needed to separate these multiple parts, which is what is in the email source file above As described by boundary="----=_NextPart_000_0007_01C03166.5B1E9510", for each content of Contact type: multipart/*, there will be such a description, indicating the separation between multiple parts. This separator is not in the text. A possible combination of a string of ancient characters. In the document, "--" plus the boundary is used to indicate the beginning of a section. At the end of the document, "--" is added to the boundary and then "--" is added at the end. " to indicate the end of the document. Since composite types can be nested, there may be multiple boundaries in the email. There is also the most important MIME header tag: Content-Transfer-Encoding: base64 It indicates the encoding method of this part of the document, which is the Base64 or QP (Quote-Printable) we introduced above. Only by identifying this description can we decode it using the correct decoding method. Due to space limitations, this is the only introduction to MIME. Below I will give a class for decoding MIME emails and give a brief description of it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531920.htmlTechArticleIntroduction to MIME encoding method (Author: Chen Junqing, October 24, 2000 15:09) Introduction to MIME encoding method Subject: =?gb2312?B?xOO6w6Oh?= This is the subject of the email, but because it is encoded, I...
Statement
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.