Example of how PHP reads and parses eml files and generates web pages,
This example describes how PHP reads, parses eml files, and generates web pages. We will share this with you for your reference. The details are as follows:
Php reads an eml instance. This instance can parse the exported eml file into a body and save the attachment to the server. I don't need to talk about Code directly.
<? Php // Author: richard e42083458@163.com // gets parameterserror_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 = '000000_niuyufu@qiaodazhao.com_ZC4422-r7GMz_R9QF3K6XUhmJOXd4c.eml'; // if ($ filename = '') $ filename = '000000' niuyufu@qiaodazhao.com _ ZC3218-dGquMgm7ytdF6HQgpSReC4c. Eml'; // if ($ filename = '') $ filename = '2017. eml'; // if ($ filename = '') $ filename = '2017. eml'; // if ($ filename = '') $ filename = 'nyf. eml '; // if ($ filename = '') $ filename = 'email _ header_icon.eml'; if ($ filename ='') $ filename = '2017. 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. '); // The title content $ pattern = "/Subject :(. *?) \ N/ims "; preg_match ($ pattern, $ content, $ subject_results); $ subject = getdecodevalue ($ subject_results [1]); echo" title :". $ subject; // Sender: $ pattern = "/From :. *? <(. *?)> /Ims "; preg_match ($ pattern, $ content, $ from_results); $ from = $ from_results [1]; echo" \ n \ r "; echo" Sender :". $ from; // recipient: $ pattern = "/:(. *?) :/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 = "/:(. *?) \ N/ims "; preg_match ($ pattern, $ content, $ to_results); $ to = $ to_results [1];} echo" \ n \ r "; echo" Recipient: "; print_r ($ to); echo" \ n \ r "; // body Content $ pattern ="/Content-Type: multipart \/alternative ;. *? Boundary = \"(.*?) \ "/Ims"; preg_match ($ pattern, $ content, $ results); if ($ results [1]! = "") {$ Seperator = "--". $ results [1];} else {die ("boundary match failed");} $ spcontent = explode ($ seperator, $ content); $ items = array (); $ keyid = 0; $ email_front_content_array = array (); foreach ($ spcontent as $ spkey => $ item) {// match header encoding 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_conve Rt_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 $ response => $ response) {if ($ email_front_content_each_value [1] = 'text/html ') {$ content_html = $ email_front_content_each_value [5]; break;} else {$ content_html = $ email_front_conten T_each_value [5] ;}} echo "content:"; echo "\ n \ r"; echo $ content_html; echo "\ n \ r "; // The attachment 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) {// matches header encoding 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 the attachment content to the server? // When the file name meets the specifications: 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 (random, 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 )) {// Row-locking fwrite ($ fp, $ item_results [6]); flock ($ fp, LOCK_UN ); // release lock} else {// echo "Couldn't lock the file! ";}Fclose ($ fp); $ item_results [9] = $ attachment_filename; $ region [] = $ item_results ;}}// print_r ($ email_attachment_content_array );} if (count ($ email_attachment_content_array)> 0) {echo "attachment:"; echo "\ n \ r "; // read the attachment foreach ($ email_attachment_content_array as $ response => $ email_attachment_content_each_value) {unset ($ email_attachment_content_each_value [5] ); Unset ($ response [6]); print_r ($ email_attachment_content_each_value [8]); print_r ($ response [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 for the Directory operation permission }?>
There is a picture with the truth:
Appendix:Click here for the complete instance codeDownload from this site.