1

我没有运气使用 php-ews 从 Exchange 2010 服务器获取电子邮件的消息正文。这是我的代码:

$ews = new ExchangeWebServices("hostname", "username","password",ExchangeWebServices::VERSION_2010);

$request = new EWSType_FindItemType();

$itemProperties = new EWSType_ItemResponseShapeType();

$itemProperties->BaseShape = EWSType_DefaultShapeNamesType::ALL_PROPERTIES;
$itemProperties->BodyType = EWSType_BodyTypeResponseType::BEST;

$request->ItemShape = $itemProperties;

//If I do not include the below three sentences I get a PHP Fatal error saying "Basepoint" not found

$request->IndexedPageItemView = new EWSType_IndexedPageViewType();
$request->IndexedPageItemView->BasePoint = 'Beginning';

$request->IndexedPageItemView->Offset = 0;

$request->ParentFolderIds = new EWSType_NonEmptyArrayOfBaseFolderIdsType();
$request->ParentFolderIds->DistinguishedFolderId = new EWSType_DistinguishedFolderIdType();
$request->ParentFolderIds->DistinguishedFolderId->Id = EWSType_DistinguishedFolderIdNameType::INBOX;

$request->Traversal = EWSType_ItemQueryTraversalType::SHALLOW;

$result = new EWSType_FindItemResponseMessageType();

$result = $ews->FindItem($request);
if ($result->ResponseMessages->FindItemResponseMessage->ResponseCode == 'NoError' && $result->ResponseMessages->FindItemResponseMessage->ResponseClass == 'Success'){

    $message = $result->ResponseMessages->FindItemResponseMessage->Items->Message;
        //This if loop is entered but the $message variable contains nothing
    print_r($message);exit;
    //var_dump($message) displays NULL
}

进入“if”循环,但 $message 变量不包含任何内容。通常它应该是一个对象,我可以从中检索电子邮件的正文。

我究竟做错了什么 ?

我在 if() 语句中尝试了这个,我得到了 $message 对象:

$message = $result->ResponseMessages->FindItemResponseMessage->RootFolder->Items->Message;
print_r($message);exit;

缺少“RootFolder”属性。但是,生成的 $message 变量不包含任何包含电子邮件正文的“正文”属性。这就是 print_r($message) 的样子:

stdClass Object

