這篇文章主要介紹了PHP讀取、解析eml檔案及產生網頁詳解,結合執行個體形式分析了PHP操作eml檔案的讀取、解析、轉換等相關實現技巧與注意事項,並附帶demo源碼供讀者下載參考,需要的朋友可以參考下
具體如下:
php讀取eml執行個體,本執行個體可以將匯出eml檔案解析成本文,並且可以將附件儲存到伺服器。不多說直接貼代碼了。
<?php
// Author: richard e42083458@163.com
// gets parameters
error_ reporting(E_ ALL ^ (E_ WARNING|E_ NOTICE));
header("Content-type: text/html; charset=utf-8");
echo "<pre>";
define(EML_ FILE_ PATH,'./yjdata/');
//if ($filename == '') $filename = '21724696_ niuyufu@qiaodazhao.com_ ZC4422-r7GMz_ R9QF3K6XUhmJOXd4c.eml';
//if ($filename == '') $filename = '21724696_ niuyufu@qiaodazhao.com_ ZC3218-dGquMgm7ytdF6HQgpSReC4c.eml';
//if ($filename == '') $filename = '163.eml';
//if ($filename == '') $filename = '166.eml';
//if ($filename == '') $filename = ' nyf.eml ';
//if ($filename == '') $filename = 'email_ header_ icon.eml ';
if ($filename == '') $filename = '20141230133705.eml';
$eml_ file = EML_ FILE_ PATH.$filename;
if (!($content = fread(fopen(EML_ FILE_ PATH.$filename, 'rb'), filesize(EML_ FILE_ PATH.$filename))))
die('File not found ('.EML_ FILE_ PATH.$filename.')');
//Title Content
$pattern="/Subject: (.*?)\n/ims";
preg_ match($pattern,$content,$subject_ results);
$subject = getdecodevalue($subject_ results[1]);
Echo "title):. $subject;
//From:
$pattern="/From: .*?<(.*?)>/ims";
preg_ match($pattern,$content,$from_ results);
$from = $from_ results[1];
echo "\n\r";
Echo "from": $from;
//Attention:
$pattern="/To:(.*?):/ims";
preg_ match($pattern,$content,$to_ results);
$pattern="/<(.*?)>/ims";
preg_ match_ all($pattern,$to_ results[1],$to_ results2);
if(count($to_ results2[1])>0){
$to = $to_ results2[1];
}else{
$pattern="/To:(.*?)\n/ims";
preg_ match($pattern,$content,$to_ results);
$to = $to_ results[1];
}
echo "\n\r";
Echo "to":;
print_ r($to);
echo "\n\r";
//Text content
$pattern = "/Content-Type: multipart\/alternative;.*?boundary=\"(.*?)\"/ims";
preg_ match($pattern,$content,$results);
if($results[1]!=""){
$seperator = "--".$results[1];
}else{
Die ("boundary matching failed");
}
$spcontent = explode($seperator, $content);
$items = array();
$keyid = 0;
$email_ front_ content_ array = array();
foreach($spcontent as $spkey=>$item) {
//Match header code and other information
$pattern = "/Content-Type: ([^;]*?);.*?charset=(.*?)\nContent-Transfer-Encoding: (.*?)\n/ims";
preg_ match($pattern,$item,$item_ results);
if(count($item_ results)==4){
$Content_ code = str_ replace($item_ results[0],"",$item);
$item_ results[4] = $Content_ code;
if(trim($item_ results[3])=="base64"){
$item_ results[5] = base64_ decode($item_ results[4]);
}
if(trim($item_ results[3])=="quoted-printable"){
$item_ results[5] = quoted_ printable_ decode($item_ results[4]);
}
$item_ results[5] = mb_ convert_ encoding($item_ results[5], 'UTF-8', trim($item_ results[2]));
//echo $item_ results[5];exit;
$email_ front_ content_ array[] = $item_ results;
}
}
foreach ($email_ front_ content_ array as $email_ front_ content_ each_ key=>$email_ front_ content_ each_ value){
if($email_ front_ content_ each_ value[1]=='text/html'){
$content_ html = $email_ front_ content_ each_ value[5];
break;
}else{
$content_ html = $email_ front_ content_ each_ value[5];
}
}
Echo "content":;
echo "\n\r";
echo $content_ html;
echo "\n\r";
//Annex content
$pattern = "/Content-Type: multipart\/mixed;.*?boundary=\"(.*?)\"/ims";
preg_ match($pattern,$content,$results);
if($results[1]!=""){
$seperator = "--".$results[1];
$spcontent = explode($seperator, $content);
$items = array();
$keyid = 0;
$email_ attachment_ content_ array = array();
foreach($spcontent as $spkey=>$item) {
//Match header code and other information
$pattern = "/Content-Type: ([^;]*?);.*?name=(.*?)\nContent-Transfer-Encoding: (.*?)\nContent-Disposition: attachment;.*?filename=(.*?)\n/ims";
preg_ match($pattern,$item,$item_ results);
//print_ r($item_ results);
if(count($item_ results)==5){
$Content_ code = str_ replace($item_ results[0],"",$item);
$item_ results[5] = trim($Content_ code);
if(trim($item_ results[3])=="base64"){
$item_ results[6] = base64_ decode($item_ results[5]);
}
if(trim($item_ results[3])=="quoted-printable"){
$item_ results[6] = quoted_ printable_ decode($item_ results[5]);
}
$item_ results[7] = str_ replace("\"","",getdecodevalue($item_ results[2]));
$item_ results[8] = str_ replace("\"","",getdecodevalue($item_ results[4]));
//Save attachment content to server?
//When the file name conforms to the specification: when there is a suffix.
if(strrpos($item_ results[8], '.')!==false){
$ext = substr($item_ results[8], strrpos($item_ results[8], '.') + 1);
//$filename = "./yjdata/attachment/".date("YmdHis").mt_ rand(10000,99999).".".trim($ext);
$attachment_ filename = "./yjdata/attachment/".trim(str_ replace("\"","",getbase64code($item_ results[4]))).".".trim($ext);
mkdirs(dirname($attachment_ filename));
$fp = fopen($attachment_ filename, "w+");
if (flock($fp, LOCK_ Exclusive locking
fwrite($fp, $item_ results[6]);
flock($fp, LOCK_ UN); / / release the lock
} else {
//echo "Couldn't lock the file !";
}
fclose($fp);
$item_ results[9] = $attachment_ filename;
$email_ attachment_ content_ array[] = $item_ results;
}
}
}
//print_ r($email_ attachment_ content_ array);
}
if(count($email_ attachment_ content_ array)>0){
"Echo" is attached;
echo "\n\r";
//Attachment reading
foreach($email_ attachment_ content_ array as $email_ attachment_ content_ each_ key=>$email_ attachment_ content_ each_ value){
unset($email_ attachment_ content_ each_ value[5]);
unset($email_ attachment_ content_ each_ value[6]);
print_ r($email_ attachment_ content_ each_ value[8]);
print_ r($email_ attachment_ content_ each_ value[9]);
}
}
function getbase64code($content){
$pattern="/=\?GB2312\?B\?(.*?)\?=|=\?GBK\?B\?(.*?)\?=|=\?UTF-8\?B\?(.*?)\?=/ims";
preg_ match($pattern,$content,$subject_ results);
if($subject_ results[1]!=""){
$subject = $subject_ results[1];
$charset = "GB2312";
}
elseif($subject_ results[2]!=""){
$subject = $subject_ results[2];
$charset = "GBK";
}
elseif($subject_ results[3]!=""){
$subject = $subject_ results[3];
$charset = "UTF-8";
}else{
$subject = $content;
$charset = "";
}
return $subject;
}
function getdecodevalue($content){
$pattern="/=\?GB2312\?B\?(.*?)\?=|=\?GBK\?B\?(.*?)\?=|=\?UTF-8\?B\?(.*?)\?=/ims";
preg_ match($pattern,$content,$subject_ results);
if($subject_ results[1]!=""){
$subject = base64_ decode($subject_ results[1]);
$charset = "GB2312";
}
elseif($subject_ results[2]!=""){
$subject = base64_ decode($subject_ results[2]);
$charset = "GBK";
}
elseif($subject_ results[3]!=""){
$subject = base64_ decode($subject_ results[3]);
$charset = "UTF-8";
}else{
$subject = $content;
$charset = "";
}
if($charset!=""){
$subject = mb_ convert_ encoding($subject, 'UTF-8', $charset);
}
return $subject;
}
function mkdirs($dir)
{
if(!is_ dir($dir))
{
if(!mkdirs(dirname($dir))){
return false;
}
if(!mkdir($dir,0777)){
return false;
}
}
Chmod ($dir, 777); / / give directory operation permission
return true;
}
> >
相關推薦:
php如何讀取解析eml檔案以及產生網頁的樣本分享
php讀取eml執行個體、php解析eml、eml解析成網頁_PHP教程
php讀取eml範例、php解析eml、eml解析成網頁