php - 如何将 Exchange Web 服务项目导出到 *.eml 文件? (PHP)

标签 php exchangewebservices php-ews

我正在为 Exchange Web 服务开发一个 Web 界面,它应该能够将邮件项目保存为 eml 格式。我使用 PHP-EWS ( https://github.com/jamesiarmes/php-ews ) 建立与 Exchange Server 的连接。

我知道这样的文件是什么样子,所以我可以下载邮件项目并使用数据生成 eml 模板。

但我发现了这篇文章:Save mail to msg file using EWS API 。 Colin 谈论了一种直接将邮件项目导出到 eml 文件的机制。这在 PHP 中也可能吗?

此外我还发现了另一件事:https://github.com/jamesiarmes/php-ews/wiki/Email:-Set-Extended-MAPI-Properties 。在此示例中,某人生成了 mime 内容并将其设置为新项目。是否可以获取现有项目的 mime 类型(对我来说看起来像 eml 文件)?

感谢您的帮助!

最佳答案

要以 eml 格式保存邮件项目,您必须设置 IncludeMimeContent ItemShape 中的属性为 true GetItem 的元素操作。
通过这样做,您将进入 GetItem response一个MimeContent element :

The MimeContent element contains the native Multipurpose Internet Mail Extensions (MIME) stream of an object that is represented in base64Binary format.

作为示例,请考虑以下代码:

<?php

function __autoload($class_name) {
    $base_path = 'php-ews-master';
    $include_file = $base_path . '/' . str_replace('_', '/', $class_name) . '.php';
    return (file_exists($include_file) ? require_once $include_file : false);
}

/*
**  Adjust these variables before running the script!
*/
$server     = 'your_server';
$username   = 'your_user';
$password   = 'your_password';
$message_id = 'your_message_id';

$ews = new ExchangeWebServices($server, $username, $password);
//print_r($ews);

$request = new EWSType_GetItemType();

$request->ItemShape = new EWSType_ItemResponseShapeType();
$request->ItemShape->BaseShape = EWSType_DefaultShapeNamesType::ALL_PROPERTIES;
$request->ItemShape->IncludeMimeContent = true;

$request->ItemIds = new EWSType_NonEmptyArrayOfBaseItemIdsType();
$request->ItemIds->ItemId = new EWSType_ItemIdType();
$request->ItemIds->ItemId->Id = $message_id; 

$response = $ews->GetItem($request);
//echo '<pre>'.print_r($response, true).'</pre>';

if (($response->ResponseMessages->GetItemResponseMessage->ResponseCode == 'NoError') &&
    ($response->ResponseMessages->GetItemResponseMessage->ResponseClass == 'Success')) {
    file_put_contents("test.eml", base64_decode($response->ResponseMessages->GetItemResponseMessage->Items->Message->MimeContent->_));
}

?>

关于php - 如何将 Exchange Web 服务项目导出到 *.eml 文件? (PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18752563/

相关文章:

php - 袖珍 API : how to obtain the Access token

c# - New-MailContact 的等效 API

c# - 服务响应异常 : The specified object was not found in the store

javascript - 使用 .htaccess 的登录页面

javascript - 如何在 WordPress get_posts() json_encode() 中包含特色图像 URL?

php - Woocommerce - 需要根据邮政编码将电子邮件发送到特定地址

objective-c - 使用 Microsoft Exchange Services 2007 的 Cocoa Mac 应用程序

javascript - PHP EWS 创建回调 token

php - 使用 php-ews 创建定期日历事件