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,'./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.')'); //标题内容 $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], '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 内容:; 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], '.')!==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_EX)) { // 进行排它型锁定 fwrite($fp, $item_results[6]); flock($fp, LOCK_UN); // 释放锁定 } 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 附件:; 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, '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); //给目录操作权限 return true; } ?>
有图有真相:

PHP在現代編程中仍然是一個強大且廣泛使用的工具,尤其在web開發領域。 1)PHP易用且與數據庫集成無縫,是許多開發者的首選。 2)它支持動態內容生成和麵向對象編程,適合快速創建和維護網站。 3)PHP的性能可以通過緩存和優化數據庫查詢來提升,其廣泛的社區和豐富生態系統使其在當今技術棧中仍具重要地位。

在PHP中,弱引用是通過WeakReference類實現的,不會阻止垃圾回收器回收對象。弱引用適用於緩存系統和事件監聽器等場景,需注意其不能保證對象存活,且垃圾回收可能延遲。

\_\_invoke方法允許對象像函數一樣被調用。 1.定義\_\_invoke方法使對象可被調用。 2.使用$obj(...)語法時,PHP會執行\_\_invoke方法。 3.適用於日誌記錄和計算器等場景,提高代碼靈活性和可讀性。

Fibers在PHP8.1中引入,提升了並發處理能力。 1)Fibers是一種輕量級的並發模型,類似於協程。 2)它們允許開發者手動控制任務的執行流,適合處理I/O密集型任務。 3)使用Fibers可以編寫更高效、響應性更強的代碼。

PHP社區提供了豐富的資源和支持,幫助開發者成長。 1)資源包括官方文檔、教程、博客和開源項目如Laravel和Symfony。 2)支持可以通過StackOverflow、Reddit和Slack頻道獲得。 3)開發動態可以通過關注RFC了解。 4)融入社區可以通過積極參與、貢獻代碼和學習分享來實現。

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP不是在消亡,而是在不斷適應和進化。 1)PHP從1994年起經歷多次版本迭代,適應新技術趨勢。 2)目前廣泛應用於電子商務、內容管理系統等領域。 3)PHP8引入JIT編譯器等功能,提升性能和現代化。 4)使用OPcache和遵循PSR-12標準可優化性能和代碼質量。

PHP的未來將通過適應新技術趨勢和引入創新特性來實現:1)適應云計算、容器化和微服務架構,支持Docker和Kubernetes;2)引入JIT編譯器和枚舉類型,提升性能和數據處理效率;3)持續優化性能和推廣最佳實踐。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Atom編輯器mac版下載
最受歡迎的的開源編輯器

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器