PHP and Exchange web services: fetching message body using php-ews

I had no luck getting the email body from Exchange 2010 using php-ews. This is my code:

$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
}

      

The if loop is entered, but the $ message variable contains nothing. Usually this should be an object from which I can get the body of the email.

What am I doing wrong?

I tried this inside an if () statement and I got the $ message object:

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

      

The "RootFolder" attribute is missing. However, the resulting $ message variable does not contain a body attribute that contains the body of the message. This is what print_r ($ message) looks like:

stdClass Object

      

([ItemId] => Object stdClass ([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
            )

    )

      

)

I also need the sender and recipient EMAIL ADDRESS. Only the first and last name are displayed here. Please help, this is very relevant.

+1


source to share


3 answers


Finally I got the job! This is how you retrieve unread emails from 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.

        }

    }

}

      

+3


source


Here is the best solution. You don't want to call GetItem request for a message every time. It's expensive and inefficient, and getting emails in folders containing thousands of emails is terribly slow. Also note, if an exchange returns 1 email or multiple emails, some objects are embedded in an array, so your code must account for it. This code snippet will return multiple email bodies, if available.



$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);
}

      

+1


source


$message

is an object containing the entire message (including headers, for example), not a line with the message body. To get an object containing the message body, the type of the message body, etc. Use $message->body

, and if you're just interested in printing the content of the message body, use$message->body->_

0


source







All Articles