PHP leverages Mail

Source: Internet
Author: User
Tags config error code mail account

The

  focuses on the One_mail function. Use the Mail_mimedecode class to extract headers and message bodies from messages, as follows

The code is as follows: <?php  header ("content-type:text/html; Charset=utf-8 "); /*  * Record kid words and insert in database  * User by sending email to publication K ID 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 Mark deletion error   define (' Delelet_error ', 6); Delete Mail 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 whether the connection was successful   if ($pop 3->connect ($host)) { //Determine whether the login was successful   if ($pop 3->login ($user, $password) = = True) {  $list = $pop 3->_cmdlist ();  $sum = $pop 3->_cmdstat ();   //How many messages per fetch   $step = 2;  $r = 0; // $step Mail   for ($n = 0; $n < $sum [0]; $n + + $step) {  $t = 0;  $users = Array ();  $mail = array ();  $offset = ($sum [0]-$n) < $step? $sum [0]-$n: $step; //Mail cycle   for ($i = $r; $i < $n + $offset; $i + +) {   //Get mail message   $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 (); &N Bsp  //Current mail array loop, current mail user email, child name, children's message   for ($m = 0; $m < count ($users); $m + +) {  $num = 0;  $mn = $m + $n; //Data processing   $post _text = $users [$m] [' Conntent '];  if (!empty ($post _text)) {  $pos T_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 { //Database Data   for ($x = 0; $x < count ($str); $x + +) { //Determine if From_mail is a 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; } } }   //Determine whether the data in the database is successfully matched to   if ($num = = 0) {  $val = $th Is->kid_data ($users, $m, 1, $post _text, $post _link_num, $post _text_undo);  if (Isset ($val)) {  $value ["$t" = = $val;  $num = 1;  $t = $t +1; } } } //For a segment of data operations   $valu =}  ("), (", $ 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 is wrong! ";  $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&nbs P */  function One_mail ($i, $pop 3, $list, $mail _log) {  $stg = $pop 3->getparsedheaders ($list [$i] [' msg_id ']);   $from = Imap_mime_header_decode ($stg [' from ']); Mail Sender   $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 = $m ail_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") {//Judgment 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) {  & nbsp $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 ($l ist[$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 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", & nbsp "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  * @ac Cess 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 ();  & nbsp while ($arr = Mysql_fetch_array ($query)) {  Array_push ($str 1, $arr); }  return $str 1; } /*&NB Sp *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 ($m Ailtype, $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", "$mailtyp E//ignore ", $mailbody); }    $mail _type =" HTML ";  $smtp = new SMTP ($smtpserver, $smtpserverport, Tru E, $smtpuser, $smtppass);  $smtp->debug = false;  $send _mail = $smtp->sendmail ($smtpemailto, $ Smtpusermail, $mailsubject, $mailbody, $mail _type, "" "" ");    if ($send _mail = = False) {  return" Send FAI Le ";  $send _mail = $smtp->sendmail ($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mail _type," "," "); nbsp } } /*  *the Kid ' s age then publication Kid words  * @access public  * @param int $kid _birthday Kid birthday  * @return Array Kid Year month day  */    Functio N 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) {&N Bsp $years = Floor ($age/10000); }    $age = $age% 10000;  $months = Floor ($age/100);  if ($mont HS > A)   $months-= 88;  $days = $age% 100;  if ($days > $cur _date%) {  $days = $days-( 100-date ("D", Strtotime date ("Ym"). "3600"); }  return Array (  $years,  $months,  $days  ); }  ; *judge the $m message and the $x data  * @access public  * @param array $users mail information  * @param arra Y $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 FR Om_mail children     $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] <) {  $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 p ublic  * @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 _ma Il = $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 ()); nbsp $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 mation  * @access public  * @param array $users the array () of users  * @param int $m The M items of Array&nbs P * @param int $kid _num the kid_num kid  * @param string $post _text the message of mail  * @param int $post _link_num Count (link) 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 + +) {&nbsp ; if ($kid _birthdy[$j] >= 0 && $kid _birthdy[$j] <) {  $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 * @access words  * @parAm array $value the array () of users  * @return void  */  function Kid_message_count ($value) {  $use _na Mes = 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 (c Ount ($use _names) = = 1) {  $d _users[0] = $use _names[0]; } else {   //First   if ($use _names[0]!= $ USE_NAMES[1]) {  $d _users[0] = $use _names[0]; } else {  $s _users[$s] = $use _names[0];  $s = $s +1;&nb Sp } //Last   if ($use _names[$sum _kid-1]!= $use _names[$sum _kid-2]) {  $d _users[$sum _kid-1] = $use _names[$s um_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 ()); &nbs P }    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_quer Y ($sql) or Die (Mysql_error ()); }   }   } /**  * Point error  * @access private& nbsp * @param int Error_num the error code  * @return void  *  */  Private Function _error ($error _num) {&nbsp ; $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); } } ?> 
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.