PHP 利用Mail_MimeDecode类提取邮件正文

参考链接:http://blog.csdn.net/dmtnewtons_blog/article/details/18765289

参考链接:http://blog.csdn.net/dmtnewtons_blog/article/details/19327105


根据主流的邮件解析类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;
}


PHP 利用Mail_MimeDecode类提取邮件正文

上一篇:jQuery深入之大图轮播原理.


下一篇:[Mugeda HTML5技术教程之17] 理解Mugeda访问统计结果