phpexchangewebservicesphp-ews

Getting emails after a specific date with php-ews (Exchange Web Services)


In my PHP script, I need to figure out how to retrieve all emails that are either after a specified message ID or after a specific date (Either will work, I just need to retrieve emails that are new since the last time I scraped the inbox).

This inbox is getting thousands of emails a day, and I can't delete any emails for 30 days. For the initial import I was just doing an offset from the beginning of the inbox, but obviously that won't work once we start cleaning out emails.

I think I have to set the $Restriction property of the class "EWSType_FindItemType", but I don't think the necessary classes exist in php-ews for me to do this. I've tried to add them myself, but I don't understand enough about EWS or SOAP.

So far the only thing I've come up with is this:

$Request->Restriction = new EWSType_RestrictionType();
$Request->Restriction->IsGreaterThan = new stdClass;
$Request->Restriction->IsGreaterThan->FieldURIOrConstant = new stdClass;
$Request->Restriction->IsGreaterThan->FieldURIOrConstant->Constant = '2012-01-02T07:04:00Z';
$Request->Restriction->IsGreaterThan->FieldURI = new stdClass;
$Request->Restriction->IsGreaterThan->FieldURI->FieldURI = 'item:DateTimeReceived';

And that doesn't work :(

Here's the code I am currently using to retrieve email:

<?php
require( dirname( __FILE__ ) . '/ews/ExchangeWebServicesLoader.php' );

$ews = new ExchangeWebServices( EXCHANGE_HOSTNAME, EXCHANGE_USERNAME, EXCHANGE_PASSWORD, ExchangeWebServices::VERSION_2010_SP1 );

$Request = new EWSType_FindItemType();

$Request->ItemShape = new EWSType_ItemResponseShapeType();
$Request->ItemShape->BaseShape = EWSType_DefaultShapeNamesType::ALL_PROPERTIES;
$Request->ItemShape->BodyType = EWSType_BodyTypeResponseType::TEXT;
$Request->ItemShape->BodyTypeSpecified = true;

$Request->Traversal = EWSType_ItemQueryTraversalType::SHALLOW;

$Request->IndexedPageItemView = new EWSType_IndexedPageViewType();
$Request->IndexedPageItemView->MaxEntriesReturned = 25;
$Request->IndexedPageItemView->BasePoint = 'Beginning';
$Request->IndexedPageItemView->Offset = $offset;

$Request->ParentFolderIds = new EWSType_NonEmptyArrayOfBaseFolderIdsType();
$Request->ParentFolderIds->DistinguishedFolderId = new EWSType_DistinguishedFolderIdType();
$Request->ParentFolderIds->DistinguishedFolderId->Id = 'inbox';
$Request->ParentFolderIds->DistinguishedFolderId->Mailbox = new EWSType_EmailAddressType();
$Request->ParentFolderIds->DistinguishedFolderId->Mailbox->EmailAddress = 'sharedmailbox@company.org';

// sort order
$Request->SortOrder = new EWSType_NonEmptyArrayOfFieldOrdersType();
$Request->SortOrder->FieldOrder = array();
$order = new EWSType_FieldOrderType();
$order->FieldURI = new stdClass;
$order->FieldURI->FieldURI = 'item:DateTimeReceived';
$order->Order = 'Ascending';
$Request->SortOrder->FieldOrder[] = $order;

$response = $ews->FindItem($Request);
$items = $response->ResponseMessages->FindItemResponseMessage->RootFolder->Items->Message;

foreach ( $items as $item ) {
    // Do stuff
}

Any help would be greatly appreciated!


Solution

  • Restriction are tricky in EWS, true. You can take a look at haw they are used in EWSWrapper, here's example how to create AND restriction to get items in between date range:

    //create AND restrction
    $request->Restriction = new EWSType_RestrictionType();
    $request->Restriction->And = new EWSType_AndType();
    
    $request->Restriction->And->IsGreaterThanOrEqualTo = new EWSType_IsGreaterThanOrEqualToType();
    $request->Restriction->And->IsGreaterThanOrEqualTo->ExtendedFieldURI = new EWSType_PathToExtendedFieldType;
    $request->Restriction->And->IsGreaterThanOrEqualTo->ExtendedFieldURI->DistinguishedPropertySetId = "Task";
    $request->Restriction->And->IsGreaterThanOrEqualTo->ExtendedFieldURI->PropertyId = "33029";
    $request->Restriction->And->IsGreaterThanOrEqualTo->ExtendedFieldURI->PropertyType = "SystemTime";
    $request->Restriction->And->IsGreaterThanOrEqualTo->FieldURIOrConstant->Constant->Value = date('c', $start);
    
    $request->Restriction->And->IsLessThanOrEqualTo = new EWSType_IsLessThanOrEqualToType();
    $request->Restriction->And->IsLessThanOrEqualTo->ExtendedFieldURI = new EWSType_PathToExtendedFieldType;
    $request->Restriction->And->IsLessThanOrEqualTo->ExtendedFieldURI->DistinguishedPropertySetId = "Task";
    $request->Restriction->And->IsLessThanOrEqualTo->ExtendedFieldURI->PropertyId = "33029";
    $request->Restriction->And->IsLessThanOrEqualTo->ExtendedFieldURI->PropertyType = "SystemTime";
    $request->Restriction->And->IsLessThanOrEqualTo->FieldURIOrConstant->Constant->Value = date('c', $end);
    

    And the types used:

    class EWSType_RestrictionType extends EWSType {
    /**
     * SearchExpression property
     * 
     * @var EWSType_SearchExpressionType
     */
    public $SearchExpression;
    
    /**
     * Constructor
     */
    public function __construct() {
        $this->schema = array(
            array(
                'name' => 'SearchExpression',
                'required' => false,
                'type' => 'SearchExpressionType',
            ),
        ); // end $this->schema
    } // end function __construct()
    } // end class RestrictionType
    
    <?php
    
    class EWSType_AndType extends EWSType {
    /**
     * SearchExpression property
     * 
     * @var EWSType_MultipleOperandBooleanExpressionType
     */
    public $SearchExpression;
    
    /**
     * Constructor
     */
    public function __construct() {
        $this->schema = array(
            array(
                'name' => 'SearchExpression',
                'required' => false,
                'type' => 'MultipleOperandBooleanExpressionType',
            ),          
        ); // end $this->schema
    } // end function __construct()
    } // end class AndType
    class EWSType_IsLessThanOrEqualToType extends EWSType {
    /**
     * SearchExpression property
     * 
     * @var EWSType_TwoOperandExpressionType
     */
    public $SearchExpression;
    
    /**
     * Constructor
     */
    public function __construct() {
        $this->schema = array(
            array(
                'name' => 'SearchExpression',
                'required' => false,
                'type' => 'TwoOperandExpressionType',
            ),
        ); // end $this->schema
    } // end function __construct()
    } // end class IsLessThanOrEqualToType
    
    
    class EWSType_IsGreaterThanOrEqualToType extends EWSType {
    /**
     * SearchExpression property
     * 
     * @var EWSType_TwoOperandExpressionType
     */
    public $SearchExpression;
    
    /**
     * Constructor
     */
    public function __construct() {
        $this->schema = array(
            array(
                'name' => 'SearchExpression',
                'required' => false,
                'type' => 'TwoOperandExpressionType',
            ),
        ); // end $this->schema
    } // end function __construct()
    } // end class IsGreaterThanOrEqualToType