php-ews
php-ews copied to clipboard
Trouble in getting search results from combined restriction
I'm trying to get all mails from a folder that are a.) unread b.) newer than a special date. Each single restriction works fine in this call:
$mailItems = $api->getMailItems( $subFolder->getFolderId(), $options);
The AND combined restriction does not - not sure what is going wrong.
First the working restrictions:
$options = array(
'Restriction' => array(
'IsGreaterThanOrEqualTo' => array(
'dateTimeReceived' => $fromDate
)
)
);
$options = array(
'Restriction' => array(
'IsEqualTo' => array(
'IsRead' => false
)
)
);
The combined that does not work:
$options = array(
'Restriction' => array(
'And' => array(
'IsGreaterThanOrEqualTo' => array( 'dateTimeReceived' => $fromDate ),
'IsEqualTo' => array( 'IsRead' => false )
)
)
);
would be very happy for any hint.
I'm late to the party but this works for me. Also uses impersonation, pagination of results as well as trawling sub folder rather than root Inbox.
$host = 'myexch';
$username = 'myuser';
$password = 'mypassword';
$api = MailAPI::withUsernameAndPassword($host, $username, $password, ['impersonation' => '[email protected]','timezone' => 'GMT Standard Time']);
$folder = $api->getFolderByDisplayName('MyFolder', \garethp\ews\API\Enumeration\DistinguishedFolderIdNameType::INBOX);
$folderId = $folder->getfolderId();
$datefilter = date("F d 07:00:00", strtotime('today'));
$date = new DateTime($datefilter);
$restriction = [
'And' => [
'IsGreaterThan' => ['DateTimeReceived' => $date->format('c')],
'And' => [
'IsEqualTo' => [
'IsRead' => false
]
]
]
];
$emails = array();
$i=0;
$step = 50;
do {
$results = $api->getMailItems($folderId, [
'IndexedPageItemView' => [
'MaxEntriesReturned' => $step,
'Offset' => $i,
'BasePoint' => 'Beginning'
],
'Restriction' => $restriction
]);
foreach ($results as $z=>$a) {
$emails[] = $a->getSubject();
}
$i = $i+$step;
} while ($results->isIncludesLastItemInRange()===false);
print_r($emails);