$tmpdir, "AttachmentsDir" => $tmpdir, ); if ( array_key_exists($varname,$config) ) { return $config[$varname]; } return FALSE; } function parser($eml_file='') { $tempdir = get_config('TempDir'); if ( ! is_dir($tempdir) ) { die('The temporary directory "'.$tempdir.'" doesn\'t exist.'); } // get a unique temporary file name $tmpfilepath = tempnam($tempdir, strval(mt_rand(1000,9999))); if (defined('HESK_IMAP')) { global $imap, $email_number; @file_put_contents($tmpfilepath, imap_fetchbody($imap, $email_number, "")); } else { // read the mail that is forwarded to the script // then save the mail to a temporary file save_forward_mail($tmpfilepath, $eml_file); } if(file_exists($tmpfilepath) === FALSE) { die('Failed to save the mail as '.$tmpfilepath.'.'); } $ret = analyze($tmpfilepath,$tempdir); //die (print_r($ret)); return $ret; } function analyze($tmpfilepath,$tempdir) { $mime=new mime_parser_class; $mime->mbox = 0; $mime->decode_bodies = 1; $mime->ignore_syntax_errors = 1; $mime->track_lines = 0; $parameters=array( 'File'=>$tmpfilepath, 'SaveBody'=>$tempdir, ); /* only process the first email */ if($mime->Decode($parameters, $decoded)) { if($mime->decode_bodies) { if($mime->Analyze($decoded[0], $results)) { #echo "MIME:\n\n"; #print_r($results); #echo "\nEND MIME\n\n"; return process_results($results,$tempdir) ; } else { echo 'MIME message analyse error: '.$mime->error."\n"; } } } return False; } function process_addrname_pairs($email_info) { $result = array(); foreach($email_info as $info) { $address = ""; $name = ""; $encoding = ""; if ( array_key_exists("address", $info) ) { $address = $info["address"]; } if ( array_key_exists("name", $info) ) { $name = $info["name"]; } if ( array_key_exists("encoding", $info) ) { $encoding = $info["encoding"]; } $result[] = array("address"=>$address, "name"=>$name, "encoding"=>$encoding); } return $result; } function process_attachments($attachments) { $result = array(); foreach($attachments as $key => $info) { $orig_name = ""; $size = 0; $stored_name = ""; $type = ""; if ( array_key_exists("Type", $info) ) { $type = $info["Type"]; } if ( array_key_exists("FileName", $info) ) { $orig_name = $info["FileName"]; } elseif ($type == 'message') { $orig_name = ($key + 1) . ".msg"; } if ( ! strlen($orig_name)) { continue; } if ( array_key_exists("Data", $info) ) { $data = $info["Data"]; $size = strlen($data); if ($size == 0) { continue; } $attachsdir = get_config("AttachmentsDir"); if ( ! is_dir($attachsdir) ) { die('The attachments directory "'.$attachsdir.'" doesn\'t exist.'); } $stored_name = save_attachment($attachsdir,getFileExtension($orig_name),$data); } else { $stored_name = $info['DataFile']; $size = filesize($stored_name); } $result[] = array("orig_name"=>$orig_name,"size"=>$size,"stored_name"=>$stored_name,"type"=>$type); } return $result; } /* save an attachment file into the predefined directory. return stored name */ function save_attachment($dir,$extension,$data) { $dir = rtrim($dir,"/\\"); /* " */ $path = ""; $stored_name = ""; do { $stored_name = date("YmdHis")."_".strval(mt_rand()).".".$extension; $path = $dir . "/" . $stored_name; } while(file_exists($path)); $fp = fopen($path,"w"); if($fp === FALSE) { die("Cannot save file ".$path." ."); } fwrite($fp,$data); fclose($fp); return $stored_name; } function process_results($result,$tempdir) { global $hesk_settings; $r = array(); // from address and name $r["from"] = process_addrname_pairs($result["From"]); // to address and name $r["to"] = process_addrname_pairs($result["To"]); // cc address and name if( array_key_exists("Cc", $result) ) { $r["cc"] = process_addrname_pairs($result["Cc"]); } else { $r["cc"] = array(); } // bcc address and name if( array_key_exists("Bcc", $result) ) { $r["bcc"] = process_addrname_pairs($result["Bcc"]); } else { $r["bcc"] = array(); } // reply-to address and name if( array_key_exists("Reply-to", $result) ) { $r["reply-to"] = process_addrname_pairs($result["Reply-to"]); } else { $r["reply-to"] = array(); } // subject and subject encoding $r["subject"] = $result["Subject"]; $r["subject_encoding"] = isset($result["SubjectEncoding"]) ? strtoupper($result["SubjectEncoding"]) : ""; // Message encoding $r["encoding"] = isset($result["Encoding"]) ? strtoupper($result["Encoding"]) : ""; // If message is saved in a file get it from the file if ( ! isset($result["Data"]) ) { $result["Data"] = ( isset($result["DataFile"]) && ! ( isset($result["FileDisposition"]) && $result["FileDisposition"] == "attachment") ) ? file_get_contents($result["DataFile"]) : ""; } // Convert to UTF-8 before processing further if ($r["encoding"] != "" && $r["encoding"] != 'UTF-8') { $result["Data"] = $result["Data"] == "" ? "" : (function_exists('iconv') ? iconv($r["encoding"], 'UTF-8', $result["Data"]) : utf8_encode($result["Data"])); $r["encoding"] = 'UTF-8'; } // the message shall be converted to text if it is in html if ( $result["Type"] === "html" ) { $r["message"] = convert_html_to_text($result["Data"]); } else { $r["message"] = $result["Data"]; } // Fix for inline attachments if (isset($result["FileDisposition"]) && ($result["FileDisposition"] == "attachment" || $result["FileDisposition"] == "inline")) { $r["message"] = ""; } // Message attachments $r["attachments"] = array(); if ($hesk_settings['attachments']) { // Attachment with no message if ( isset($result["FileDisposition"]) && ($result["FileDisposition"] == "attachment" || $result["FileDisposition"] == "inline")) { $tmp = array(); $tmp[0]['FileDisposition'] = "attachment"; if ( isset($result["Type"]) ) { $tmp[0]['Type'] = $result["Type"]; } if ( isset($result["SubType"]) ) { $tmp[0]['SubType'] = $result["SubType"]; } if ( isset($result["Description"]) ) { $tmp[0]['Description'] = $result["Description"]; } if ( isset($result["DataFile"]) ) { $tmp[0]['DataFile'] = $result["DataFile"]; } if ( isset($result["FileName"]) ) { $tmp[0]['FileName'] = $result["FileName"]; } $r["attachments"] = array_merge($r["attachments"], process_attachments($tmp) ); } // File attachments if ( array_key_exists("Attachments", $result) ) { $r["attachments"] = array_merge($r["attachments"], process_attachments($result["Attachments"]) ); } // Save embedded files (for example embedded images) if ($hesk_settings['save_embedded'] && array_key_exists("Related", $result) ) { $r["attachments"] = array_merge($r["attachments"], process_attachments($result["Related"]) ); } } // Name of the temporary folder $r["tempdir"] = $tempdir; return $r; } /* save the forwarded mail to a temporary file no return value */ function save_forward_mail($tmpfilepath, $eml_file) { // create a temporary file $tmpfp = fopen($tmpfilepath,"w"); // Just a line used for testing // $eml_file = 'C:\\Users\\4N\\Desktop\\test.txt'; // open the stdin as a file handle or input file $fp = $eml_file ? fopen($eml_file, "r") : fopen("php://stdin", "r"); $fileContent = @stream_get_contents($fp); fwrite($tmpfp, $fileContent); fclose($tmpfp); } #test(); function test() { $results = parser(); print_r($results); exit(); // from address and name echo "from :\n"; echo $results["from"][0]["address"]."\n"; echo $results["from"][0]["name"]."\n"; echo "\nto :\n"; foreach( $results["to"] as $to ){ echo $to["address"]."\n"; echo $to["name"]."\n"; } echo "\nreply-to :\n"; foreach( $results["reply-to"] as $to ){ echo $to["address"]."\n"; echo $to["name"]."\n"; } echo "\ncc :\n"; foreach( $results["cc"] as $to ){ echo $to["address"]."\n"; echo $to["name"]."\n"; } echo "\nbcc :\n"; foreach( $results["bcc"] as $to ){ echo $to["address"]."\n"; echo $to["name"]."\n"; } echo "\nsubject :\n"; echo $results["subject"]."\n"; echo "\nmessage :\n"; echo $results["message"]."\n"; echo "\nattachments :\n"; foreach( $results["attachments"] as $attach ){ echo $attach["orig_name"]."\n"; echo $attach["size"]."\n"; echo $attach["stored_name"]."\n"; echo $attach["type"]."\n"; } } function deleteAll($directory, $empty = false) { if(substr($directory,-1) == "/") { $directory = substr($directory,0,-1); } if(!file_exists($directory) || !is_dir($directory)) { return false; } elseif( ! is_readable($directory)) { return false; } else { $directoryHandle = opendir($directory); while ($contents = readdir($directoryHandle)) { if($contents != '.' && $contents != '..') { $path = $directory . "/" . $contents; if(is_dir($path)) { deleteAll($path); } else { @unlink($path); } } } closedir($directoryHandle); if($empty == false) { if(!rmdir($directory)) { return false; } } return true; } } function convert_html_to_text($data) { $html2text = new html2text($data); $h2t = & ref_new($html2text); // Simply call the get_text() method for the class to convert // the HTML to the plain text. Store it into the variable. $text = $h2t->get_text(); return $text; } class html2text { /** * Contains the HTML content to convert. * * @var string $html * @access public */ var $html; /** * Contains the converted, formatted text. * * @var string $text * @access public */ var $text; /** * Maximum width of the formatted text, in columns. * * Set this value to 0 (or less) to ignore word wrapping * and not constrain text to a fixed-width column. * * @var integer $width * @access public */ var $width = 70; /** * List of preg* regular expression patterns to search for, * used in conjunction with $replace. * * @var array $search * @access public * @see $replace */ var $search = array( "/\r/", // Non-legal carriage return "/[\n\t]+/", // Newlines and tabs '/[ ]{2,}/', // Runs of spaces, pre-handling '/]*>.*?<\/script>/i', //