Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP-EWS 2010, how to set the IsRead flag

I have been able to successfully retrieve the unread emails from an Exchange 2010 inbox using php-ews API. However after I have fetched the emails, I want to set the IsRead property of the email to true, so that these messages do not appear the next time I fetch emails.

Anyone done this before ?

EDIT :

This is how I am trying to set the IsRead flag :

$message_id = ''; //id of message
$change_key = ''; //change key   
$response = $ews->GetItem($request);
        //print_r($response);exit;
        if( $response->ResponseMessages->GetItemResponseMessage->ResponseCode == 'NoError' &&
            $response->ResponseMessages->GetItemResponseMessage->ResponseClass == 'Success' ) {

            $a = array();
            $message = $response->ResponseMessages->GetItemResponseMessage->Items->Message;

            $a['message_body'] = $message->Body->_;
            $a['sender'] = $message->From->Mailbox->EmailAddress;
            $a['subject'] = $message->ConversationTopic;

            $data[] = $a;
            //process the message data.

            $messageType = new EWSType_MessageType();
            $messageType->IsRead = true;

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

            $setField = new EWSType_SetItemFieldType();
            $setField->Message = $messageType;
            $setField->FieldURI = $path;


            $u = new EWSType_ItemChangeType();
            $u->Updates = new EWSType_NonEmptyArrayOfItemChangeDescriptionsType();
            $u->Updates->SetItemField = $setField;
            $u->ItemId = new EWSType_ItemIdType();
            $u->ItemId->Id = $message_id;
            $u->ItemId->ChangeKey = $change_key;

            $updatedItems = new EWSType_NonEmptyArrayOfItemChangesType();
            $updatedItems->ItemChange = $u;

            $updateMessenger = new EWSType_UpdateItemType();
            $updateMessenger->ItemChanges = $updatedItems;
            $updateMessenger->MessageDisposition = 'SaveOnly';
            $updateMessenger->ConflictResolution = 'AutoResolve';

            try {
                $update_response = $ews->UpdateItem($updateMessenger);
            }catch (Exception $e){
                echo $e->getMessage();
            }
        }

When I run the file I get the following error :

An internal server error occurred. The operation failed.

After debugging for some time, I have concluded that the error happens at the curl_exec function in NTLMSoapClient.php file.

I dont know where to go on from here. Please help.

like image 946
Lin Avatar asked Nov 13 '22 22:11

Lin


1 Answers

I've faced a similar issue when updating a calendar event and setting the IsAllDayEvent flag. This is the code that worked for me:

$ews = new ExchangeWebServices(...);

$request = new EWSType_UpdateItemType();
$request->ConflictResolution = 'AlwaysOverwrite';
$request->ItemChanges = array();

$change = new EWSType_ItemChangeType();
$change->ItemId = new EWSType_ItemIdType();
$change->ItemId->Id = $id;
$change->ItemId->ChangeKey = $changeKey;

$field = new EWSType_SetItemFieldType();
$field->FieldURI = new EWSType_PathToUnindexedFieldType();
$field->FieldURI->FieldURI = "calendar:IsAllDayEvent";
$field->CalendarItem = new EWSType_CalendarItemType();
$field->CalendarItem->IsAllDayEvent = true;

$change->Updates->SetItemField[] = $field;

$request->ItemChanges[] = $change;
$response = $ews->UpdateItem($request);

The biggest difference I see here is that you do $u->Updates->SetItemField = $setField;, whereas my code uses $u->Updates->SetItemField[] = $setField;.

I hope this helps.

Edit: You might have already seen this, but I based my code on the one from the php-ews wiki.

like image 111
Paulo Margarido Avatar answered Nov 15 '22 13:11

Paulo Margarido