微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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使用与收件人相同的结构.我发现这种情况不一致,但我确信这背后有一个原因.

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