最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年09月04日 09:30:40   作者:e421083458  
這篇文章主要介紹了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.')');
//標(biāo)題內(nèi)容
$pattern="/Subject: (.*?)\n/ims";
preg_match($pattern,$content,$subject_results);
$subject = getdecodevalue($subject_results[1]);
echo "標(biāo)題:".$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)) { // 進(jìn)行排它型鎖定
          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;
}
?>

有圖有真相:

附:完整實例代碼點(diǎn)擊此處本站下載

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php文件操作總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》及《php字符串(string)用法總結(jié)

希望本文所述對大家PHP程序設(shè)計有所幫助。

相關(guān)文章

  • 詳解EventDispatcher事件分發(fā)組件

    詳解EventDispatcher事件分發(fā)組件

    Symfony EventDispatcher以一個簡單有效的方式實現(xiàn)了中介者模式,事件分發(fā)器就是那個中介,讓系統(tǒng)和插件不會耦合在一起,這讓上面的插件系統(tǒng)成為可能,而且他會讓你的項目可擴(kuò)展性更好。本文將對此進(jìn)行詳細(xì)介紹,需要的朋友一起來看下吧
    2016-12-12
  • 提升PHP性能的21種方法介紹

    提升PHP性能的21種方法介紹

    本篇文章是對提升PHP性能的21種方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • Thinkphp框架開發(fā)移動端接口(2)

    Thinkphp框架開發(fā)移動端接口(2)

    這篇文章主要介紹了thinkphp框架開發(fā)移動端接口的第2種方法,實現(xiàn)移動端訪問自動切換移動主題模板,從而實現(xiàn)偽app訪問,感興趣的小伙伴們可以參考一下
    2016-08-08
  • PHP兩種實現(xiàn)無級遞歸分類的方法

    PHP兩種實現(xiàn)無級遞歸分類的方法

    本文主要介紹了PHP兩種實現(xiàn)無級遞歸分類的方法,具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • php中convert_uuencode()與convert_uuencode函數(shù)用法實例

    php中convert_uuencode()與convert_uuencode函數(shù)用法實例

    這篇文章主要介紹了php中convert_uuencode()與convert_uuencode函數(shù)用法,以實例形式了convert_uuencode()與convert_uuencode進(jìn)行編碼與解碼的方法,是非常實用的技巧,需要的朋友可以參考下
    2014-11-11
  • PHP Token(令牌)設(shè)計

    PHP Token(令牌)設(shè)計

    PHP Token(令牌)設(shè)計 設(shè)計目標(biāo): 避免重復(fù)提交數(shù)據(jù). 檢查來路,是否是外部提交 匹配要執(zhí)行的動作(如果有多個邏輯在同一個頁面實現(xiàn),比如新增,刪除,修改放到一個PHP文件里操作) 這里所說的token是在頁面顯示的時候,寫到FORM的一個隱藏表單項(type=hidden). token不可明文,如果是明文,那就太危險了,所以要采用一定的加密方式.密文要可逆.俺算法很白癡,所以采用了網(wǎng)上一個現(xiàn)成的方法.
    2008-03-03
  • 詳解PHP使用OSS上傳文件

    詳解PHP使用OSS上傳文件

    這篇文章主要介紹了PHP使用OSS上傳文件,對OSS感興趣的同學(xué),可以參考并且實驗一下
    2021-04-04
  • PHP使用數(shù)組實現(xiàn)矩陣數(shù)學(xué)運(yùn)算的方法示例

    PHP使用數(shù)組實現(xiàn)矩陣數(shù)學(xué)運(yùn)算的方法示例

    這篇文章主要介紹了PHP使用數(shù)組實現(xiàn)矩陣數(shù)學(xué)運(yùn)算的方法,結(jié)合具體實例形式分析了php基于數(shù)組實現(xiàn)矩陣表示與運(yùn)算的相關(guān)操作技巧,需要的朋友可以參考下
    2017-05-05
  • php回溯算法計算組合總和的實例代碼

    php回溯算法計算組合總和的實例代碼

    在本篇文章里小編給大家整理的是一篇關(guān)于php回溯算法計算組合總和的實例代碼,有需要的朋友們可以學(xué)習(xí)參考下。
    2021-07-07
  • PHP實現(xiàn)的隨機(jī)紅包算法示例

    PHP實現(xiàn)的隨機(jī)紅包算法示例

    這篇文章主要介紹了PHP實現(xiàn)的隨機(jī)紅包算法,結(jié)合實例形式分析了php隨機(jī)紅包算法的原理、實現(xiàn)方法與相關(guān)注意事項,需要的朋友可以參考下
    2017-08-08

最新評論

修水县| 双牌县| 信丰县| 东光县| 平罗县| 牡丹江市| 南投县| 锦屏县| 华池县| 松原市| 宜章县| 三亚市| 盐边县| 内黄县| 茶陵县| 钟山县| 武陟县| 中西区| 辽宁省| 泰兴市| 澳门| 上饶市| 辉南县| 顺平县| 安仁县| 天祝| 西乌| 定安县| 综艺| 财经| 武隆县| 延寿县| 邯郸市| 宜川县| 阿拉善左旗| 保德县| 阳东县| 榆林市| 睢宁县| 太原市| 鄂尔多斯市|