function __construct($section, $info, $mime_filename) { $this->mime_filename = $mime_filename; $this->section = $section; $this->info = $info; $this->setTempFile(ParserFile::makeTempFilename(), @$info['content-type']); $this->fp = fopen($this->getTempFile(), 'wb'); if ($this->fp && !empty($this->section) && !empty($this->mime_filename)) { mailparse_msg_extract_part_file($this->section, $this->mime_filename, array($this, "writeCallback")); } @fclose($this->fp); }
/** * Enter description here... * * @param object $mime * @return CerberusParserMessage */ public static function parseMime($mime, $full_filename) { $struct = mailparse_msg_get_structure($mime); $msginfo = mailparse_msg_get_part_data($mime); $message = new CerberusParserMessage(); @($message->encoding = $msginfo['content-charset']); @($message->body_encoding = $message->encoding); // default // Decode headers @($message->headers = $msginfo['headers']); if (is_array($message->headers)) { foreach ($message->headers as $header_name => $header_val) { if (is_array($header_val)) { foreach ($header_val as $idx => $val) { $message->headers[$header_name][$idx] = self::fixQuotePrintableString($val); } } else { $message->headers[$header_name] = self::fixQuotePrintableString($header_val); } } } $settings = DevblocksPlatform::getPluginSettingsService(); $is_attachments_enabled = $settings->get('cerberusweb.core', CerberusSettings::ATTACHMENTS_ENABLED, 1); $attachments_max_size = $settings->get('cerberusweb.core', CerberusSettings::ATTACHMENTS_MAX_SIZE, 10); foreach ($struct as $st) { // echo "PART $st...<br>\r\n"; $section = mailparse_msg_get_part($mime, $st); $info = mailparse_msg_get_part_data($section); // handle parts that shouldn't have a content-name, don't handle twice $handled = 0; if (empty($info['content-name'])) { if ($info['content-type'] == 'text/plain') { $text = mailparse_msg_extract_part_file($section, $full_filename, NULL); if (isset($info['content-charset']) && !empty($info['content-charset'])) { $message->body_encoding = $info['content-charset']; if (@mb_check_encoding($text, $info['content-charset'])) { $text = mb_convert_encoding($text, LANG_CHARSET_CODE, $info['content-charset']); } else { $text = mb_convert_encoding($text, LANG_CHARSET_CODE); } } @($message->body .= $text); unset($text); $handled = 1; } elseif ($info['content-type'] == 'text/html') { @($text = mailparse_msg_extract_part_file($section, $full_filename, NULL)); if (isset($info['content-charset']) && !empty($info['content-charset'])) { if (@mb_check_encoding($text, $info['content-charset'])) { $text = mb_convert_encoding($text, LANG_CHARSET_CODE, $info['content-charset']); } else { $text = mb_convert_encoding($text, LANG_CHARSET_CODE); } } $message->htmlbody .= $text; unset($text); // Add the html part as an attachment // [TODO] Make attaching the HTML part an optional config option (off by default) $tmpname = ParserFile::makeTempFilename(); $html_attach = new ParserFile(); $html_attach->setTempFile($tmpname, 'text/html'); @file_put_contents($tmpname, $message->htmlbody); $html_attach->file_size = filesize($tmpname); $message->files["original_message.html"] = $html_attach; unset($html_attach); $handled = 1; } elseif ($info['content-type'] == 'message/rfc822') { @($message_content = mailparse_msg_extract_part_file($section, $full_filename, NULL)); $message_counter = empty($message_counter) ? 1 : $message_counter + 1; $tmpname = ParserFile::makeTempFilename(); $html_attach = new ParserFile(); $html_attach->setTempFile($tmpname, 'message/rfc822'); @file_put_contents($tmpname, $message_content); $html_attach->file_size = filesize($tmpname); $message->files['inline' . $message_counter . '.msg'] = $html_attach; unset($html_attach); $handled = 1; } } // whether or not it has a content-name, we need to add it as an attachment (if not already handled) if ($handled == 0) { if (false === strpos(strtolower($info['content-type']), 'multipart')) { if (!$is_attachments_enabled) { break; // skip attachment } $attach = new ParseCronFileBuffer($section, $info, $full_filename); // [TODO] This could be more efficient by not even saving in the first place above: // Make sure our attachment is under the max preferred size if (filesize($attach->tmpname) > $attachments_max_size * 1024000) { @unlink($attach->tmpname); break; } // if un-named, call it "unnamed message part" if (!isset($info['content-name']) || isset($info['content-name']) && empty($info['content-name'])) { // or blank $info['content-name'] = 'unnamed_message_part'; } // filenames can be quoted-printable strings, too... $info['content-name'] = self::fixQuotePrintableString($info['content-name']); // content-name is not necessarily unique... if (isset($message->files[$info['content-name']])) { $j = 1; while (isset($message->files[$info['content-name'] . '(' . $j . ')'])) { $j++; } $info['content-name'] = $info['content-name'] . '(' . $j . ')'; } $message->files[$info['content-name']] = $attach; } } } // generate the plaintext part (if necessary) if (empty($message->body) && !empty($message->htmlbody)) { $message->body = CerberusApplication::stripHTML($message->htmlbody); } return $message; }
sendReport($sender, $errors, $notices); unlink($tmpFile); exit; } // Iterate structure $body = ""; $attachments = array(); foreach ($structure as $structurePart) { $partFile = "{$tmpFile}-{$structurePart}"; $section = mailparse_msg_get_part($mp, $structurePart); $info = mailparse_msg_get_part_data($section); if (!in_array($info['content-type'], array('multipart/mixed', 'multipart/alternative'))) { // Get contents $part = mailparse_msg_get_part($mp, $structurePart); // Use NULL as callback to return as string and not to STDOUT. $contents = mailparse_msg_extract_part_file($part, $tmpFile, NULL); if (isset($info['disposition-filename'])) { // Attachment, store $name = iconv_mime_decode($info['disposition-filename']); $id = Storage::save($name, $contents); $attachments[] = $id; } else { if (!$body && $info['content-type'] == 'text/plain') { // Body part $body = trim(preg_replace('/-- [\\r\\n]+.*/s', '', $contents)); } } } } // Delete tmp file unlink($tmpFile);
$mime = mailparse_msg_create(); mailparse_msg_parse($mime, $text); echo "Extract to output\n"; mailparse_msg_extract_part_file($mime, $fp); echo "Extract and return as string\n"; $result = mailparse_msg_extract_part_file($mime, $fp, null); echo "-->\n"; echo $result; echo "\nExtract to open file\n"; $fpdest = tmpfile(); mailparse_msg_extract_part_file($mime, $fp, $fpdest); echo "\nrewinding\n"; rewind($fpdest); fpassthru($fpdest); echo "\nExtract via user function\n"; mailparse_msg_extract_part_file($mime, $fp); echo "\nExtract whole part to output\n"; mailparse_msg_extract_whole_part_file($mime, $fp); echo "\nExtract part from string to output\n"; mailparse_msg_extract_part($mime, $text); fclose($fpdest); fclose($fp); $output = ob_get_contents(); ob_end_clean(); VS($output, "Extract to output\n" . "hello, this is some text hello.\n" . "blah blah blah.\n" . "Extract and return as string\n" . "-->\n" . "hello, this is some text hello.\n" . "blah blah blah.\n" . "\n" . "Extract to open file\n" . "\n" . "rewinding\n" . "hello, this is some text hello.\n" . "blah blah blah.\n" . "\n" . "Extract via user function\n" . "hello, this is some text hello.\n" . "blah blah blah.\n" . "\n" . "Extract whole part to output\n" . "To: fred@bloggs.com\n" . "Mime-Version: 1.0\n" . "Content-Type: text/plain\n" . "Subject: A simple MIME message\n" . "\n" . "hello, this is some text hello.\n" . "blah blah blah.\n" . "\n" . "Extract part from string to output\n" . "hello, this is some text hello.\n" . "blah blah blah.\n"); ////////////////////////////////////////////////////////////////////// $msg = "Received: from mail pickup service by hotmail.com with Microsoft\n" . "SMTPSVC;\n" . "Sat, 18 Feb 2006 22:58:14 -0800\n" . "Received: from 66.178.40.49 by BAY116-DAV8.phx.gbl with DAV;\n" . "Sun, 19 Feb 2006 06:58:13 +0000\n" . "\n" . "test"; $mail = mailparse_msg_create(); mailparse_msg_parse($mail, $msg); $arr = mailparse_msg_get_structure($mail); foreach ($arr as $first => $second) {
/** * @param mixed $filename * @param callable $callback [optional] * * @return string */ public function extractPartFile($filename, callable $callback = null) { return mailparse_msg_extract_part_file($this->resource, $filename, $callback); }
/* print a choice of sections */ foreach ($struct as $st) { echo "<tr>\n"; echo "<td><a href=\"{$PHP_SELF}?showpart={$st}\">{$st}</a></td>\n"; /* get a handle on the message resource for a subsection */ $section = mailparse_msg_get_part($mime, $st); /* get content-type, encoding and header information for that section */ $info = mailparse_msg_get_part_data($section); print_r($info); echo "\n"; echo "<td>" . $info["content-type"] . "</td>\n"; echo "<td>" . $info["content-disposition"] . "</td>\n"; echo "<td>" . $info["disposition-filename"] . "</td>\n"; echo "<td>" . $info["charset"] . "</td>\n"; echo "</tr>"; } echo "</table>"; /* if we were called to display a part, do so now */ if ($showpart) { /* get a handle on the message resource for the desired part */ $sec = mailparse_msg_get_part($mime, $showpart); echo "<table border=1><tr><th>Section {$showpart}</th></tr><tr><td>"; ob_start(); /* extract the part from the message file and dump it to the output buffer * */ mailparse_msg_extract_part_file($sec, $filename); $contents = ob_get_contents(); ob_end_clean(); /* quote the message for safe display in a browser */ echo nl2br(htmlentities($contents)) . "</td></tr></table>"; }