phpexchangewebservicesphp-ews

Restriction (Search) by Sender


I'm attempting to retrieve a list of emails by a particular sender on an exchange server using PHP EWS.

My code sample related specifically to the restriction (search) construction:

$request->Restriction = new EWSType_RestrictionType();
$request->Restriction->IsEqualTo = new EWSType_IsEqualToType();

$request->Restriction->IsEqualTo->FieldURI = new EWSType_PathToUnindexedFieldType();
$request->Restriction->IsEqualTo->FieldURI->FieldURI = 'message:Sender';

$request->Restriction->IsEqualTo->FieldURIOrConstant = new EWSType_FieldURIOrConstantType();
$request->Restriction->IsEqualTo->FieldURIOrConstant->Constant->Value = 'Bob Smith';

This type of restriction results in zero results.

I notice that when I search without restrictions, the result returned contains the sender information (but it is nested). eg:

[Sender] => stdClass Object
  (
    [Mailbox] => stdClass Object
      (
        [Name] => Bob Smith
      )
  )

How do I cater for the nested information in the restriction?

Other search expression examples: https://github.com/jamesiarmes/php-ews/wiki/Search-Expression:-Simple-Conditions


Solution

  • Based on the MSDN docs, message:Sender has the following definition:

    Property Value

    Type: Microsoft.Exchange.WebServices.Data.EmailAddress

    An e-mail address.

    So instead of using the qualified name "Bob Smith" (Outlook may recognize it, but EWS has no clue), use the email address ('bsmith@foo.com').

    Additionally, while the code above should work, it will likely throw an error since Constant never gets defined. Try this instead:

    $request->Restriction->IsEqualTo->FieldURIOrConstant = new EWSType_FieldURIOrConstantType();
    $request->Restriction->IsEqualTo->FieldURIOrConstant->Constant = new EWSType_ConstantValueType();
    $request->Restriction->IsEqualTo->FieldURIOrConstant->Constant->Value = 'bsmith@foo.com';