php-ews和电子邮件ID

时间:2016-02-01 22:10:39

标签: php email exchange-server php-ews

我正在使用php-ews连接到交换服务器的php脚本。 对于初学者,我在我的收件箱中提取了所有电子邮件,并进行了一些挖掘,以了解链式电子邮件的工作原理。出于简单原因,我希望能够将电子邮件链组合在一起。我发现的是2封完全不相关的电子邮件,我的确具有相同的ID。 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
        )

1 个答案:

答案 0 :(得分:1)

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

作为旁注,我建议不要使用那个版本的php-ews。它已经过时,没有维护,也没有关注任何PSR。我维护自己的分叉garethp/php-ews。您应该发现整体工作更容易

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