1

我正在开发一个使用 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
        )
4

1 回答 1

1

您没有从 mailItem 获得更多数据的原因与您的request->ItemShape->BaseShape. 您已将其设置为默认属性,其中不包括您想要的,即conversationId. 将其设置为 AllProperties 将返回 aconversationId作为 MessageItem 的一部分,这将是一个ItemIdType,因此它将具有一个 id 和一个 changeKey。

作为旁注,我建议不要使用该版本的 php-ews。它已过时、未维护且不遵循任何 PSR。我维护自己的叉子,garethp/php-ews. 你应该会发现它更容易作为一个整体工作

编辑:一些快速的信息。changeKey 只是您想要用来跟踪对象状态的东西。如果一个对象改变了,它的changeKey也会改变。这对于了解您拥有的数据是否已过期非常有用。

于 2016-02-02T08:57:22.253 回答