用于读取和操作 Microsoft Outlook .msg 消息(MAPI 文档)的纯 PHP 库

dev-master 2023-01-11 13:22 UTC

This package is auto-updated.

Last update: 2024-09-11 16:57:49 UTC


README

简介

Hfig/MAPI 是一个 PHP 7 库,用于读取和使用 Microsoft Outlook/Exchange 格式的电子邮件消息(.msg 文件,即 MAPI 文档)。

该库可以解析 MAPI 文档,并程序化提取文档的属性和流。

可以通过利用 Swiftmailer/Switfmailer 库将消息转换为 RFC822(MIME)格式。

该库本质上是从 Ruby 语言迁移到 PHP 的 aquasync/ruby-msg 库。一些可疑的 PHP 架构决策来自迁移 Ruby 构造。一些糟糕但功能性的代码来自 Ruby 库的直接迁移。

ruby-msg 相比,此库

  • 不实现消息转换的命令行入口点
  • 仅处理 .msg 文件(或 .msg 文件数据的 PHP 流)中的 MAPI 文档
  • 不实现将 RTF 格式的消息主体转换为纯文本或 HTML
  • 对解码 MAPI 文档属性有更好的支持
  • 生成更忠实的 MAPI 文档 MIME 转换

安装

使用 composer 安装

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;

// 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);

// 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";
}

转换为 MIME

require 'vendor/autoload.php';

use Hfig\MAPI;
use Hfig\MAPI\OLE\Pear;
use Hfig\MAPI\Mime\Swiftmailer;

$messageFactory = new MAPI\MapiMessageFactory(new Swiftmailer\Factory());
$documentFactory = new Pear\DocumentFactory(); 

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

// returns a \Swift_Message object representaiton of the email
$mime = $message->toMime();

// or write it to file
$fd = fopen('dest-file.eml', 'w');
$message->copyMimeToStream($fd);

属性名称

Microsoft 在 https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2007/cc815517(v%3doffice.12) 以一种难以理解的方式记录了 MAPI 属性名称。

此库中可用于属性名称的列表包含在 MAPI/Schema/MapiFieldsMessage.yaml 文件中。

按照与 ruby-msg 库相同的约定,消息属性转换为 漂亮 的名称

  • PR_DISPLAY_NAME => display_name
  • PR_ATTACH_FILENAME => attach_filename

关于 MAPI 文档

MAPI 文档是 Microsoft OLE 结构化存储数据库,类似于旧 .doc.xls.ppt 文件。它们由类似于虚拟 FAT 文件系统的 4K 块流组成的内部目录结构。出于经济原因,每个结构化存储数据库都包含一个根流,其中包含 64 字节块,这些块又存储小数据块。有关更多信息,请参阅 Microsoft 的文档

PEAR 库 OLE 可以读取这些数据库文件。然而,此 PEAR 库已经过时,不符合任何现代编码标准,因此它完全与该库的消息解析代码分离。希望有一天可以替换它。

替代方案

在您的服务器上安装 Kopano Core 项目将使 ext-mapi 可用,这是一个 PHP 扩展,它实现了对 MAPI Win32 API 的访问。

另请参阅