display .msg file in browser using php

后端 未结 3 1438
花落未央
花落未央 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: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[] = '' . $filename . '';
            }
        }
    }
    foreach ($message->getRecipients() as $recipient) {
        $email = $recipient->getEmail();
        $name = $recipient->getName();
        if ($recipient->getType() == 'From') {
            $From[] = ($name) ? '' . $name . '' : '' . $email . '';
        } elseif ($recipient->getType() == 'To') {
            $To[] = ($name) ? '' . $name . '' : '' . $email . '';
        } elseif ($recipient->getType() == 'Cc') {
            $Cc[] = ($name) ? '' . $name . '' : '' . $email . '';
        } elseif ($recipient->getType() == 'Bcc') {
            $Bcc[] = ($name) ? '' . $name . '' : '' . $email . '';
        }
    }
    $data = array(
    'From' => '' . $message->properties['sender_name'] . '',
    '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.
    

提交回复
热议问题