Example of extracting mail information using Mail_MimeDecode

Source: Internet
Author: User
Tags email account
Focus on the one_mail function. Use the Mail_mimeDecode class to extract the mail header and body from the mail. the specific implementation of this class focuses on the one_mail function. Use the Mail_mimeDecode class to extract the mail header and body from the mail.
The code is as follows:
Header ("content-type: text/html; charset = UTF-8 ");
/*
* Record kid words and insert into database
* User by sending email to publication kid words
*
*/
Include 'pop3. php ';
Include 'email _ class. php ';
Include 'mail _ mimeDecode. php ';
// Include 'include/compatible. php ';
Include 'include/extend_common.php ';
// Include '../htdocs/include/extend_common.php ';
Define ('post _ FROM_EMAIL ', 1 );
Define ('database _ CONNECTION_ERROR ', 2); // DATABASE connection failure error
Define ('email _ CONNECTION_ERROR ', 3); // EMAIL connection failure error
Define ('account _ error', 4); // the email ACCOUNT is incorrect.
Define ('sign _ EMAIL_ERROR ', 5); // mail Mark deletion error
Define ('delelet _ error', 6); // mail deletion ERROR
Define ('Insert _ error', 7); // data insertion failure ERROR

Class mail_data {

Function mail_data (){
Include 'config. php ';
$ Mail_log = fopen ("mail_log.txt", "a + ");
$ Conn = @ mysql_connect ($ db_host, $ db_user, $ db_password );
If ($ conn ){
Mysql_select_db ('t', $ conn );
Mysql_query ("set names utf8 ");
$ Pop3 = new Net_POP3 ();
// Determine whether the connection is successful
If ($ pop3-> connect ($ host, 110 )){
// Determine whether the logon is successful
If ($ pop3-> login ($ user, $ password) === true ){
$ List = $ pop3-> _ cmdList ();
$ Sum = $ pop3-> _ 1_stat ();

// How many emails are Retrieved each time
$ Step = 2;
$ R = 0;
// Get the $ step email from the mailbox first
For ($ n = 0; $ n <$ sum [0]; $ n + = $ step ){
$ T = 0;
$ Users = array ();
$ Mail = array ();
$ Offset = ($ sum [0]-$ n) <$ step? $ Sum [0]-$ n: $ step;
// Email loop
For ($ I = $ r; $ I <$ n + $ offset; $ I ++ ){

// Obtain the email information
$ User = $ this-> one_mail ($ I, $ pop3, $ list, $ mail_log );
Array_push ($ mail, $ user ['from _ mail']);
Array_push ($ users, $ user );
$ R = $ I + 1;
}
// Obtain mail user data
$ Str = $ this-> uesr_data ($ mail );
$ Value = array ();

// The current mail array loop, email of the current mail user, child name, and Tongyan
For ($ m = 0; $ m <count ($ users); $ m ++ ){
$ Num = 0;
$ Mn = $ m + $ n;
// Data processing
$ Post_text = $ users [$ m] ['conntent'];
If (! Empty ($ post_text )){
$ Post_text = do_submit_text ($ post_text );
$ Post_text = mysql_real_escape_string ($ post_text );
$ Post_link_num = parsed_text_include_links ($ post_text );
$ Text = do_submit_text ($ post_text );
$ Post_text_undo = mysql_real_escape_string ($ text );
$ Post_text = mysql_real_escape_string ($ post_text );
$ Val = array ();
$ Kid_num = $ users [$ m] ['Kid _ nickname'];
If (is_numeric ($ kid_num) & intval ($ kid_num) <= 3 & intval ($ kid_num)> 0 ){
$ Kid_num = intval ($ kid_num );
$ Val = $ this-> kid_data ($ users, $ m, $ kid_num, $ post_text, $ post_link_num, $ post_text_undo );
If (isset ($ val )){
$ Value ["$ t"] = $ val;
$ Num = 1;
$ T = $ t + 1;
}
} Else {
// Data in the database
For ($ x = 0; $ x <count ($ str); $ x ++ ){
// Determine if it is a child of from_mail
$ Val ["$ x"] = $ this-> is_kid ($ users, $ str, $ m, $ x, $ post_text, $ post_link_num, $ post_text_undo );
If (isset ($ val ["$ x"]) {
$ Value ["$ t"] = $ val ["$ x"];
$ Num = 1;
$ T = $ t + 1;
}
}
}

// Determine whether the data in the database is matched successfully
If ($ num = 0 ){
$ Val = $ this-> kid_data ($ users, $ m, 1, $ post_text, $ post_link_num, $ post_text_undo );
If (isset ($ val )){
$ Value ["$ t"] = $ val;
$ Num = 1;
$ T = $ t + 1;
}
}
}
}
// Perform operations on a piece of data
$ Valu = implode ("), (", $ value );
If ($ valu! = ""){
$ Err_time = $ this-> insert_date ($ valu );
$ This-> kid_message_count ($ value );
}
}
Fclose ($ mail_log );
/*
If ($ pop3-> disconnect () = false ){
$ This-> _ error (DELELET_ERROR );
}
*/
} Else {
Echo "incorrect account or password! ";
$ This-> _ error (ACCOUNT_ERROR );
}
} Else {
Echo "connection failed ...";
$ This-> _ error (EMAIL_CONNECTION_ERROR );

}
} Else {
Echo "database connection failed ...";
$ This-> _ error (DATABASE_CONNECTION_ERROR );
}
}

/*
* Read the $ I email message
* @ Access public
* @ Param int $ I mail id
* @ Param object $ pop3 pop3 protocol object
* @ Return array mail from, header, content
*/
Function one_mail ($ I, $ pop3, $ list, $ mail_log ){
$ Stg = $ pop3-> getParsedHeaders ($ list [$ I] ['MSG _ id']);
$ From = imap_mime_header_decode ($ stg ['from']); // The sender of the email
$ String_from = '';
For ($ j = 0; $ j <count ($ from); $ j ++ ){
$ String_from = "$ string_from". $ from [$ j]-> text;
}
Preg_match ("/([a-z0-9A-Z _] +) @ ([a-z0-9A-Z/.] +). ([a-z0-9A-Z] +)/", $ string_from, $ from_mail );

$ String = $ pop3-> getMsg ($ list [$ I] ['MSG _ id']);
$ Body = new Mail_mimeDecode ($ string );

$ Sr = $ body-> decode (array (
'Include _ bodies' => true,
'Decode_bodies' => false,
'Decode_headers' => true
));

If (property_exists ($ sr, 'parts ')){
$ Mail_part = $ sr-> parts;
$ Mail_part = $ mail_part [0];
} Else {
$ Mail_part = $ sr;
}

$ Mail_code = $ mail_part-> headers;
$ Mail_code = $ mail_code ['content-transfer-encoding ']; // encoding format
$ Mail_type = $ mail_part-> ctype_parameters;
$ Mail_type = $ mail_type ['charset'];
$ Mail_body = $ mail_part-> body; // body content

If ($ mail_code = "base64") {// Determine the encoding format
$ Text = base64_decode ("$ mail_body ");
$ Text = iconv ("$ mail_type", "UTF-8", $ text );
} Else {
$ Text = quoted_printable_decode ("$ mail_body ");
$ Text = iconv ("$ mail_type", "UTF-8", $ text );
}

$ Mail_title = $ sr-> headers;
$ Mail_title = $ mail_title ['subobject'];
$ Mail_title = imap_mime_header_decode ($ mail_title );

If (count ($ mail_title )! = 0 ){

$ Title = $ mail_title [0]-> text;
$ T = $ mail_title [0]-> charset;

If ($ t! = "Default "){
$ Title = iconv ($ t, "UTF-8", $ title );
} Else {
$ Title = iconv ("gb2312", "UTF-8", $ title );
}
} Else {
$ Title = 1;
}
// $ Pop3-> _ includele ($ list [$ I] ['MSG _ id']);
$ Pop3-> deleteMsg ($ list [$ I] ['MSG _ id']);
If ($ pop3-> deleteMsg ($ list [$ I] ['MSG _ id']) = false ){
$ This-> _ error (SIGN_EMAIL_ERROR );
}
// Obtain the user email to be inserted, the child name, and the boy's words.
$ Users ["$ I"] = array (
"From_mail" => "$ from_mail [0]",
"Kid_nickname" => "$ title ",
"Conntent" => "$ text ",
"Body_type" => "$ mail_type"
);
$ Log = $ users ["$ I"];
Array_unshift ($ log, date ("Y-m-d H: I; s "));
$ Log = serialize ($ log );
Fwrite ($ mail_log, $ log. "/r/n ");
Return $ users ["$ I"];
}

/**
* At database search $ mail user's information
* @ Access public
* @ Param string $ mail all email
* @ Return array mail user's information
*/
Function uesr_data ($ mail ){
$ Mails = implode ("','", $ mail );
$ SQL = "SELECT a. mail, a. user_name, a. user_nickname, B. kid_id, B. kid_name, B. kid_avatar, B. kid_birthday
FROM't _ users 'a,'t _ users_kid 'B
WHERE a. mail in ('$ mails') AND a. user_id = B. user_id ";
$ Query = mysql_query ($ SQL) or die (mysql_error ());
$ Str1 = array ();

While ($ arr = mysql_fetch_array ($ query )){
Array_push ($ str1, $ arr );
}
Return $ str1;
}
/*
* Insert $ value into database
* @ Access public
* @ Param string $ value kid information
* @ Return void
*/
Function insert_date ($ value ){
$ SQL _insert = "INSERT INTO't _ posts '(kid_id, user_name, user_nickname, post_time, post_text, user_avatar, post_link_num, post_text_undo, post_from, add_time)
VALUES ($ value )";
$ Num = mysql_query ($ SQL _insert) or die (mysql_error ());

If ($ num! = 1 ){
$ This-> _ error (INSERT_ERROR );
}
}
/*
* Send email to $ smtpemailto
* @ Access public
* @ Param string $ mailtype mail_from type
* @ Param string $ smtpemailto mail_from
* @ Param string $ user_kid_name mail title
* @ Return void
*/
Function reply_email ($ mailtype, $ smtpemailto, $ user_kid_nickname ){
Require "config. php ";
$ Mailsubject = "You have not". $ user_kid_nickname. "";
$ Mailsubject = "=? UTF-8? B? ". Base64_encode ($ mailsubject )."? = ";
$ Mailbody = "Add a baby first ";

If ($ mailtype! = "ISO-8859-1 "){
$ Mailbody = iconv ("UTF-8", "$ mailtype // ignore", $ mailbody );
}

$ Mail_type = "HTML ";
$ Smtp = new smtp ($ smtpserver, $ smtpserverport, true, $ smtpuser, $ smtppass );
$ Smtp-> debug = FALSE;
$ Send_mail = $ smtp-> sendmail ($ smtpemailto, $ smtpusermail, $ mailsubject, $ mailbody, $ mail_type ,"","");

If ($ send_mail = false ){
Return "send faile ";
$ Send_mail = $ smtp-> sendmail ($ smtpemailto, $ smtpusermail, $ mailsubject, $ mailbody, $ mail_type ,"","");
}
}
/*
* The kid's age then publication kid words
* @ Access public
* @ Param int $ kid_birthday kid birthday
* @ Return array kid year month day
*/

Function get_kid_age_info ($ kid_birthday ){
$ Cur_date = date ("Ymd ");
$ Age = $ cur_date-$ kid_birthday;
If ($ age <0 ){
Return false;
}
$ Years = 0;
$ Months = 0;
$ Days = 0;
'If ($ age> 10000 ){
$ Years = floor ($ age/10000 );
}

$ Age = $ age % 10000;
$ Months = floor ($ age/100 );
If ($ months> 12)
$ Months-= 88;
$ Days = $ age % 100;
If ($ days> $ cur_date %100 ){
$ Days = $ days-(100-date ("d", strtotime (date ("Ym"). "01")-24*3600 ));
}
Return array (
$ Years,
$ Months,
$ Days
);
}
/**
* Judge the $ m message and the $ x data
* @ Access public
* @ Param array $ users mail information
* @ Param array $ str user information
* @ Param int $ m $ users grade
* @ Param int $ x $ str grade
* @ Param string $ post_text the mail text
* @ Return string information
*/
Function is_kid ($ users, $ str, $ m, $ x, $ post_text, $ post_link_num, $ post_text_undo ){

If ($ users [$ m] ['from _ mail'] = $ str [$ x] ['mail']) {// determines whether the child is from_mail

$ Kid_id = $ str [$ x] ['Kid _ id'];
$ User_name = $ str [$ x] ['User _ name'];
$ User_nickname = $ str [$ x] ['User _ nickname'];
$ Kid_diff = $ str [$ x] ['Kid _ birthday'];
$ Kid_name = $ str [$ x] ['Kid _ name'];
$ Kid_diff = date ("Ymd", $ kid_diff );
$ Kid_birthdy = $ this-> get_kid_age_info ($ kid_diff );
// Format conversion
For ($ j = 0; $ j <count ($ kid_birthdy); $ j ++ ){
If ($ kid_birthdy [$ j]> = 0 & $ kid_birthdy [$ j] <10 ){
$ Kid_birthdy [$ j] = "0". "$ kid_birthdy [$ j]";
}
}
$ Post_time = $ kid_birthdy [0]. $ kid_birthdy [1]. $ kid_birthdy [2];
$ User_avatar = get_kid_avatar ($ user_name, $ kid_id );
$ Kid_avatar = $ user_avatar;
If ($ users [$ m] ['Kid _ nickname'] = $ kid_name ){
$ Kid_id = mysql_real_escape_string ("$ kid_id ");
$ User_name = mysql_real_escape_string ("$ user_name ");
$ Post_time = mysql_real_escape_string ("$ post_time ");
$ Kid_avatar = mysql_real_escape_string ("$ kid_avatar ");
$ From = POST_FROM_EMAIL;
$ Add_time = time ();
$ Values = "'$ kid_id', '$ user_name', '$ user_nickname', '$ post_time', '$ post_text', '$ kid_avatar', '$ post_link_num ', '$ post_text_undo', '$ from', '$ add_time '";
Return $ values;
}
}
}
/**
* Have the kid_num kid of users information
* @ Access public
* @ Param array $ users the array () of users
* @ Param int $ m the m items of array
* @ Param int $ kid_num the kid_num kid
* @ Return array kid information
*/
Function user_kid ($ users, $ m, $ kid_num ){
$ M_mail = $ users ["$ m"] ['from _ mail'];
$ SQL = "SELECT a. user_name, a. user_nickname, B. kid_id, B. kid_name, B. kid_avatar, B. kid_birthday
FROM't _ users 'a,'t _ users_kid 'B
WHERE a. mail = '$ m_mail' AND a. user_id = B. user_id order by B. kid_birthday ASC ";
$ Query = mysql_query ($ SQL) or die (mysql_error ());
$ Str1 = array ();
$ Kids = array ();
$ I = 0;
While ($ arr = mysql_fetch_array ($ query )){
$ Str1 [$ I] = $ arr;
$ I = $ I + 1;
}
$ Kid_num = $ kid_num-1;
If ($ kid_num> (count ($ str1)-1 )){
Return $ num = 0;
} Else {
Return $ str1 ["$ kid_num"];
}

}
/**
* Get the kid_num kid information
* @ Access public
* @ Param array $ users the array () of users
* @ Param int $ m the m items of array
* @ Param int $ kid_num the kid_num kid
* @ Param string $ post_text the message of mail
* @ Param int $ post_link_num count (link) of message body
* @ Return array $ values the kid information
*/
Function kid_data ($ users, $ m, $ kid_num, $ post_text, $ post_link_num, $ post_text_undo ){
$ Use_kid = $ this-> user_kid ($ users, $ m, $ kid_num );

If ($ use_kid! = 0 ){
$ Kid_id = $ use_kid ['Kid _ id'];
$ User_name = $ use_kid ['User _ name'];
$ User_nickname = $ use_kid ['User _ nickname'];
$ Kid_diff = $ use_kid ['Kid _ birthday'];
$ Kid_diff = date ("Ymd", $ kid_diff );
$ Kid_birthdy = $ this-> get_kid_age_info ($ kid_diff );
// Format conversion
For ($ j = 0; $ j <count ($ kid_birthdy); $ j ++ ){
If ($ kid_birthdy [$ j]> = 0 & $ kid_birthdy [$ j] <10 ){
$ Kid_birthdy [$ j] = "0". "$ kid_birthdy [$ j]";
}
}
$ Post_time = $ kid_birthdy [0]. $ kid_birthdy [1]. $ kid_birthdy [2];

$ User_avatar = get_kid_avatar ($ user_name, $ kid_id );
$ Kid_avatar = $ user_avatar;
$ Add_time = time ();
$ From = POST_FROM_EMAIL;
$ Values = "'$ kid_id', '$ user_name', '$ user_nickname', '$ post_time', '$ post_text', '$ kid_avatar', '$ post_link_num ', '$ post_text_undo', '$ from', '$ add_time '";
Return $ values;
}
}
/**
* Update data when have kid words
* @ Access public
* @ Param array $ value the array () of users
* @ Return void
*/
Function kid_message_count ($ value ){
$ Use_names = array ();

For ($ k = 0; $ k <count ($ value); $ k ++ ){
$ K_name = explode (",", $ value [$ k]);
$ Use_names [$ k] = $ k_name [1];
}
Asort ($ use_names );
$ Sum_kid = count ($ use_names );
$ S = 0;
If (count ($ use_names) = 1 ){
$ D_users [0] = $ use_names [0];
} Else {

// The first
If ($ use_names [0]! = $ Use_names [1]) {
$ D_users [0] = $ use_names [0];
} Else {
$ S_users [$ s] = $ use_names [0];
$ S = $ s + 1;
}
// Last
If ($ use_names [$ sum_kid-1]! = $ Use_names [$ sum_kid-2]) {
$ D_users [$ sum_kid-1] = $ use_names [$ sum_kid-1];
} Else {
$ S_users [$ s] = $ use_names [$ sum_kid-1];
$ S = $ s + 1;
}

For ($ k = 1; $ k <count ($ use_names)-1; $ k ++ ){

If ($ use_names [$ k] = $ use_names [$ k-1] | $ use_names [$ k] = $ use_names [$ k + 1]) {
$ S_users [$ s] = $ use_names [$ k];
$ S = $ s + 1;
} Else {
$ D_users [$ k] = $ use_names [$ k];
}

}
}

If (isset ($ d_users )){
$ Names = implode (",", $ d_users );
$ SQL = "UPDATE't _ users 'set post_num = post_num + 1 WHERE 't_users'. user_name in ($ names )";
$ Query = mysql_query ($ SQL) or die (mysql_error ());
}

If (isset ($ s_users )){

For ($ s = 0; $ s <count ($ s_users); $ s ++ ){
$ Name = $ s_users [$ s];
$ SQL = "UPDATE 't_ users' set post_num = post_num + 1 WHERE 't_users'. user_name = $ name ";
$ Query = mysql_query ($ SQL) or die (mysql_error ());
}

}

}
/**
* Point error
* @ Access private
* @ Param int error_num the error code
* @ Return void
*
*/
Private function _ error ($ error_num ){
$ Error_log = fopen ("error_log.txt", "a + ");
Switch ($ error_num ){
Case 2:
Fwrite ($ error_log, date ("Y-m-d H: I: s"). "/t". $ error_num. "/tcocould not connect database! /R/n ");
Break;
Case 3:
Fwrite ($ error_log, date ("Y-m-d H: I: s"). "/t". $ error_num. "/tConnection Failure! /R/n ");
Break;
Case 4:
Fwrite ($ error_log, date ("Y-m-d H: I: s"). "/t". $ error_num. "/tAccount number or password error !! /R/n ");
Break;
Case 5:
Fwrite ($ error_log, date ("Y-m-d H: I: s"). "/t". $ error_num. "/tsign email failed! /R/n ");
Break;
Case 6:
Fwrite ($ error_log, date ("Y-m-d H: I: s"). "/t". $ error_num. "/tdelete emails failed! /R/n ");
Break;
Case 7:
Fwrite ($ error_log, date ("Y-m-d H: I: s"). "/t". $ error_num. "/tinsert data failed! /R/n ");
Break;
}
Fclose ($ error_log );
}
}
?>
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.