mail mime.php,PHP 利用Mail_MimeDecode类提取邮件正文

根据主流的邮件解析类Mail_MimeDecode,提取邮件正文。如有问题请指教。

#获取邮件正文

#[path] 邮件路径

function getMailBody($path){

if(!is_file($path)) return false;

$contents_c = file_get_contents($path);

if(empty($contents_c)) return false;

$mmd_c = new Mail_mimeDecode($contents_c); //创建Mail_mimeDecode类的实例

$sr = $mmd_c->decode(array (

‘include_bodies‘ => true, //是否包含邮件正文

‘decode_bodies‘ => false,

‘decode_headers‘ => true

));

unset($mail_part);

$mail_part = getMailPart($sr);

$mail_code = $mail_part->headers;

$mail_code = empty($mail_code[‘content-transfer-encoding‘])?‘‘:$mail_code[‘content-transfer-encoding‘]; //编码格式

$mail_type = $mail_part->ctype_parameters;

$mail_type = empty($mail_type[‘charset‘])?‘GBK‘:$mail_type[‘charset‘];

$mail_body = $mail_part->body; //正文内容

if ($mail_code == "base64") { //判断编码格式,受‘decode_bodies‘影响

$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);

}

$body = $text;

return $body;

}

function getMailPart($sr){

$accept_primary = array("multipart", "text", "message"); //允许显示的邮件主要正文,过滤image等附件格式

if (property_exists($sr, ‘parts‘)) {

$mail_part = $sr->parts;

foreach($mail_part as $k => $m_part){

if(in_array($m_part->ctype_primary, $accept_primary)){

if($m_part->ctype_secondary == "html"){

$mail_part = $m_part;

break;

}else{

$mail_part = &getMailPart($m_part);

}//end if

}//end if

}//end for

} else {

$mail_part = $sr;

}//end if

return $mail_part;

}

原文:http://blog.csdn.net/dmtnewtons_blog/article/details/19326363

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值