( [ItemId] => stdClass Object ( [Id] => AAMkADZjOTZhNjNkLTBmZTAtNDdkZi04NGI5LTdiZWY0ZWYzOGNhMwBGAAAAAABI9LX/rIDdRr5XMdDM8RGZBwAL3uOTQAYYQ4U8CTQP/+wKAAAAWsnbAAAL3uOTQAYYQ4U8CTQP/+wKAAAAWvd0AAA= [ChangeKey] => CQAAABYAAAAL3uOTQAYYQ4U8CTQP/+wKAAAAWyCi )

[ParentFolderId] => stdClass Object
    (
        [Id] => AQMkADZjOTZhNjNkLTBmZTAtNDdkZi04NGI5LTdiZWY0ZWYzOGNhMwAuAAADSPS1/6yA3Ua+VzHQzPERmQEAC97jk0AGGEOFPAk0D//sCgAAAVrJ2wAAAA==
        [ChangeKey] => AQAAAA==
    )

[ItemClass] => IPM.Note
[Subject] => Test
[Sensitivity] => Normal
[DateTimeReceived] => 2012-09-05T13:03:08Z
[Size] => 4754
[Importance] => Normal
[IsSubmitted] => 
[IsDraft] => 
[IsFromMe] => 
[IsResend] => 
[IsUnmodified] => 1
[DateTimeSent] => 2012-09-05T13:03:00Z
[DateTimeCreated] => 2012-09-05T13:03:08Z
[DisplayCc] => 
[DisplayTo] => sara cooper
[HasAttachments] => 
[Culture] => en-IN
[EffectiveRights] => stdClass Object
    (
        [CreateAssociated] => 
        [CreateContents] => 
        [CreateHierarchy] => 
        [Delete] => 1
        [Modify] => 1
        [Read] => 1
    )

[LastModifiedName] => Lin T
[LastModifiedTime] => 2012-09-05T13:03:08Z
[IsAssociated] => 
[WebClientReadFormQueryString] => ?ae=Item&a=Open&t=IPM.Note&id=RgAAAABI9LX%2frIDdRr5XMdDM8RGZBwAL3uOTQAYYQ4U8CTQP%2f%2bwKAAAAWsnbAAAL3uOTQAYYQ4U8CTQP%2f%2bwKAAAAWvd0AAAJ&exvsurl=1
[ConversationId] => stdClass Object
    (
        [Id] => AAQkADZjOTZhNjNkLTBmZTAtNDdkZi04NGI5LTdiZWY0ZWYzOGNhMwAQAJ56TrE0QUIAk59ALIqWrrY=
    )

[Sender] => stdClass Object
    (
        [Mailbox] => stdClass Object
            (
                [Name] => Lin T
                [MailboxType] => OneOff
            )

    )

[IsReadReceiptRequested] => 
[ConversationIndex] => Í‹fÃIžzN±4AB“Ÿ@,Š–®¶
[ConversationTopic] => Test
[From] => stdClass Object
    (
        [Mailbox] => stdClass Object
            (
                [Name] => Lin T
                [MailboxType] => OneOff
            )

    )

[InternetMessageId] => <027d01cd8b66$c8c967e0$5a5c37a0$@marlabs.com>
[IsRead] => 
[ReceivedBy] => stdClass Object
    (
        [Mailbox] => stdClass Object
            (
                [Name] => sara cooper
                [MailboxType] => OneOff
            )

    )

[ReceivedRepresenting] => stdClass Object
    (
        [Mailbox] => stdClass Object
            (
                [Name] => sara cooper
                [MailboxType] => OneOff
            )

    )

)

我还需要发件人和收件人的电子邮件地址。这里只显示名字和姓氏。请帮助这变得非常紧迫。

4

3 回答 3

3

我终于让它工作了!以下是从 Exchange 2010 Server 检索未读电子邮件的方法:

    $ews = new ExchangeWebServices("hostname", "username", "password",ExchangeWebServices::VERSION_2010);

$request = new EWSType_FindItemType();
$itemProperties = new EWSType_ItemResponseShapeType();
$itemProperties->BaseShape = EWSType_DefaultShapeNamesType::ID_ONLY;
$itemProperties->BodyType = EWSType_BodyTypeResponseType::BEST;
$request->ItemShape = $itemProperties;

$fieldType = new EWSType_PathToUnindexedFieldType();
$fieldType->FieldURI = 'message:IsRead';

$constant = new EWSType_FieldURIOrConstantType();
$constant->Constant = new EWSType_ConstantValueType();
$constant->Constant->Value = "0";

$IsEqTo = new EWSType_IsEqualToType();
$IsEqTo->FieldURIOrConstant = $constant;
$IsEqTo->Path = $fieldType;

$request->Restriction = new EWSType_RestrictionType();
$request->Restriction->IsEqualTo = new EWSType_IsEqualToType();
$request->Restriction->IsEqualTo->FieldURI = $fieldType;
$request->Restriction->IsEqualTo->FieldURIOrConstant = $constant;

$request->IndexedPageItemView = new EWSType_IndexedPageViewType();
$request->IndexedPageItemView->BasePoint = 'Beginning';
$request->IndexedPageItemView->Offset = 0;

$request->ParentFolderIds = new EWSType_NonEmptyArrayOfBaseFolderIdsType();
$request->ParentFolderIds->DistinguishedFolderId = new EWSType_DistinguishedFolderIdType();
$request->ParentFolderIds->DistinguishedFolderId->Id = EWSType_DistinguishedFolderIdNameType::INBOX;

$request->Traversal = EWSType_ItemQueryTraversalType::SHALLOW;

