1

我正在使用 php-ews 读取 Exchange 2010 上超过 1000 个邮箱的内容,我有一个使用 EWSType_FindItemType 列出所有电子邮件的功能,以获取所有 ID 密钥并将它们存储到我拥有的数组中然后使用该数组的 foreach 循环调用另一个函数来获取该电子邮件的内容,例如正文和来自电子邮件地址。

我在一个小得多的邮箱上对此进行了测试,它在两种不同的测试中运行良好,一种在同一个 Exchange 上,另一种在 Microsoft 365 上使用 Exchange 2013。

但是,当我从上次调用的键数组中收到大约 300 封电子邮件时,我不断收到此错误:

SoapFault exception: [Client] looks like we got no XML document in E:\Development\ExchangeInt\php-ews\ExchangeWebServices.php:17
Stack trace:
#0 E:\Development\ExchangeInt\php-ews\ExchangeWebServices.php(17): SoapClient->__call('GetItem', Array)
#1 E:\Development\ExchangeInt\php-ews\ExchangeWebServices.php(17): NTLMSoapClient_Exchange->GetItem(Object(EWSType_GetItemType))
#2 E:\Development\ExchangeInt\php-ews\ExchangeWebServices.php(694): ExchangeWebServices->__doRequest(Object(EWSType_GetItemType))
#3 E:\Development\ExchangeInt\mail.php(405): ExchangeWebServices->GetItem(Object(EWSType_GetItemType))
#4 E:\Development\ExchangeInt\mail.php(341): Message(Object(Folder), Object(User), 'AAMkAGEzOGJmNjg...')
#5 E:\Development\ExchangeInt\mail.php(800): listFolder(Object(Folder), Object(User))
#6 E:\Development\ExchangeInt\mail.php(99): getFolders(Object(User))
#7 E:\Development\ExchangeInt\exchange.php(38): User->mailboxes()
#8 {main}

我真的不知道该怎么办,我一直在转圈,一些帮助会很有帮助。多谢你们

我的代码:列出电子邮件并获取 ID 密钥 -

$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 = 500; // 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->EX->FindItem($request);

//For Debugging
//die("<pre>" . print_r($arr, 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;
}

$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 still not in range retrieve the next 1000 messages
{
    $limit = $request->IndexedPageItemView->MaxEntriesReturned;
    $request->IndexedPageItemView->Offset = $limit * $i;
    $response = $ews->EX->FindItem($request);

    $rootFolder = $response->ResponseMessages->FindItemResponseMessage->RootFolder;
    $messages = array_merge($messages, $rootFolder->Items->Message);
    $lastItemInRange = $rootFolder->IncludesLastItemInRange;

    $i++;
}

foreach($messages as $msg)
{
    $arrID;
    $arrID = $msg->ItemId->Id; 
    echo $arrID . "<br>";
    Message($user, $ews, $arrID);
    //echo print_r($msg);
}

die();

消息功能:

function Message($user,$ews,$key) {
    $email = new Email;
    $email->ID = $key;

    // Build the request for the parts.
    $request = new EWSType_GetItemType();
    $request->ItemShape = new EWSType_ItemResponseShapeType();
    $request->ItemShape->BaseShape = EWSType_DefaultShapeNamesType::DEFAULT_PROPERTIES;

    // You can get the body as HTML, text or "best".
    $request->ItemShape->BodyType = EWSType_BodyTypeResponseType::TEXT;

    // Add the body property.
    $body_property = new EWSType_PathToUnindexedFieldType();
    $body_property->FieldURI = 'item:Body';
    $request->ItemShape->AdditionalProperties = new EWSType_NonEmptyArrayOfPathsToElementType();
    $request->ItemShape->AdditionalProperties->FieldURI = array($body_property);

    $request->ItemIds = new EWSType_NonEmptyArrayOfBaseItemIdsType();
    $request->ItemIds->ItemId = array();

    // Add the message to the request.
    $message_item = new EWSType_ItemIdType();
    $message_item->Id = $key;
    $request->ItemIds->ItemId[] = $message_item;

    $response = $ews->EX->GetItem($request);  <-- Breaks Here I Think

    echo print_r($response);

    return;
}
4

0 回答 0