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

PHP讀取、解析eml文件及生成網(wǎng)頁的方法示例

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

本文實例講述了PHP讀取、解析eml文件及生成網(wǎng)頁的方法。分享給大家供大家參考,具體如下:

php讀取eml實例,本實例可以將導(dǎo)出eml文件解析成正文,并且可以將附件保存到服務(wù)器。不多說直接貼代碼了。

<?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.')');
//標題內(nèi)容
$pattern="/Subject: (.*?)\n/ims";
preg_match($pattern,$content,$subject_results);
$subject = getdecodevalue($subject_results[1]);
echo "標題:".$subject;
//發(fā)件人:
$pattern="/From: .*?<(.*?)>/ims";
preg_match($pattern,$content,$from_results);
$from = $from_results[1];
echo "\n\r";
echo "發(fā)件人:".$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";
//正文內(nèi)容
$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 "內(nèi)容:";
echo "\n\r";
echo $content_html;
echo "\n\r";
//附件內(nèi)容
$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]));
      //保存附件內(nèi)容到服務(wù)器?
      //符合規(guī)范的文件名時:有后綴名時。
      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);  //給目錄操作權(quán)限
  return true;
}
?>

有圖有真相:

PHP讀取、解析eml文件及生成網(wǎng)頁的方法示例

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

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

相關(guān)文檔推薦

這篇文章主要介紹了PHP有序表查找之插值查找算法,簡單分析了插值查找算法的概念、原理并結(jié)合實例形式分析了php實現(xiàn)針對有序表插值查找的相關(guān)操作技巧,需要的朋友可以參考下
下面小編就為大家分享一篇ThinkPHP整合datatables實現(xiàn)服務(wù)端分頁的示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
下面小編就為大家分享一篇PHP實現(xiàn)APP微信支付的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
這篇文章主要介紹了PHP實現(xiàn)的多維數(shù)組排序算法,結(jié)合實例形式對比分析了php針對多維數(shù)組及帶有鍵名的多維數(shù)組進行排序相關(guān)操作技巧與注意事項,需要的朋友可以參考下
這篇文章主要為大家詳細介紹了php結(jié)合ajaxuploadfile實現(xiàn)無刷新文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本篇文章給大家詳細介紹了PHP開發(fā)接口使用RSA進行加密解密方法,對此有興趣的朋友可以學(xué)習(xí)下。
主站蜘蛛池模板: 国产日韩欧美另类 | 免费不卡视频 | 国产高清一区二区三区 | 亚洲精品国产电影 | 91一区二区三区 | 99精品国产一区二区青青牛奶 | 国产精品一码二码三码在线 | 欧美一区二区三区四区在线 | 日韩欧美视频在线 | 一区二区国产在线观看 | 欧美激情第一区 | 欧美视频在线播放 | 五月天婷婷久久 | 久久亚洲春色中文字幕久久久 | 欧美美女一区二区 | 九九热在线观看视频 | 亚洲第一天堂 | 欧美精品一区三区 | 精品一区国产 | 99色视频| 不卡的av在线 | 91中文| 国产午夜精品久久 | 久草成人| 日本久久网 | 久久久久久久综合色一本 | 97色综合| 91精品久久久久久久久久入口 | 欧美日韩在线一区二区 | 天堂综合| 精品国产成人 | 精品不卡| 日韩精品视频在线 | 国产精品久久久久久久粉嫩 | 蜜臀久久 | 日韩成人免费视频 | 成人在线精品视频 | 在线成人免费视频 | 亚洲精品乱码久久久久久蜜桃91 | 激情的网站 | 精品久久久久久 |