我正在开发一个使用 php-ews 连接到交换服务器的 php 脚本。首先,我提取了收件箱中的所有电子邮件,并进行了一些挖掘以了解连锁电子邮件的工作原理。出于简单的原因,我希望能够将电子邮件链组合在一起。我发现有两封完全不相关的电子邮件,我的 ID 完全相同。ChangeKeys 似乎是唯一的,但链式电子邮件具有唯一的 changekeys,因此我不能基于此将它们组合在一起。关于如何获得“连锁电子邮件”唯一 ID 的任何想法?
以下是“开箱即用”的脚本:
<?php
set_time_limit(120);
require_once('autoload.php');
require_once('credentials.php');
//$ews = new ExchangeWebServices("hostname", "username", "password", ExchangeWebServices::VERSION_2010);
$ews = new ExchangeWebServices($host, $username, $password);
$request = new EWSType_FindItemType();
$request->ItemShape = new EWSType_ItemResponseShapeType();
$request->ItemShape->BaseShape = EWSType_DefaultShapeNamesType::DEFAULT_PROPERTIES;
$request->Traversal = EWSType_ItemQueryTraversalType::SHALLOW;
// Limits the number of items retrieved
$request->IndexedPageItemView = new EWSType_IndexedPageViewType();
$request->IndexedPageItemView->BasePoint = "Beginning";
$request->IndexedPageItemView->Offset = 0; // Item number you want to start at
$request->IndexedPageItemView->MaxEntriesReturned = 1000; // Numer of items to return in total
$request->ParentFolderIds = new EWSType_NonEmptyArrayOfBaseFolderIdsType();
$request->ParentFolderIds->DistinguishedFolderId = new EWSType_DistinguishedFolderIdType();
$request->ParentFolderIds->DistinguishedFolderId->Id = EWSType_DistinguishedFolderIdNameType::INBOX;
// sort order
$request->SortOrder = new EWSType_NonEmptyArrayOfFieldOrdersType();
$request->SortOrder->FieldOrder = array();
$order = new EWSType_FieldOrderType();
// sorts mails so that oldest appear first
// more field uri definitions can be found from types.xsd (look for UnindexedFieldURIType)
$order->FieldURI = '';
@$order->FieldURI->FieldURI = 'item:DateTimeReceived'; // @ symbol stops the creating default object from empty value error
$order->Order = 'Ascending';
$request->SortOrder->FieldOrder[] = $order;
$response = $ews->FindItem($request);
//For Debugging
//die("<pre>" . print_r($response, 1) . "</pre>");
if(!isset($response->ResponseMessages->FindItemResponseMessage->RootFolder))
{
$responseMessage = $response->ResponseMessages->FindItemResponseMessage;
die("<h3 style='text-align: center;'>Email</h3>" . $responseMessage->MessageText . "<br /><br />" . $responseMessage->ResponseCode);
}
else
$totalItems = $response->ResponseMessages->FindItemResponseMessage->RootFolder->TotalItemsInView;
$emails = array();
$rootFolder = $response->ResponseMessages->FindItemResponseMessage->RootFolder;
$messages = $rootFolder->Items->Message;
$lastItemInRange = $rootFolder->IncludesLastItemInRange;
$i = 1; // Counter to multply the max etries retrurned, to create the offset value
while($lastItemInRange != 1) // While the last item in the inbox is strill not in range retrieve the next 1000 messages
{
$limit = $request->IndexedPageItemView->MaxEntriesReturned;
$request->IndexedPageItemView->Offset = $limit * $i;
$response = $ews->FindItem($request);
$rootFolder = $response->ResponseMessages->FindItemResponseMessage->RootFolder;
$messages = array_merge($messages, $rootFolder->Items->Message);
$lastItemInRange = $rootFolder->IncludesLastItemInRange;
echo '<pre>';
print_r( $messages);
echo '</pre>';
$i++;
}
?>
以下是返回的电子邮件示例:
[100] => stdClass Object
(
[ItemId] => stdClass Object
(
[Id] => XXXXXXXXXXXXXXXXX
[ChangeKey] => XXXXXXXXXXXXXXXXXXXX
)
[Subject] => Test Email
[Sensitivity] => Normal
[Size] => 30200
[DateTimeSent] => 2016-02-01T21:43:06Z
[DateTimeCreated] => 2016-02-01T21:43:09Z
[HasAttachments] =>
[From] => stdClass Object
(
[Mailbox] => stdClass Object
(
[Name] => Test User
[EmailAddress] => Test@Test.com
[RoutingType] => SMTP
)
)
[IsRead] => 1
)