萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> PHP 利用Mail

PHP 利用Mail

 重點為one_mail函數。利用Mail_mimeDecode類從郵件中提取郵件頭和郵件正文,具體實現如下

代碼如下: <?php  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); //數據庫連接失敗錯誤  define('EMAIL_CONNECTION_ERROR', 3); //郵箱連接失敗錯誤  define('ACCOUNT_ERROR', 4); //郵箱的賬號錯誤  define('SIGN_EMAIL_ERROR', 5); //郵件標記刪除錯誤  define('DELELET_ERROR', 6); //刪除郵件錯誤  define('INSERT_ERROR', 7); //插入數據失敗錯誤    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();  //判斷連接是否成功  if ($pop3->connect($host, 110)) {  //判斷登入是否成功  if ($pop3->login($user, $password) === true) {  $list = $pop3->_cmdList();  $sum = $pop3->_cmdStat();    //每次取多少郵件  $step = 2;  $r = 0;  //先對郵箱取$step郵件  for ($n = 0; $n < $sum[0]; $n += $step) {  $t = 0;  $users = array ();  $mail = array ();  $offset = ($sum[0] - $n) < $step ? $sum[0] - $n : $step;  //郵件循環  for ($i = $r; $i < $n + $offset; $i++) {    //取得郵件信息  $user = $this->one_mail($i, $pop3, $list, $mail_log);  array_push($mail, $user['from_mail']);  array_push($users, $user);  $r = $i +1;  }  //取得mail用戶數據  $str = $this->uesr_data($mail);  $value = array ();    //當前郵件數組循環,當前郵件用戶的email,小孩名,童言  for ($m = 0; $m < count($users); $m++) {  $num = 0;  $mn = $m + $n;  //數據處理  $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 {  //數據庫中的數據  for ($x = 0; $x < count($str); $x++) {  //判斷是否是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;  }  }  }    //判斷是否成功與數據庫中數據匹配到  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;  }  }  }  }  //對一段數據操作  $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 "帳號或密碼錯誤!";  $this->_error(ACCOUNT_ERROR);  }  } else {  echo "連接失敗...";  $this->_error(EMAIL_CONNECTION_ERROR);    }  } else {  echo "數據庫連接失敗...";  $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']); //郵件的發送者  $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']; //編碼格式  $mail_type = $mail_part->ctype_parameters;  $mail_type = $mail_type['charset'];  $mail_body = $mail_part->body; //正文內容    if ($mail_code == "base64") { //判斷編碼格式  $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;  }  //$pop3->_cmdDele($list[$i]['msg_id']);  $pop3->deleteMsg($list[$i]['msg_id']);  if ($pop3->deleteMsg($list[$i]['msg_id']) == false) {  $this->_error(SIGN_EMAIL_ERROR);  }  //取得需要插入的用戶email,小孩名,童言  $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_av
copyright © 萬盛學電腦網 all rights reserved