PHP read EML instances, PHP parsing eml, EML parsing into a Web page

Source: Internet
Author: User
Tags base64 explode flock

PHP reads the EML instance, this instance can parse the exported eml file into the body, and can save the attachment to the server. Not much to say the direct code.

If you think this code is good, please click to praise. Thanks ~ ~ ~


<?php//Author:richard [email protected]//gets parameterserror_reporting (e_all ^ (E_WARNING| E_notice)); header ("content-type:text/html; Charset=utf-8 "), echo" <pre> ";d efine (Eml_file_path, './yjdata/');//if ($filename = =") $filename = ' [email  Protected]_zc4422-r7gmz_r9qf3k6xuhmjoxd4c.eml ';//if ($filename = = ") $filename = ' [email protected]_ Zc3218-dgqumgm7ytdf6hqgpsrec4c.eml ';//if ($filename = = ") $filename = ' 163.eml ';//if ($filename = =") $filename = ' 166.e ML ';//if ($filename = = ') $filename = ' nyf.eml ';//if ($filename = = ") $filename = ' email_header_icon.eml '; if ($filename = = ') $filename = ' 20141230133705.eml '; $eml _file = Eml_file_path. $filename; $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 ";p reg_match ($pattern, $content, $subject _results); $subject = Getdecodevalue ($subject _results[1]); echo" title: ". $subject;/sender: $pattern = "/from: .*?< (. *?) >/ims ";p reg_match ($pattern, $content, $from _results); $from = $from _results[1];echo" \n\r "; echo" sender: "$FROM;// Recipient: $pattern = "/to: (. *?):/ IMS ";p Reg_match ($pattern, $content, $to _results); $pattern ="/< (. *?) >/ims ";p reg_match_all ($pattern, $to _results[1], $to _results2), if (count ($to _results2[1]) >0) {$to = $to _ RESULTS2[1];} else{$pattern = "/to: (. *?) \n/ims ";p reg_match ($pattern, $content, $to _results); $to = $to _results[1];} echo "\n\r", echo "addressee:";p rint_r ($to); echo "\n\r";//body content $pattern = "/content-type:multipart\/alternative;. *?boundary=\ "(. *?) \ "/ims";p reg_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 code 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";//attachment content $pattern = "/content-type:multipart\/mixed;. *?boundary=\ "(. *?) \ "/ims";p reg_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 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 contents to server?  When the file name conforms to the canonical name: 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_EX)) {//row-lock 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; $email _attachment_content_array[] = $item _results; }}}//print_r ($email _attachment_content_array);} if (count ($email _attachment_content_array) >0) {echo "attachment:"; echo "\n\r";//Attachment read 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]);p Rint_r ($email _attachment_content_each_ VALUE[8]);p Rint_r ($email _attachmENT_CONTENT_EACH_VALUE[9]);}} function Getbase64code ($content) {$pattern = "/=\? Gb2312\? B\? (.*?) \?=|=\? Gbk\? B\? (.*?) \?=|=\? Utf-8\? B\? (.*?) \?=/ims ";p reg_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 ";p reg_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; }?>

There is a picture of the truth:


Code Download:

http://download.csdn.net/detail/e421083458/8365495


PHP read EML instances, PHP parsing eml, EML parsing into a Web page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.