私は、php-ews を使用して Exchange サーバーに接続する php スクリプトに取り組んでいます。手始めに、受信トレイにあるすべてのメールを取り出し、チェーンメールがどのように機能するかを調べました。簡単にするために、メール チェーンをグループ化できるようにしたいと考えています。私が見つけたのは、完全に無関係な 2 つのメールで、まったく同じ ID を持っています。ChangeKey は一意のように見えますが、チェーン メールには一意の changekey があるため、それに基づいてそれらをグループ化することはできません。「チェーンメール」の一意の 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
)