久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

PHP讀取、解析eml文件及生成網頁的方法示例

這篇文章主要介紹了PHP讀取、解析eml文件及生成網頁的方法,結合實例形式分析了PHP操作eml文件的讀取、解析、轉換等相關實現技巧與注意事項,并附帶demo源碼供讀者下載參考,需要的朋友

本文實例講述了PHP讀取、解析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>";
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: (.*?)\n/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 "\n\r";
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:(.*?)\n/ims";
  preg_match($pattern,$content,$to_results);
  $to = $to_results[1];
}
echo "\n\r";
echo "收件人:";
print_r($to);
echo "\n\r";
//正文內容
$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=(.*?)\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 "內容:";
echo "\n\r";
echo $content_html;
echo "\n\r";
//附件內容
$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=(.*?)\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]));
      //保存附件內容到服務器?
      //符合規范的文件名時:有后綴名時。
      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 "\n\r";
  //附件讀取
  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讀取、解析eml文件及生成網頁的方法示例

附:完整實例代碼點擊此處本站下載

【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。

相關文檔推薦

這篇文章主要介紹了PHP有序表查找之插值查找算法,簡單分析了插值查找算法的概念、原理并結合實例形式分析了php實現針對有序表插值查找的相關操作技巧,需要的朋友可以參考下
下面小編就為大家分享一篇ThinkPHP整合datatables實現服務端分頁的示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
下面小編就為大家分享一篇PHP實現APP微信支付的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
這篇文章主要介紹了PHP實現的多維數組排序算法,結合實例形式對比分析了php針對多維數組及帶有鍵名的多維數組進行排序相關操作技巧與注意事項,需要的朋友可以參考下
這篇文章主要為大家詳細介紹了php結合ajaxuploadfile實現無刷新文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本篇文章給大家詳細介紹了PHP開發接口使用RSA進行加密解密方法,對此有興趣的朋友可以學習下。
主站蜘蛛池模板: 最新av在线网址 | 在线精品一区二区三区 | 精品日韩在线观看 | 国产精品不卡一区 | 中文字幕在线观看一区 | 日本精品视频一区二区 | 91影院在线观看 | 久久久精品一区 | 精品视频一区二区 | 久久国产精品无码网站 | 国产一区二区三区四 | 日韩美女在线看免费观看 | 一区二区蜜桃 | 精品国偷自产在线 | 99精品九九 | 欧美一区免费 | 在线播放中文 | 国产小视频自拍 | 久久av综合 | 国产精品一区二区三区久久 | 午夜影院免费体验区 | 日韩在线综合 | 国产精品视频一区二区三区四区国 | 春色av| 伊人焦久影院 | 国产成人网| 亚洲一一在线 | 欧美一区二区视频 | 天堂在线免费视频 | 二区在线视频 | 在线欧美亚洲 | 久久婷婷国产麻豆91 | 国产精品久久久久久吹潮 | 成人av免费网站 | 久久精品视频在线观看 | 香蕉一区| 天堂一区在线 | 精品欧美一区二区三区久久久小说 | 亚州春色 | 国产一级片免费看 | 欧美视频三区 |