使用php在浏览器中显示.msg文件

时间:2013-05-02 05:48:22

标签: php

我在php应用程序中附加了outlook msg文件。我将该文件存储在sql server数据库中。

现在我想打开并在浏览器中显示它。

我试过这段代码:

  if($ext=="msg")
  {
    header('ContentType : application/octet-stream');
    header('Content-Disposition: attachment; filename='. basename($filename));
    echo base64_decode($file);
  }

$ filename和$ file来自数据库。

它在IE浏览器和Chrome中打开了msg文件,但它没有从firefox开放。

有没有办法让它在所有浏览器中运行?

或者我错了某处或浏览器中有任何设置?

3 个答案:

答案 0 :(得分:1)

我有一个几乎相似的情况,并能够解决它。 包括下面的标题,它应该可以正常工作。

此致

header("Pragma: public");
header("Expires: 0");
header('Content-Encoding: UTF-8');
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: application/vnd.ms-outlook;charset=UTF-8"); 
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=test.msg");
header("Content-Transfer-Encoding: binary ");

答案 1 :(得分:0)

if($ext=="msg")
  {
    header("Content-Type: text/Calendar");
    header('Content-Disposition: inline; filename='. basename($filename));
    echo base64_decode($file);
  }

答案 2 :(得分:0)

#composer require hfig/mapi
# needed if you want to convert to MIME format
#composer require swiftmailer/swiftmailer

require 'vendor/autoload.php';

use Hfig\MAPI;
use Hfig\MAPI\OLE\Pear;

$decodelocation = '/var/html/tmp/';
$baseurl = 'http://example.com/tmp/';
$uniquefolder = uniqid();
// message parsing and file IO are kept separate
$messageFactory = new MAPI\MapiMessageFactory();
$documentFactory = new Pear\DocumentFactory();

$ole = $documentFactory->createFromFile('source-file.msg');
$message = $messageFactory->parseMessage($ole);

$html = preg_replace_callback($this->regex, "utf8replacer", $message->getBodyHTML());

if (count($message->getAttachments()) > 0) {
foreach ($message->getAttachments() as $attach) {
    $filename = $attach->getFilename();
    $temploc = $decodelocation . '/' . $uniquefolder . '/' . $filename;
    $fileurl = $baseurl . '/' . $uniquefolder . '/' . $filename;
    $replace_string = get_string_between($html, 'cid:' . $filename, '"');
    if ($replace_string) {
        file_put_contents($temploc, $attach->getData());
        $html = str_replace('cid:' . $filename . $replace_string, base_url($temploc), $html);
    } else {
        $geturl = array(
            'filename' => $filename,
            'path' => cencode($temploc),
        );
        $attachments[] = '<a target="_blank" href="' . $fileurl . '">' . $filename . '</a>';
        }
    }
}
foreach ($message->getRecipients() as $recipient) {
    $email = $recipient->getEmail();
    $name = $recipient->getName();
    if ($recipient->getType() == 'From') {
        $From[] = ($name) ? '<a href="mailto:' . $email . '">' . $name . '</a>' : '<a href="mailto:' . $email . '">' . $email . '</a>';
    } elseif ($recipient->getType() == 'To') {
        $To[] = ($name) ? '<a href="mailto:' . $email . '">' . $name . '</a>' : '<a href="mailto:' . $email . '">' . $email . '</a>';
    } elseif ($recipient->getType() == 'Cc') {
        $Cc[] = ($name) ? '<a href="mailto:' . $email . '">' . $name . '</a>' : '<a href="mailto:' . $email . '">' . $email . '</a>';
    } elseif ($recipient->getType() == 'Bcc') {
        $Bcc[] = ($name) ? '<a href="mailto:' . $email . '">' . $name . '</a>' : '<a href="mailto:' . $email . '">' . $email . '</a>';
    }
}
$data = array(
'From' => '<a href="mailto:' . $message->properties['sender_email_address'] . '">' . $message->properties['sender_name'] . '</a>',
'To' => ($To) ? implode('; ', $To) : '',
'Cc' => ($Cc) ? implode('; ', $Cc) : '',
'Bcc' => ($Bcc) ? implode('; ', $Bcc) : '',
'Subject' => $message->properties['subject'],
'hasAttachment' => $message->properties['hasattach'],
'attachments' => ($attachments) ? implode('; ', $attachments) : false,
'html' => $html,
);
#customize your html page using data array. It is take long time for greater than 5 MB.