hfig / mapi
纯PHP库,用于读取和操作Microsoft Outlook .msg消息(MAPI文档)
Requires
- php: ^7.1||^8.0
- ext-bcmath: *
- ext-mbstring: *
- pear/ole: ^1.0
- psr/log: ^1.0||^2.0||^3.0
- ramsey/uuid: ^3.8||^4.0
- symfony/yaml: ^4.1||^5.0||^6.0||^7.0
Requires (Dev)
- phpunit/phpunit: ^8.3
- swiftmailer/swiftmailer: ^6.1
Suggests
- swiftmailer/swiftmailer: Conversion to MIME (eml file) message format
README
简介
Hfig/MAPI
是一个PHP库,用于读取和操作Microsoft Outlook/Exchange格式的电子邮件消息(.msg
文件,又称MAPI文档)。
该库可以解析MAPI文档,并程序化地提取文档的属性和流。
可以使用 Swiftmailer/Switfmailer
库将消息转换为 RFC822
(MIME)格式。
该库基本上是将Ruby语言的 aquasync/ruby-msg
库移植到PHP。一些有问题的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的访问。
另请参阅
Email::Outlook::Message
(Perl)aquasync/ruby-msg
(Ruby)jtnef
(Java)