$result = new EWSType_FindItemResponseMessageType();
$result = $ews->FindItem($request);
/*echo '<pre>';
print_r($result);exit;*/
if ($result->ResponseMessages->FindItemResponseMessage->ResponseCode == 'NoError' && $result->ResponseMessages->FindItemResponseMessage->ResponseClass == 'Success'){
    $count = $result->ResponseMessages->FindItemResponseMessage->RootFolder->TotalItemsInView;
    for ($i = 0; $i < $count; $i++){
        $message_id = $result->ResponseMessages->FindItemResponseMessage->RootFolder->Items->Message[$i]->ItemId->Id;
        $request = new EWSType_GetItemType();

        $request->ItemShape = new EWSType_ItemResponseShapeType();
        $request->ItemShape->BaseShape = EWSType_DefaultShapeNamesType::ALL_PROPERTIES;

        $request->ItemIds = new EWSType_NonEmptyArrayOfBaseItemIdsType();
        $request->ItemIds->ItemId = new EWSType_ItemIdType();
        $request->ItemIds->ItemId->Id = $message_id; 

        $response = $ews->GetItem($request);
        //print_r($response);exit;
        if( $response->ResponseMessages->GetItemResponseMessage->ResponseCode == 'NoError' &&
            $response->ResponseMessages->GetItemResponseMessage->ResponseClass == 'Success' ) {

            $message = $response->ResponseMessages->GetItemResponseMessage->Items->Message;

            //process the message data.

        }

    }

}
于 2012-09-10T12:44:00.720 回答
1

这是一个更好的解决方案。您不希望每次都针对消息发出 GetItem 请求。这是昂贵且低效的,并且在包含数千封电子邮件的文件夹中获取电子邮件非常慢。另请注意,如果 exchange 返回 1 封电子邮件与多封电子邮件,则某些对象会嵌入到数组中,因此您的代码需要考虑它。此代码段将带回多个电子邮件正文(如果可用)。

$ews = new ExchangeWebServices("hostname", "username", "password", ExchangeWebServices::VERSION_2010);

$request = new EWSType_FindItemType();
$itemProperties = new EWSType_ItemResponseShapeType();
$itemProperties->BaseShape = EWSType_DefaultShapeNamesType::ID_ONLY;
$itemProperties->BodyType = EWSType_BodyTypeResponseType::TEXT;
$request->ItemShape = $itemProperties;

$request->ParentFolderIds = new EWSType_NonEmptyArrayOfBaseFolderIdsType();
$request->ParentFolderIds->DistinguishedFolderId = new EWSType_DistinguishedFolderIdType();
$request->ParentFolderIds->DistinguishedFolderId->Id = EWSType_DistinguishedFolderIdNameType::INBOX;

$request->Traversal = EWSType_ItemQueryTraversalType::SHALLOW;

$result = new EWSType_FindItemResponseMessageType();
$result = $ews->FindItem($request);

if ($result->ResponseMessages->FindItemResponseMessage->ResponseCode == 'NoError' && $result->ResponseMessages->FindItemResponseMessage->ResponseClass == 'Success') {
    $count = $result->ResponseMessages->FindItemResponseMessage->RootFolder->TotalItemsInView;
    $request = new EWSType_GetItemType();
    $request->ItemShape = new EWSType_ItemResponseShapeType();
    $request->ItemShape->BaseShape = EWSType_DefaultShapeNamesType::ALL_PROPERTIES;
    for ($i = 0; $i < $count; $i++){
        $message_id = $result->ResponseMessages->FindItemResponseMessage->RootFolder->Items->Message[$i]->ItemId->Id;

        $messageItem = new \EWSType_ItemIdType();
        $messageItem->Id = $message_id;
        $request->ItemIds->ItemId[] = $messageItem;

    }

    // Here is your response
    $response = $ews->GetItem($request);
}
于 2014-05-20T18:49:57.030 回答
0

$message是一个包含整个消息(例如包括标题)的对象,而不是带有消息正文的字符串。要获取包含消息正文、消息正文类型等的对象$message->body,如果您只是对打印消息正文内容感兴趣,请使用$message->body->_

于 2012-09-06T14:03:47.037 回答