Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read .msg file in php

Tags:

php

msg

I want to read Outlook .msg email with PHP language and I don't know how to read it with simple file read function.

I have enabled Mailparse extension in my Linux system and by using it I can read the .eml files correctly but not .msg.

Could you point me to correct code or library I need to use?

Thanks in advance

like image 961
Vinod Avatar asked Nov 19 '22 06:11

Vinod


1 Answers

https://github.com/hfig/MAPI will do it.

        require 'autoload.php';


        $newfn= "outlook.msg";
        // message parsing and file IO are kept separate
        $messageFactory = new Hfig\MAPI\MapiMessageFactory();
        $documentFactory = new Hfig\MAPI\OLE\Pear\DocumentFactory();

        $ole = $documentFactory->createFromFile($newfn);
        $message = $messageFactory->parseMessage($ole);

        // raw properties are available from the "properties" member
        echo $message->properties['subject'], "\n";

        // some properties have helper methods
        echo $message->getSender(), "\n";
        echo $message->getBody(), "\n";

        // recipients and attachments are composed objects
        foreach ($message->getRecipients() as $recipient) {
            // eg "To: John Smith <[email protected]>
            echo sprintf('%s: %s', $recipient->getType(), (string)$recipient), "\n";
        }
        exit;
like image 72
user1432181 Avatar answered Jan 05 '23 19:01

user1432181