使用php读取.eml文件

时间:2015-04-05 07:42:57

标签: php

我有php脚本,如下所示。我想问一下,使用这个脚本我怎样才能阅读正常的.eml文件,因为我们在gmail或yahoo中阅读?我怎样才能从中得到附件?

<?php 

/**
*
* This code has been written by Alexis Ulrich (http://alx2002.free.fr)
* This code is in the public domain.
*
*/

define(EML_FILE_PATH,''); 
define(PICTURE_DIRECTORY_PATH,'img/'); 

// gets parameters 
$images = substr($_GET['images'],0,5); 
$filename = substr($_GET['filename'],0,50); 
if ($filename == '') $filename = 'toto.eml'; 
$eml_file = EML_FILE_PATH.$filename; 

// opens file 
if (!($content = fread(fopen(EML_FILE_PATH.$filename, 'r'), filesize(EML_FILE_PATH.$filename)))) 
    die('File not found ('.EML_FILE_PATH.$filename.')'); 

// two kinds of separators 
if (strpos($content,'------_') !== false) $separator = '------_'; 
else $separator = '------=_'; 
$aContent = explode($separator,$content); 

$aImages = array(); 
foreach($aContent as $thisContent) { 
    if (strpos($thisContent,'Content-Type: text/html') !== false) { 
        // email HTML body 
        $thisContent = substr($thisContent,strpos($thisContent,'<!DOCTYPE')); 
        $thisHTMLContent = quoted_printable_decode($thisContent); 
        //echo "<hr>$thisHTMLContent<hr>\n\n"; 
    } 
    if (strpos($thisContent,'Content-Type: image/gif;') !== false) { 
        // base64 gif picture 
        $begin = strpos($thisContent,'Content-ID: <') + 13; 
        $long = strpos(substr($thisContent,strpos($thisContent,'Content-ID: <') + 13),'>'); 
        $img_id = substr($thisContent,$begin,$long); 
        $img_name = substr($thisContent,strpos($thisContent,'name="')+6,strpos($thisContent,'.gif"')-strpos($thisContent,'name="')-6); 
        if (strpos($thisContent,'Content-Location: ') !== false) { 
            $img_location = substr($thisContent,strpos($thisContent,'Content-Location: ')+18); 
            $img_location = substr($img_location,0,strpos($img_location,'.gif')); 
            $searched = 'Content-Location: ' . $img_location . '.gif'; 
            $img_base64 = substr($thisContent,strpos($thisContent,$searched)+strlen($searched)); 
        } 
        else { 
            $img_location = $img_name; 
            $searched = 'Content-ID: <' . $img_id . '>'; 
            $Content_ID_pos = strpos($thisContent,'Content-ID: <'); 
            $end_content = substr($thisContent,$Content_ID_pos); 
            $end_Content_ID_pos = strpos($end_content,'>'); 
            $img_base64 = substr($end_content,$end_Content_ID_pos + 1); 
        } 
        $aThisImage = array('id'=>$img_id, 'name'=>$img_name, 'location'=>$img_location, 'type'=>'gif', 'base64'=>$img_base64); 
        //print_r($aThisImage); 
        $aImages[] = $aThisImage; 
    } 
    if (strpos($thisContent,'Content-Type: image/jpeg;') !== false) { 
        // base64 jpg picture 
        $begin = strpos($thisContent,'Content-ID: <') + 13; 
        $long = strpos(substr($thisContent,strpos($thisContent,'Content-ID: <') + 13),'>'); 
        $img_id = substr($thisContent,$begin,$long); 
        $img_name = substr($thisContent,strpos($thisContent,'name="')+6,strpos($thisContent,'.jpg"')-strpos($thisContent,'name="')-6); 
        if (strpos($thisContent,'Content-Location: ') !== false) { 
            $img_location = substr($thisContent,strpos($thisContent,'Content-Location: ')+18); 
            $img_location = substr($img_location,0,strpos($img_location,'.jpg')); 
            $searched = 'Content-Location: ' . $img_location . '.jpg'; 
            $img_base64 = substr($thisContent,strpos($thisContent,$searched)+strlen($searched)); 
        } 
        else { 
            $img_location = $img_name; 
            $searched = 'Content-ID: <' . $img_id . '>'; 
            $Content_ID_pos = strpos($thisContent,'Content-ID: <'); 
            $end_content = substr($thisContent,$Content_ID_pos); 
            $end_Content_ID_pos = strpos($end_content,'>'); 
            $img_base64 = substr($end_content,$end_Content_ID_pos + 1); 
        } 
        $aThisImage = array('id'=>$img_id, 'name'=>$img_name, 'location'=>$img_location, 'type'=>'jpg', 'base64'=>$img_base64); 
        $aImages[] = $aThisImage; 
    } 
} 
//print_r($aImages); 
foreach($aImages as $image) { 
    if ($images == 'filed') { 
        // image file creation 
        if (!file_exists(PICTURE_DIRECTORY_PATH."$image[location].$image[type]")) { 
            if (!$handle = fopen (PICTURE_DIRECTORY_PATH."$image[location].$image[type]", "wb")) 
                     die("Cannot open file (".PICTURE_DIRECTORY_PATH."$image[location].$image[type])"); 
            if (!fwrite($handle, base64_decode($image[base64]))) 
                       die("Cannot write into file (".PICTURE_DIRECTORY_PATH."$image[location].$image[type])"); 
            fclose($handle); 
        } 
        $thisHTMLContent = str_replace('cid:'.$image['id'],PICTURE_DIRECTORY_PATH."$image[location].$image[type]",$thisHTMLContent); 
    } 
    else { 
        // images to be created on the fly 
        $imageLocation = urlencode($image[location]); 
        $file = urlencode($eml_file); 
        $thisHTMLContent = str_replace('cid:'.$image['id'],"viewImage.php?imageLocation=$imageLocation&file=$file",$thisHTMLContent); 
    } 

    $thisHTMLContent = preg_replace("/<IMG HEIGHT=(\d*)/i","<img ",$thisHTMLContent); 
    // no base href referring to local file 
    $thisHTMLContent = preg_replace("/href=\"file(.*)\"/i","",$thisHTMLContent); 
} 
echo $thisHTMLContent; 
?> 

提前致谢,希望尽快收到回复。

0 个答案:

没有答案
相关问题