Use PHP to decode POP3 emails (3)

Source: Internet
Author: User
Tags ereg image identifier php source code strtok

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

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

MIME Decoding class

A class that implements MIME Decoding

Decode ($ head = NULL, $ body = NULL, $ content_num =-1) is used to decode this class. for processing convenience, you must enter a two-character array, in our previous article, the pop class used is charged with two arrays, one being the mail header content and the other being the mail body content. I will not describe it in detail due to the limited length. Its implementation idea is similar to the pop class described in the previous article. See the annotations.

This class uses a large number of regular expression operations. If you are not familiar with this operation, please refer to the relevant information about regular expressions.

Class decode_mail

{

VaR $ from_name; var $ to_name; var $ mail_time; var $ from_mail; var $ to_mail;

VaR $ reply_to; var $ cc_to; var $ subject;

// The decoded mail header information:

VaR $ body;

// The decoded body data is an array.

VaR $ body_type; // body type

VaR $ tem_num = 0;

VaR $ get_content_num = 0;

VaR $ body_temp = array ();

VaR $ body_code_type;

VaR $ boundary;

// The above are some global temporary variables used in some methods. Because PHP cannot be well encapsulated, it can only be defined here

VaR $ err_str; // error message

VaR $ DEBUG = 0; // debug tag

VaR $ month_num = array ("Jan" => 1, "FEB" => 2, "Mar" => 3, "APR" => 4, "may" => 5, "Jun" => 6, "Jul" => 7,

"Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov" => 11, "dec" => 12 ); // converts an English month to a month in number.

Function decode ($ head = NULL, $ body = NULL, $ content_num =-1) // calls the main method. $ head and $ body are two arrays, $ content_num indicates that when the body contains multiple parts, only the content of the specified part is retrieved to improve efficiency. The default value is-1, indicating that all content is decoded. If the decoding is successful, this method returns true.

{

If (! $ Head and! $ Body)

{

$ This-> err_str = "No email header or content is specified !! ";

Return false;

}

If (GetType ($ head) = "array ")

{

$ Have_decode = true;

$ This-> decode_head ($ head );

}

If (GetType ($ body) = "array ")

{

$ This-> get_content_num = $ content_num;

$ This-> body_temp = $ body;

$ Have_decode = true;

$ This-> decode_body ();

Unset ($ this-> body_temp );

}

If (! $ Have_decode)

{

$ This-> err_str = "The passed parameter is incorrect. Usage: both new decode_mail (Head, body) parameters are arrays ";

Return false;

}

}

Function decode_head ($ head) // decodes the header content to retrieve meaningful content from the header.

{

$ I = 0;

$ This-> from_name = $ this-> to_name = $ this-> mail_time = $ this-> from_mail = $ this->

To_mail = $ this-> reply_to = $ this-> cc_to = $ this-> subject = "";

$ This-> body_type = $ sThis-> boundary = $ this-> body_code_type = "";

While ($ head [$ I])

{

If (strpos ($ head [$ I], "=? "))

$ Head [$ I] = $ this-> decode_mime ($ head [$ I]); // decodes the encoded content, the decoding function is decode_mime () described above ()

$ Pos = strpos ($ head [$ I], ":");

$ Summ = substr ($ head [$ I], 0, $ POS );

$ Content = substr ($ head [$ I], $ POS + 1); // separate the ID of the mail header information from the content

If ($ this-> Debug) echo $ summ. ": ----:". $ content ."
";

Switch (strtoupper ($ summ ))

{

Case "from": // sender's address and name (there may be no name, only address information)

If ($ left_tag_pos = strpos ($ content, "<"))

{

$ Mail_lenth = strrpos ($ content, ">")-$ left_tag_pos-1;

$ This-> from_name = substr ($ content, 0, $ left_tag_pos );

$ This-> from_mail = substr ($ content, $ left_tag_pos + 1, $ mail_lenth );

If (TRIM ($ this-> from_name) = "") $ this-> from_name = $ this-> from_mail;

Else

If (ereg ("[" | '] ([^' "] +) ['|"] ", $ this-> from_name, $ REG ))

$ This-> from_name = $ Reg [1];

}

Else

{

$ This-> from_name = $ content;

$ This-> from_mail = $ content;

// No sender's email address

}

Break;

Case "to": // recipient's address and name (maybe no name)

If ($ left_tag_pos = strpos ($ content, "<"))

{

$ Mail_lenth = strrpos ($ content, ">")-$ left_tag_pos-1;

$ This-> to_name = substr ($ content, 0, $ left_tag_pos );

$ This-> to_mail = substr ($ content, $ left_tag_pos + 1, $ mail_lenth );

If (TRIM ($ this-> to_name) = "") $ this-> to_name = $ this-> to_mail;

Else

If (ereg ("[" | '] ([^' "] +) ['|"] ", $ this-> to_name, $ REG ))

$ This-> to_name = $ Reg [1];

}

Else

{

$ This-> to_name = $ content;

$ This-> to_mail = $ content;

// No separate recipient email addresses

}

Break;

Case "date": // sending date. For processing convenience, a Unix timestamp is returned. You can use date ("Y-m-d", $ this-> mail_time) to obtain the date in the normal format.

$ Content = trim ($ content );

$ Day = strtok ($ content ,"");

$ Day = substr ($ day, 0, strlen ($ day)-1 );

$ Date = strtok ("");

$ Month = $ this-> month_num [strtok ("")];

$ Year = strtok ("");

$ Time = strtok ("");

$ Time = Split (":", $ time );

$ This-> mail_time = mktime ($ time [0], $ time [1], $ time [2], $ month, $ date, $ year );

Break;

Case "subject": // Email Subject

$ This-> subject = $ content;

Break;

Case "reply_to": // reply address (may not exist)

If (ereg ("<([^>] +)>", $ content, $ REG ))

$ This-> reply_to = $ Reg [1];

Else $ this-> reply_to = $ content;

Break;

Case "Content-Type": // The content type of the entire email, eregi ("([^;] *);", $ content, $ REG );

$ This-> body_type = trim ($ Reg [1]);

If (eregi ("multipart", $ content) // If the multipart type is used, obtain the delimiter

{

While (! Eregi ('boundary = "(. *)" ', $ head [$ I], $ REG) and $ head [$ I])

$ I ++;

$ This-> boundary = $ Reg [1];

}

Else // for general body types, directly obtain the encoding method

{

While (! Eregi ("charset = [" | '] (. *) [' | "]", $ head [$ I], $ REG ))

$ I ++;

$ This-> body_char_set = $ Reg [1];

While (! Eregi ("content-transfer-encoding :(. *)", $ head [$ I], $ REG ))

$ I ++;

$ This-> body_code_type = trim ($ Reg [1]);

}

Break;

Case "cc": // CC ..

If (ereg ("<([^>] +)>", $ content, $ REG ))

$ This-> cc_to = $ Reg [1];

Else

$ This-> cc_to = $ content;

Default:

Break;

} // End Switch

$ I ++;

} // End while

If (TRIM ($ this-> reply_to) = "") // if no reply address is specified, the reply address is the sender address

$ This-> reply_to = $ this-> from_mail;

} // End function define

Function decode_body () // decodes the body, and uses a lot of information obtained from the email header decoding.

{

$ I = 0;

If (! Eregi ("multipart", $ this-> body_type) // if it is not a composite type, you can directly decode it.

{

$ Tem_body = implode ($ this-> body_temp, "RN ");

Switch (strtolower ($ this-> body_code_type) // body_code_type, the encoding method of the body, obtained from the mail header information

{Case "base64 ":

$ Tem_body = base64_decode ($ tem_body );

Break;

Case "quoted-printable ":

$ Tem_body = quoted_printable_decode ($ tem_body );

Break;

}

$ This-> tem_num = 0;

$ This-> body = array ();

$ This-> body [$ this-> tem_num] [content_id] = "";

$ This-> body [$ this-> tem_num] [type] = $ this-> body_type;

Switch (strtolower ($ this-> body_type ))

{

Case "text/html ":

$ This-> body [$ this-> tem_num] [name] = "hypertext body ";

Break;

Case "text/plain ":

$ This-> body [$ this-> tem_num] [name] = "text body ";

Break;

Default:

$ This-> body [$ this-> tem_num] [name] = "unknown body ";

}

$ This-> body [$ this-> tem_num] [size] = strlen ($ tem_body );

$ This-> body [$ this-> tem_num] [content] = $ tem_body;

Unset ($ tem_body );

}

Else // For composite type

{

$ This-> body = array ();

$ This-> tem_num = 0;

$ This-> decode_mult ($ this-> body_type, $ this-> boundary, 0); // call the compound type Decoding Method

}

}

Function decode_mult ($ type, $ boundary, $ begin_row) // This method uses a recursive method to decode the composite mail body. The mail source file is taken from the body_temp array, the types and delimiters of the composite type and the start pointer in the body_temp array are provided during the call.

{

$ I = $ begin_row;

$ Lines = count ($ this-> body_temp );

While ($ I <$ lines) // This is the end ID of a part;

{

While (! Eregi ($ boundary, $ this-> body_temp [$ I]) // locate a start ID

$ I ++;

If (eregi ($ boundary. "--", $ this-> body_temp [$ I])

{

Return $ I;

}

While (! Eregi ("Content-Type :( [^;] *);", $ this-> body_temp [$ I], $ REG) and $ this-> body_temp [$ I])

$ I ++;

$ Sub_type = trim ($ Reg [1]); // The type of the obtained part is Milt or text ....

If (eregi ("multipart", $ sub_type) // This sub-part has multiple parts;

{

While (! Eregi ('boundary = "([^"] *) "', $ this-> body_temp [$ I], $ REG) and $ this-> body_temp [$ I])

$ I ++;

$ Sub_boundary = $ Reg [1]; // delimiter of sub-division;

$ I ++;

$ Last_row = $ this-> decode_mult ($ sub_type, $ sub_boundary, $ I );

$ I = $ last_row;

}

Else

{

$ Comm = "";

While (TRIM ($ this-> body_temp [$ I])! = "")

{

If (strpos ($ this-> body_temp [$ I], "=? "))

$ This-> body_temp [$ I] = $ this-> decode_mime ($ this-> body_temp [$ I]);

If (eregi ("content-transfer-encoding :(. *)", $ this-> body_temp [$ I], $ REG ))

$ Code_type = strtolower (TRIM ($ Reg [1]); // encoding method

$ Comm. = $ this-> body_temp [$ I]. "RN ";

$ I ++;

} // Comm is the encoding description

If (eregi ('name = ["] ([^"] *) ["] ', $ comm, $ REG ))

$ Name = $ Reg [1];

If (eregi ("content-Disposition :(. *);", $ comm, $ REG ))

$ Disp = $ Reg [1];

If (eregi ("charset = [" | '] (. *) [' | "]", $ comm, $ REG ))

$ Char_set = $ Reg [1];

If (eregi ("content-ID: [] * <(. *)>", $ comm, $ REG) // The image identifier.

$ Content_id = $ Reg [1];

$ This-> body [$ this-> tem_num] [type] = $ sub_type;

$ This-> body [$ this-> tem_num] [content_id] = $ content_id;

$ This-> body [$ this-> tem_num] [char_set] = $ char_set;

If ($ name)

$ This-> body [$ this-> tem_num] [name] = $ name;

Else

Switch (strtolower ($ sub_type ))

{

Case "text/html ":

$ This-> body [$ this-> tem_num] [name] = "hypertext body ";

Break;

Case "text/plain ":

$ This-> body [$ this-> tem_num] [name] = "text body ";

Break;

Default:

$ This-> body [$ this-> tem_num] [name] = "unknown body ";

}

// The next row begins to retrieve the body

If ($ this-> get_content_num =-1 or $ this-> get_content_num = $ this-> tem_num) // determine whether this part is required. -1 indicates all

{

$ Content = "";

While (! Ereg ($ boundary, $ this-> body_temp [$ I])

{

// $ Content [] = $ this-> body_temp [$ I];

$ Content. = $ this-> body_temp [$ I]. "RN ";

$ I ++;

}

// $ Content = implode ("RN", $ content );

Switch ($ code_type)

{

Case "base64 ":

$ Content = base64_decode ($ content );

Break;

Case "quoted-printable ":

$ Content = str_replace ("N", "RN", quoted_printable_decode ($ content ));

Break;

}

$ This-> body [$ this-> tem_num] [size] = strlen ($ content );

$ This-> body [$ this-> tem_num] [content] = $ content;

}

Else

{

While (! Ereg ($ boundary, $ this-> body_temp [$ I])

$ I ++;

}

$ This-> tem_num ++;

}

// End else

} // End while;

} // End function define

Function decode_mime ($ string ){

// Decode_mime is provided in the preceding section. It is skipped here.

}

} // End class define

Here, we must note that the decoding of the image used in the HTML text is particularly important. When you send an HTML text, you will encounter the problem of how to transfer the image. An image is a tag in an HTML document. The key is the source file. Many mail processing methods use an absolute url id, that is, using tags such as in the HTML body of the mail. In this way, when reading the mail, the email reader (usually using an embedded browser) Automatically downloads images from the Internet. However, if the connection to the Internet is disconnected after the email is received, the images cannot be displayed normally.

Therefore, a better way is to put the image in an email and send it out. In mime encoding, besides the multipart/related MIME header information mentioned above, a content-ID is used to describe the relationship between the image and the body: to establish a relationship between the image and the html body. When an image in an HTML document is encoded, an attribute such as content-ID: 122223443556dsdf @ ntsever is added to its MIME header. 122223443556dsdf @ ntsever is a unique identifier. In the HTML document, when decoding a tag, you also need to modify the tags in the HTML body to point to the specific path of the decoded image. But considering the specific decodingProgramThe image is processed differently, so the labels in the hmtl body are not modified in the decoded class. Therefore, when using this class, some processing is required for HTML text with images. Images in the body can be saved using temporary files or databases.

Now we have introduced how POP3 collects mails and performs MIME Decoding. The following is a small program using these two classes:


Include ("pop3.inc. php ");

Include ("mime. Inc. php ");

$ Host = "pop.china.com ";

$ User = "boss_ch ";

$ Pass = "mypassword ";

$ Rec = new POP3 ($ host, 110,2 );

$ Decoder = new decode_mail ();

If (! $ Rec-> open () Die ($ rec-> err_str );

If (! $ Rec-> login ($ user, $ pass) Die ($ rec-> err_str );

If (! $ Rec-> Stat () Die ($ rec-> err_str );

Echo "A total of". $ rec-> messages. "mails, a total of". $ rec-> size. "bytes
";

If ($ rec-> messages> 0)

{

If (! $ Rec-> listmail () Die ($ rec-> err_str );

Echo "The following is the mail content:
";

For ($ I = 1; $ I <= count ($ rec-> mail_list); $ I ++)

{

Echo "letter". $ rec-> mail_list [$ I] [num]. ", size:". $ rec-> mail_list [$ I] [size]."
";

$ Rec-> getmail ($ rec-> mail_list [$ I] [num]);

$ Decoder-> decode ($ rec-> head, $ rec-> body );

Echo"
Body of the email header:

";

Echo $ decoder-> from_name. "(". $ decoder-> from_mail. ") on ". date ("Y-m-d h: I: s", $ decoder-> mail_time ). "Sent ". $ decoder-> to_name. "(". $ decoder-> to_mail. ")";

Echo "n
Cc :";

If ($ decoder-> cc_to) echo $ decoder-> cc_to; else echo "NONE ";

Echo "n
Topic: ". $ decoder-> subject;

Echo "n
Reply to: ". $ decoder-> reply_to;

Echo"
Body of the email:

";

Echo "body type:". $ decoder-> body_type;

Echo"
Body content :";

For ($ J = 0; $ jbody); $ J ++)

{

Echo "n
Type: ". $ decoder-> body [$ J] [type];

Echo "n
Name: ". $ decoder-> body [$ J] [name];

Echo "n
Size: ". $ decoder-> body [$ J] [size];

Echo "n
Content_id: ". $ decoder-> body [$ J] [content_id];

Echo "n
Body Character Set ". $ decoder-> body [$ J] [char_set];

Echo"
"; Echo" body content: ". $ decoder-> body [$ J] [content]; echo"
";

}

$ Rec-> DELE ($ I );

}

}

$ Rec-> close ();

?>

If you want to completeSource codePlease contact me: boss_ch@netease.com

Author: Chen junqing
Reprinted: chinacnet

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

Love J2EE follow Java Michael Jackson video station JSON online tools

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

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.