PHP实例:读取、解析eml文件以及生成网页

PHP是现今很多编程人员会经常使用到的,它有着非常大的作用,能为我们带来很大的便利。这次文章就给大家分享个实例,通过PHP读取、解析文件以及生成网页,这也是一个非常不错的应用,下面我们具体来看看实现代码。

php读取eml实例,本实例可以将导出eml文件解析成正文,并且可以将附件保存到服务器。不多说直接贴代码了。

  1. <?php
  2. // Author: richard e42083458@163.com
  3. // gets parameters
  4. error_reporting(E_ALL ^ (E_WARNING|E_NOTICE));
  5. header("Content-type: text/html; charset=utf-8");
  6. echo "<pre>";
  7. define(EML_FILE_PATH,'./yjdata/');
  8. //if ($filename == '') $filename = '21724696_niuyufu@qiaodazhao.com_ZC4422-r7GMz_R9QF3K6XUhmJOXd4c.eml';
  9. //if ($filename == '') $filename = '21724696_niuyufu@qiaodazhao.com_ZC3218-dGquMgm7ytdF6HQgpSReC4c.eml';
  10. //if ($filename == '') $filename = '163.eml';
  11. //if ($filename == '') $filename = '166.eml';
  12. //if ($filename == '') $filename = 'nyf.eml';
  13. //if ($filename == '') $filename = 'email_header_icon.eml';
  14. if ($filename == '') $filename = '20141230133705.eml';
  15. $eml_file = EML_FILE_PATH.$filename;
  16. if (!($content = fread(fopen(EML_FILE_PATH.$filename, 'rb'), filesize(EML_FILE_PATH.$filename))))
  17. die('File not found ('.EML_FILE_PATH.$filename.')');
  18. //标题内容
  19. $pattern="/Subject: (.*?)\n/ims";
  20. preg_match($pattern,$content,$subject_results);
  21. $subject = getdecodevalue($subject_results[1]);
  22. echo "标题:".$subject;
  23. //发件人:
  24. $pattern="/From: .*?<(.*?)>/ims";
  25. preg_match($pattern,$content,$from_results);
  26. $from = $from_results[1];
  27. echo "\n\r";
  28. echo "发件人:".$from;
  29. //收件人:
  30. $pattern="/To:(.*?):/ims";
  31. preg_match($pattern,$content,$to_results);
  32. $pattern="/<(.*?)>/ims";
  33. preg_match_all($pattern,$to_results[1],$to_results2);
  34. if(count($to_results2[1])>0){
  35. $to = $to_results2[1];
  36. }else{
  37. $pattern="/To:(.*?)\n/ims";
  38. preg_match($pattern,$content,$to_results);
  39. $to = $to_results[1];
  40. }
  41. echo "\n\r";
  42. echo "收件人:";
  43. print_r($to);
  44. echo "\n\r";
  45. //正文内容
  46. $pattern = "/Content-Type: multipart\/alternative;.*?boundary=\"(.*?)\"/ims";
  47. preg_match($pattern,$content,$results);
  48. if($results[1]!=""){
  49. $seperator = "--".$results[1];
  50. }else{
  51. die("boundary匹配失败");
  52. }
  53. $spcontent = explode($seperator, $content);
  54. $items = array();
  55. $keyid = 0;
  56. $email_front_content_array = array();
  57. foreach($spcontent as $spkey=>$item) {
  58. //匹配header编码等信息
  59. $pattern = "/Content-Type: ([^;]*?);.*?charset=(.*?)\nContent-Transfer-Encoding: (.*?)\n/ims";
  60. preg_match($pattern,$item,$item_results);
  61. if(count($item_results)==4){
  62. $Content_code = str_replace($item_results[0],"",$item);
  63. $item_results[4] = $Content_code;
  64. if(trim($item_results[3])=="base64"){
  65. $item_results[5] = base64_decode($item_results[4]);
  66. }
  67. if(trim($item_results[3])=="quoted-printable"){
  68. $item_results[5] = quoted_printable_decode($item_results[4]);
  69. }
  70. $item_results[5] = mb_convert_encoding($item_results[5], 'UTF-8', trim($item_results[2]));
  71. //echo $item_results[5];exit;
  72. $email_front_content_array[] = $item_results;
  73. }
  74. }
  75. foreach ($email_front_content_array as $email_front_content_each_key=>$email_front_content_each_value){
  76. if($email_front_content_each_value[1]=='text/html'){
  77. $content_html = $email_front_content_each_value[5];
  78. break;
  79. }else{
  80. $content_html = $email_front_content_each_value[5];
  81. }
  82. }
  83. echo "内容:";
  84. echo "\n\r";
  85. echo $content_html;
  86. echo "\n\r";
  87. //附件内容
  88. $pattern = "/Content-Type: multipart\/mixed;.*?boundary=\"(.*?)\"/ims";
  89. preg_match($pattern,$content,$results);
  90. if($results[1]!=""){
  91. $seperator = "--".$results[1];
  92. $spcontent = explode($seperator, $content);
  93. $items = array();
  94. $keyid = 0;
  95. $email_attachment_content_array = array();
  96. foreach($spcontent as $spkey=>$item) {
  97. //匹配header编码等信息
  98. $pattern = "/Content-Type: ([^;]*?);.*?name=(.*?)\nContent-Transfer-Encoding: (.*?)\nContent-Disposition: attachment;.*?filename=(.*?)\n/ims";
  99. preg_match($pattern,$item,$item_results);
  100. //print_r($item_results);
  101. if(count($item_results)==5){
  102. $Content_code = str_replace($item_results[0],"",$item);
  103. $item_results[5] = trim($Content_code);
  104. if(trim($item_results[3])=="base64"){
  105. $item_results[6] = base64_decode($item_results[5]);
  106. }
  107. if(trim($item_results[3])=="quoted-printable"){
  108. $item_results[6] = quoted_printable_decode($item_results[5]);
  109. }
  110. $item_results[7] = str_replace("\"","",getdecodevalue($item_results[2]));
  111. $item_results[8] = str_replace("\"","",getdecodevalue($item_results[4]));
  112. //保存附件内容到服务器?
  113. //符合规范的文件名时:有后缀名时。
  114. if(strrpos($item_results[8], '.')!==false){
  115. $ext = substr($item_results[8], strrpos($item_results[8], '.') + 1);
  116. //$filename = "./yjdata/attachment/".date("YmdHis").mt_rand(10000,99999).".".trim($ext);
  117. $attachment_filename = "./yjdata/attachment/".trim(str_replace("\"","",getbase64code($item_results[4]))).".".trim($ext);
  118. mkdirs(dirname($attachment_filename));
  119. $fp = fopen($attachment_filename, "w+");
  120. if (flock($fp, LOCK_EX)) { // 进行排它型锁定
  121. fwrite($fp, $item_results[6]);
  122. flock($fp, LOCK_UN); // 释放锁定
  123. } else {
  124. //echo "Couldn't lock the file !";
  125. }
  126. fclose($fp);
  127. $item_results[9] = $attachment_filename;
  128. $email_attachment_content_array[] = $item_results;
  129. }
  130. }
  131. }
  132. //print_r($email_attachment_content_array);
  133. }
  134. if(count($email_attachment_content_array)>0){
  135. echo "附件:";
  136. echo "\n\r";
  137. //附件读取
  138. foreach($email_attachment_content_array as $email_attachment_content_each_key=>$email_attachment_content_each_value){
  139. unset($email_attachment_content_each_value[5]);
  140. unset($email_attachment_content_each_value[6]);
  141. print_r($email_attachment_content_each_value[8]);
  142. print_r($email_attachment_content_each_value[9]);
  143. }
  144. }
  145. function getbase64code($content){
  146. $pattern="/=\?GB2312\?B\?(.*?)\?=|=\?GBK\?B\?(.*?)\?=|=\?UTF-8\?B\?(.*?)\?=/ims";
  147. preg_match($pattern,$content,$subject_results);
  148. if($subject_results[1]!=""){
  149. $subject = $subject_results[1];
  150. $charset = "GB2312";
  151. }
  152. elseif($subject_results[2]!=""){
  153. $subject = $subject_results[2];
  154. $charset = "GBK";
  155. }
  156. elseif($subject_results[3]!=""){
  157. $subject = $subject_results[3];
  158. $charset = "UTF-8";
  159. }else{
  160. $subject = $content;
  161. $charset = "";
  162. }
  163. return $subject;
  164. }
  165. function getdecodevalue($content){
  166. $pattern="/=\?GB2312\?B\?(.*?)\?=|=\?GBK\?B\?(.*?)\?=|=\?UTF-8\?B\?(.*?)\?=/ims";
  167. preg_match($pattern,$content,$subject_results);
  168. if($subject_results[1]!=""){
  169. $subject = base64_decode($subject_results[1]);
  170. $charset = "GB2312";
  171. }
  172. elseif($subject_results[2]!=""){
  173. $subject = base64_decode($subject_results[2]);
  174. $charset = "GBK";
  175. }
  176. elseif($subject_results[3]!=""){
  177. $subject = base64_decode($subject_results[3]);
  178. $charset = "UTF-8";
  179. }else{
  180. $subject = $content;
  181. $charset = "";
  182. }
  183. if($charset!=""){
  184. $subject = mb_convert_encoding($subject, 'UTF-8', $charset);
  185. }
  186. return $subject;
  187. }
  188. function mkdirs($dir)
  189. {
  190. if(!is_dir($dir))
  191. {
  192. if(!mkdirs(dirname($dir))){
  193. return false;
  194. }
  195. if(!mkdir($dir,0777)){
  196. return false;
  197. } //phpfensi.com
  198. }
  199. chmod($dir, 777); //给目录操作权限
  200. return true;
  201. }
  202. ?>