PHP 利用Mail_MimeDecode类提取邮件信息示例


Posted in PHP onJanuary 26, 2014

重点为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_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 = "您暂时还没有" . $user_kid_nickname . "宝宝"; 
$mailsubject = "=?UTF-8?B?" . base64_encode($mailsubject) . "?="; 
$mailbody = "请先添加宝宝"; 
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']) { //判断是否是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); 
//格式转换 
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); 
//格式转换 
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 { 
//第一个 
if ($use_names[0] != $use_names[1]) { 
$d_users[0] = $use_names[0]; 
} else { 
$s_users[$s] = $use_names[0]; 
$s = $s +1; 
} 
//最后一个 
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); 
} 
} 
?>
PHP 相关文章推荐
phpmyadmin的#1251问题
Nov 25 PHP
PHP 中的批处理的实现
Jun 14 PHP
PHP之变量、常量学习笔记
Mar 27 PHP
linux php mysql数据库备份实现代码
Mar 10 PHP
用穿越火线快速入门php面向对象
Feb 22 PHP
php替换超长文本中的特殊字符的函数代码
May 22 PHP
php 字符串压缩方法比较示例
Jan 23 PHP
php实现的短网址算法分享
Jun 20 PHP
PHP中使用socket方式GET、POST数据实例
Apr 02 PHP
PHP实现中国公民身份证号码有效性验证示例代码
May 03 PHP
解决php 处理 form 表单提交多个 name 属性值相同的 input 标签问题
May 11 PHP
PHP实现用户异地登录提醒功能的方法【基于thinkPHP框架】
Mar 15 PHP
php读取纯真ip数据库使用示例
Jan 26 #PHP
curl不使用文件存取cookie php使用curl获取cookie示例
Jan 26 #PHP
php版小黄鸡simsimi聊天机器人接口分享
Jan 26 #PHP
百度ping方法使用示例 自动ping百度
Jan 26 #PHP
PHP弹出提示框并跳转到新页面即重定向到新页面
Jan 24 #PHP
header导出Excel应用示例
Jan 24 #PHP
使用openssl实现rsa非对称加密算法示例
Jan 24 #PHP
You might like
php 禁止页面缓存输出
2009/01/07 PHP
PHP CURL CURLOPT参数说明(curl_setopt)
2013/09/30 PHP
php实现随机显示图片方法汇总
2015/05/21 PHP
Javascript Jquery 遍历Json的实现代码
2010/03/31 Javascript
最佳JS代码编写的14条技巧
2011/01/09 Javascript
自己动手制作jquery插件之自动添加删除行的实现
2011/10/13 Javascript
jquery ajax应用中iframe自适应高度问题解决方法
2014/04/12 Javascript
js数组的操作指南
2014/12/28 Javascript
快速学习JavaScript的6个思维技巧
2015/10/13 Javascript
通过正则表达式获取url中参数的简单实现
2016/06/07 Javascript
canvas绘制的直线动画
2017/01/23 Javascript
详解如何在Angular优雅编写HTTP请求
2018/12/05 Javascript
express框架中使用jwt实现验证的方法
2019/08/25 Javascript
如何通过javaScript去除字符串两端的空白字符
2020/02/06 Javascript
JavaScript 实现轮播图特效的示例
2020/11/05 Javascript
Python简单实现Base64编码和解码的方法
2017/04/29 Python
python和ruby,我选谁?
2017/09/13 Python
python中logging库的使用总结
2017/10/18 Python
详解Django+Uwsgi+Nginx的生产环境部署
2018/06/25 Python
Window环境下Scrapy开发环境搭建
2018/11/18 Python
python中yield的用法详解——最简单,最清晰的解释
2019/04/04 Python
Python创建文件夹与文件的快捷方法
2020/12/08 Python
python中常用的数据结构介绍
2021/01/12 Python
css3实现动画的三种方式
2020/08/24 HTML / CSS
伯克斯奥特莱斯:Burkes Outlet
2019/03/30 全球购物
洗车工岗位职责
2014/03/15 职场文书
车辆转让协议书
2014/09/24 职场文书
关于运动会的广播稿50字
2014/10/17 职场文书
表扬通报怎么写
2015/01/16 职场文书
家装业务员岗位职责
2015/04/03 职场文书
倡议书格式及范文
2015/04/29 职场文书
2016年毕业实习心得体会范文
2015/10/09 职场文书
2016年七夕情人节宣传语
2015/11/25 职场文书
个人销售励志奋斗口号
2019/12/05 职场文书
详解PHP设计模式之依赖注入模式
2021/05/25 PHP
Win11自动黑屏怎么办 Win11自动黑屏设置教程
2022/07/15 数码科技