The emphasis is one_mail function. Use the Mail_mimedecode class to extract headers and message bodies from messages.
Copy Code code as follows:
<?php
Header ("content-type:text/html; Charset=utf-8 ");
/*
* Record kid words and insert INTO database
* User by sending e-mail 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); Mailbox Connection Failure Error
Define (' Account_error ', 4); e-mail Account error
Define (' Sign_email_error ', 5); Message Flag Deletion Error
Define (' Delelet_error ', 6); Delete message Error
Define (' Insert_error ', 7); Insert Data 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");
$pop 3 = new NET_POP3 ();
Determine if the connection was successful
if ($pop 3->connect ($host, 110)) {
To determine whether the login was successful
if ($pop 3->login ($user, $password) = = True) {
$list = $pop 3->_cmdlist ();
$sum = $pop 3->_cmdstat ();
How many emails per fetch
$step = 2;
$r = 0;
Take $step mail to 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;
Message Looping
for ($i = $r; $i < $n + $offset; $i + +) {
Get mail information
$user = $this->one_mail ($i, $pop 3, $list, $mail _log);
Array_push ($mail, $user [' from_mail ']);
Array_push ($users, $user);
$r = $i +1;
}
Get Mail user Data
$str = $this->uesr_data ($mail);
$value = Array ();
Current mail array loop, current mail user's email, child's name, children's word
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 + +) {
To determine if it was From_mail's child.
$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;
}
}
}
Determines whether the data in the database is successfully matched to
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;
}
}
}
}
Manipulating a piece of data
$valu = Implode ("), (", $value);
if ($valu!= "") {
$err _time = $this->insert_date ($valu);
$this->kid_message_count ($value);
}
}
Fclose ($mail _log);
/*
if ($pop 3->disconnect () ==false) {
$this->_error (Delelet_error);
}
*/
} else {
echo "Account or password Error! ";
$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 $pop 3 POP3 protocol Object
* @return array mail from, header,content
*/
function One_mail ($i, $pop 3, $list, $mail _log) {
$STG = $pop 3->getparsedheaders ($list [$i] [' msg_id ']);
$from = Imap_mime_header_decode ($stg [' from ']); Sender of Message
$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 = $pop 3->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") {//Judge 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[' subject '];
$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;
}
$pop 3->_cmddele ($list [$i] [' msg_id ']);
$pop 3->deletemsg ($list [$i] [' msg_id ']);
if ($pop 3->deletemsg ($list [$i] [' msg_id ']) = = False) {
$this->_error (Sign_email_error);
}
Get the user email that needs to be inserted, child name, children's word
$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 ());
$str 1 = array ();
while ($arr = Mysql_fetch_array ($query)) {
Array_push ($str 1, $arr);
}
return $STR 1;
}
/*
*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 yet". $user _kid_nickname. "Baby";
$mailsubject = "=? UTF-8? B? ". Base64_encode ($mailsubject). "?=";
$mailbody = "Please add 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 ']) {//Judge whether it is a child of 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 ());
$str 1 = array ();
$kids = Array ();
$i = 0;
while ($arr = Mysql_fetch_array ($query)) {
$str 1[$i] = $arr;
$i = $i +1;
}
$kid _num = $kid _num-1;
if ($kid _num > (count ($str 1)-1)) {
return $num = 0;
} else {
return $str 1["$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)
* @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 {
First one
if ($use _names[0]!= $use _names[1]) {
$d _users[0] = $use _names[0];
} else {
$s _users[$s] = $use _names[0];
$s = $s +1;
}
Last one
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. "/tcould 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);
}
}
?>