PHP read EML instance, PHP parsing eml, eml parsing into web pages

Source: Internet
Author: User
Tags foreach array base64 count explode flock header trim

PHP reads the EML instance, this example resolves the exported eml file to the body, and can save the attachment to the server. Do not say more directly paste code.

If you think this code is good, please click on the praise. Thank you ~ ~ ~


 -->;

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. ') ';
Title Content $pattern =/subject: (. *?)/ims;
Preg_match ($pattern, $content, $subject _results);
$subject = Getdecodevalue ($subject _results[1]);

echo title:. $subject; Sender: $pattern =/from: .*?< (. *?)
>/ims;
Preg_match ($pattern, $content, $from _results);
$from = $from _results[1];
echo;

echo sender:. $from; //Recipient: $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: (. *?)/ims;
	Preg_match ($pattern, $content, $to _results);
$to = $to _results[1];
} Echo;
echo recipient:;
Print_r ($to);

echo; Body content $pattern =/content-type:multipart/alternative;. *?boundary= (. *?)
/ims;

Preg_match ($pattern, $content, $results); if ($results [1]!=) {$seperator =--. $results [1];}
else{die (boundary match failure);} $spcontent = Explode ($seperator, $content);
$items = Array ();

$keyid = 0;
$email _front_content_array = Array (); foreach ($spcontent as $spkey => $item) {//Matching header encoding Information $pattern =/content-type: ([^;] *?);.
*?charset= (. *?)
  	Content-transfer-encoding: (. *?)/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; A 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 Content:;
echo;
echo $content _html;

echo; Attachment content $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) {//Matching header encoding Information $pattern =/content-type: ([^;] *?);.
*?name= (. *?)
Content-transfer-encoding: (. *?) Content-disposition:attachment;.
	  	*?filename= (. *?)/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));
	  		Save attachment content to server?
	  		When a file name conforms to the specification: when there is a suffix name. 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)) {//For exclusive locking fwrite ($FP, $item _results[6]); Flock ($FP, lock_un);
				Release Lock} 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 attachment:;
	
	echo; Attachment reads 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;
    	The function mkdirs ($dir) {if (!is_dir ($dir)) {if (!mkdirs (DirName ($dir))) {return false;
	    } if (!mkdir ($dir, 0777)) {return false;    } chmod ($dir, 777); 
Return True for directory operation permissions; }?>

There is a picture of the truth:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.