display .msg file in browser using php

后端 未结 3 1439
花落未央
花落未央 2021-01-16 06:02

I have attached outlook msg file in php application. I am storing that file in sql server database.

Now i want to open and display it from browser.

I tried

相关标签:
3条回答
  • 2021-01-16 06:42

    I had an almost similar situation and was able to solve it. Include the headers below and it should work just fine.

    Regards

    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 ");
    
    0 讨论(0)
  • 2021-01-16 06:47
    if($ext=="msg")
      {
        header("Content-Type: text/Calendar");
        header('Content-Disposition: inline; filename='. basename($filename));
        echo base64_decode($file);
      }
    
    0 讨论(0)
  • 2021-01-16 06:53

    #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.
    
    0 讨论(0)
提交回复
热议问题