PHP-EWS在多个附件上失败

我用James Armes’s PHP-EWS library.

以下代码适用于单个附件,但与多个文件失败.

<?PHP
$msgRequest->Messagedisposition = 'SaveOnly';

$msgResponse = $ews->CreateItem($msgRequest);
$msgResponseItems = $msgResponse->ResponseMessages->CreateItemResponseMessage->Items;

// Create attachment(s)
$attachments = array();
$i = 0;
foreach ($message_details['attachment'] as $attachment) {
    $attachments[$i] = new EWSType_FileAttachmentType();
    $attachments[$i]->Content = file_get_contents($attachment['path'] . '/' . $attachment['file']);
    $attachments[$i]->Name = $attachment['file'];
    $i++;
}
//
// Attach files to message
$attRequest = new EWSType_CreateAttachmentType();
$attRequest->ParentItemId = $msgResponseItems->Message->ItemId;
$attRequest->Attachments = new EWSType_NonEmptyArrayOfAttachmentsType();
$attRequest->Attachments->FileAttachment = $attachments;

$attResponse = $ews->CreateAttachment($attRequest);
$attResponseId = $attResponse->ResponseMessages->CreateAttachmentResponseMessage->Attachments->FileAttachment->AttachmentId;

// Save message id from create attachment response
$msgitemId = new EWSType_ItemIdType();
$msgitemId->ChangeKey = $attResponseId->RootItemChangeKey;
$msgitemId->Id = $attResponseId->RootItemId;

// Send and save message
$msgSendRequest = new EWSType_SendItemType();
$msgSendRequest->ItemIds = new EWSType_NonEmptyArrayOfBaseItemIdsType();
$msgSendRequest->ItemIds->ItemId = $msgitemId;
$msgSendRequest->SaveItemToFolder = true;
$msgSendResponse = $ews->SendItem($msgSendRequest);
$response = $msgSendResponse->ResponseMessages->SendItemResponseMessage;
?>

$ews-> SendItem()返回此错误

Uncaught SoapFault exception: [a:ErrorSchemaValidation] The request
Failed schema validation: The required attribute ‘Id’ is missing.

在这里想念什么?

解决方法:

在这里找到答案:

https://github.com/jamesiarmes/php-ews/issues/132

如果只有一个附件,Exchange基本上不使用数组,因此需要额外检查以确定从何处获取ID.

if(!is_array($attResponse->ResponseMessages->CreateAttachmentResponseMessage))
    $attResponseId = $attResponse->ResponseMessages->CreateAttachmentResponseMessage->Attachments->FileAttachment->AttachmentId;
else {
    $attResponseId = $attResponse->ResponseMessages->CreateAttachmentResponseMessage[0]->Attachments->FileAttachment->AttachmentId;
}

Exchange使用与收件人相同的结构.我发现这种情况不一致,但我确信这背后有一个原因.

我希望有人会从提高这一点中受益.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...