目錄
php读取eml实例、php解析eml、eml解析成网页
首頁 php教程 php手册 php读取eml实例、php解析eml、eml解析成网页

php读取eml实例、php解析eml、eml解析成网页

Jun 13, 2016 am 09:15 AM
實例 網頁

php读取eml实例、php解析eml、eml解析成网页

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 class="code">;

define(EML_FILE_PATH,&#39;./yjdata/&#39;);

//if ($filename == &#39;&#39;) $filename = &#39;21724696_niuyufu@qiaodazhao.com_ZC4422-r7GMz_R9QF3K6XUhmJOXd4c.eml&#39;;
//if ($filename == &#39;&#39;) $filename = &#39;21724696_niuyufu@qiaodazhao.com_ZC3218-dGquMgm7ytdF6HQgpSReC4c.eml&#39;;
//if ($filename == &#39;&#39;) $filename = &#39;163.eml&#39;;
//if ($filename == &#39;&#39;) $filename = &#39;166.eml&#39;;
//if ($filename == &#39;&#39;) $filename = &#39;nyf.eml&#39;;
//if ($filename == &#39;&#39;) $filename = &#39;email_header_icon.eml&#39;;
if ($filename == &#39;&#39;) $filename = &#39;20141230133705.eml&#39;;

$eml_file = EML_FILE_PATH.$filename;

if (!($content = fread(fopen(EML_FILE_PATH.$filename, &#39;rb&#39;), filesize(EML_FILE_PATH.$filename))))
    die(&#39;File not found (&#39;.EML_FILE_PATH.$filename.&#39;)&#39;);
    
//标题内容
$pattern=/Subject: (.*?)
/ims;
preg_match($pattern,$content,$subject_results);
$subject = getdecodevalue($subject_results[1]);
echo 标题:.$subject;

//发件人:
$pattern=/From: .*?<(.*?)>/ims;
preg_match($pattern,$content,$from_results);
$from = $from_results[1];
echo 

;
echo 发件人:.$from;

//收件人:
$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:(.*?)
/ims;
	preg_match($pattern,$content,$to_results);
	$to = $to_results[1];
}
echo 

;
echo 收件人:;
print_r($to);
echo 

;

//正文内容
$pattern = /Content-Type: multipart/alternative;.*?boundary=(.*?)/ims;
preg_match($pattern,$content,$results);

if($results[1]!=){
	$seperator = --.$results[1];
}else{
	die(boundary匹配失败);
}
$spcontent = explode($seperator, $content);
$items = array();
$keyid = 0;

$email_front_content_array = array();
foreach($spcontent as $spkey=>$item) {
	//匹配header编码等信息
  	$pattern = /Content-Type: ([^;]*?);.*?charset=(.*?)
Content-Transfer-Encoding: (.*?)
/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], &#39;UTF-8&#39;, 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]==&#39;text/html&#39;){
		$content_html = $email_front_content_each_value[5];
		break;
	}else{
		$content_html = $email_front_content_each_value[5];
	}
}

echo 内容:;
echo 

;
echo $content_html;
echo 

;

//附件内容
$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) {
		//匹配header编码等信息
	  	$pattern = /Content-Type: ([^;]*?);.*?name=(.*?)
Content-Transfer-Encoding: (.*?)
Content-Disposition: attachment;.*?filename=(.*?)
/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]));	
	  		
	  		//保存附件内容到服务器?
	  		//符合规范的文件名时:有后缀名时。
	  		if(strrpos($item_results[8], &#39;.&#39;)!==false){
		  		$ext = substr($item_results[8], strrpos($item_results[8], &#39;.&#39;) + 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_EX)) { // 进行排它型锁定
				    fwrite($fp, $item_results[6]);
				    flock($fp, LOCK_UN); // 释放锁定
				} else {
				    //echo Couldn&#39;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 附件:;
	echo 

;
	
	//附件读取
	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, &#39;UTF-8&#39;, $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);    //给目录操作权限
    return true; 
}

?>
登入後複製

 

有图有真相:

 

 

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Edge瀏覽器怎麼將網頁用捷徑傳送到桌面? Edge瀏覽器怎麼將網頁用捷徑傳送到桌面? Mar 14, 2024 pm 05:22 PM

  Edge瀏覽器怎麼將網頁用捷徑傳送到桌面?我們很多用戶為了方便直接打開訪問頁面,想要將經常使用的網頁以快捷方式的形式顯示在桌面,但是不知道應該如何操作,針對這個問題,本期小編就來和廣大用戶們分享解決方法,一起來看看今日軟體教學分享的內容。  Edge瀏覽器將網頁傳送到桌面捷徑方法:  1、開啟軟體,點選頁面中的「...」按鈕。  2、在下拉式選單選項中選擇「應用」中的「將此網站作為應用程式安裝」。  3、最後在彈出的視窗中將其

網頁圖片載入不出來怎麼辦? 6種解決辦法 網頁圖片載入不出來怎麼辦? 6種解決辦法 Mar 15, 2024 am 10:30 AM

  有網友發現打開瀏覽器網頁,網頁上的圖片遲遲加載不出來,是怎麼回事?檢查過網路是正常的,那是哪裡出現了問題呢?下面小編就來跟大家介紹一下網頁圖片載入不出來的六種解決方法。網頁圖片載入不出來:  1、網速問題網頁顯示不出圖片有可能是因為電腦的網路速度比較慢,電腦中開啟的軟體比較多,  而我們造訪的圖片比較大,這就可能因為載入逾時,導致圖片顯示不出來,  可以將比較佔網速的軟體將關掉,可以去任務管理器查看一下。  2、造訪人數過多  網頁顯示不出圖片還有可能是因為我們造訪的網頁,在同時段造訪的

網頁自動刷新設定方法 網頁自動刷新設定方法 Oct 26, 2023 am 10:52 AM

設定網頁的自動刷新可以使用HTML的「meta」標籤、JavaScript的「setTimeout」函數、「setInterval」函數或HTTP的」Refresh「頭。詳細介紹:1、使用HTML的「meta」標籤,在HTML文件的「<head>」標籤中,可以使用「meta」標籤來設定網頁的自動刷新;2、JavaScript的「setTimeout」函數等等。

網頁無法存取怎麼辦 網頁無法存取怎麼辦 Sep 06, 2023 am 09:36 AM

網頁無法存取的解決方案有檢查網路連線、清除瀏覽器快取、檢查網頁位址、嘗試使用其他瀏覽器、檢查伺服器狀態、檢查網域解析、檢查防火牆和安全設定和聯絡網站管理員等。詳細介紹:1、檢查網絡連接,確保網絡連接正常,可以嘗試打開其他網頁或使用其他設備進行訪問,確定是否是網絡連接問題,如果其他網頁可以正常訪問,那麼可能是該網頁的問題;2、清除瀏覽器緩存,瀏覽器快取可能導致網頁無法載入等等。

網路連線正常但瀏覽器無法存取網頁可能原因 網路連線正常但瀏覽器無法存取網頁可能原因 Feb 19, 2024 pm 03:45 PM

瀏覽器打不開網頁但是網路正常,可能的原因是多種多樣的。當問題出現時,我們需要逐步排查,才能確定具體的原因並解決問題。首先,確定網頁打不開的現像是侷限於某個特定的瀏覽器或所有瀏覽器都無法開啟網頁。如果只有一個瀏覽器無法開啟網頁,可以嘗試使用其他瀏覽器,如Google瀏覽器、火狐瀏覽器等進行測試。如果其他瀏覽器能夠正常開啟網頁,那麼問題很可能出在該特定瀏覽器上,可能

網頁打不開怎麼解決 網頁打不開怎麼解決 Feb 21, 2024 am 10:24 AM

網頁打不開怎麼解決隨著網路的快速發展,人們越來越依賴網路來獲取資訊、進行交流和娛樂。然而,有時我們會遇到網頁打不開的問題,這給我們帶來了很多困擾。本文將為大家介紹一些常見的方法,幫助解決網頁打不開的問題。首先,我們要確定是因為什麼原因導致網頁打不開。可能的原因包括網頁問題、伺服器問題、瀏覽器設定問題等。以下是一些解決方法:檢查網路連線:首先,我們需要

php怎麼在網頁打開 php怎麼在網頁打開 Mar 22, 2024 pm 03:20 PM

在網頁中執行 PHP 程式碼需要確保 Web 伺服器支援並已正確配置 PHP。可以透過三種方式開啟 PHP: * **伺服器環境:**將 PHP 檔案放置在伺服器根目錄並透過瀏覽器存取。 * **整合開發環境:**將 PHP 檔案放置在指定 Web 根目錄並透過瀏覽器存取。 * **遠端伺服器:**透過伺服器提供的 URL 位址存取託管在遠端伺服器上的 PHP 檔案。

如何使用 JavaScript 實現網頁底部固定導覽列的顯示隱藏效果? 如何使用 JavaScript 實現網頁底部固定導覽列的顯示隱藏效果? Oct 19, 2023 am 09:04 AM

如何使用JavaScript實現網頁底部固定導覽列的顯示隱藏效果?在網頁設計中,固定導覽列是一種常見的設計元素,它可以為使用者提供快速存取網站的導覽功能。當使用者捲動頁面時,導覽列可以固定在頁面底部,提供持續的導覽服務。本文將介紹如何使用JavaScript實現此效果,並提供具體的程式碼範例。實現網頁底部固定導覽列的顯示隱藏效果,可分為以下步驟:步

See all articles