4

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

Bizley
  • 17,392
  • 5
  • 49
  • 59
Vinod
  • 67
  • 2
  • 7

2 Answers2

2

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 <john.smith@example.com>
            echo sprintf('%s: %s', $recipient->getType(), (string)$recipient), "\n";
        }
        exit;
user1432181
  • 918
  • 1
  • 9
  • 24
0

You can parse using Aspose_Email_Java_for_PHP Download here

Some example

$mapiMessage=new MapiMessage();
$outlook_message_file = $mapiMessage->fromFile($dataDir . "Message.msg");

Display sender's name

print "Sender Name : " . $outlook_message_file->getSenderName();

Display Subject

print "Subject : " . $outlook_message_file->getSubject();

Display Body

print "Body : " . $outlook_message_file->getBody();

Reference : https://asposeemailjavaphp.codeplex.com/SourceControl/latest#src/aspose/email/ProgrammingOutlook/WorkingWithOutlookMessageFiles/ParseOutlookMessageFile.php

Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71