要以 eml 格式保存邮件项目,您必须在GetItem操作的ItemShape元素中将 IncludeMimeContent属性设置为true 。
通过这样做,您将在GetItem 响应中获得一个MimeContent 元素:
MimeContent 元素包含以 base64Binary 格式表示的对象的本机多用途 Internet 邮件扩展 (MIME) 流。
例如,考虑以下代码:
<?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->_));
}
?>