找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1679|回复: 0
打印 上一主题 下一主题

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

[复制链接]

3444

主题

3465

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11142
跳转到指定楼层
楼主
发表于 2018-2-14 06:00:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            重点为one_mail函数。利用Mail_mimeDecode类从邮件中提取邮件头和邮件正文。
[U]复制代码[/U] 代码如下:
connect($host, 110)) {
//判断登入是否成功
if ($pop3->login($user, $password) === true) {
$list = $pop3->_cmdList();
$sum = $pop3->_cmdStat();
//每次取多少邮件
$step = 2;
$r = 0;
//先对邮箱取$step邮件
for ($n = 0; $n 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  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 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 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  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 = 0 && $kid_birthdy[$j]  (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 = 0 && $kid_birthdy[$j]  
            
            
您可能感兴趣的文章:
  • 用PHP读取IMAP邮件
  • redhat下apache+php+mysql+imap+ldap+jdk+tomcat的安装
  • php实现统计邮件大小的方法
  • php 检查电子邮件函数(自写)
  • php 发送带附件邮件示例
  • php 邮件发送问题解决
  • php使用smtp发送支持附件的邮件示例
  • PHP+Ajax检测用户名或邮件注册时是否已经存在实例教程
  • php实现的发送带附件邮件类实例
  • 使用PHPMailer实现邮件发送代码分享
  • PHP基于imap获取邮件实例
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端