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

怎甘沉沦 提交于 2021-02-07 08:53:55

问题


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!


回答1:


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



回答2:


Thanks Maiiku for your code samples!

This is how I enabled filtering by date and subject field using the PHP Exchange Web Services library (php-ews).

(You will need to require_once the relevant EWSType libraries first before using this sample).

$start = new DateTime('2013-03-31'); 

$Request->Restriction = new EWSType_RestrictionType();
$Request->Restriction->And = new EWSType_AndType();

$Request->Restriction->And->IsGreaterThanOrEqualTo = new EWSType_IsGreaterThanOrEqualToType();
$Request->Restriction->And->IsGreaterThanOrEqualTo->FieldURI = new stdClass;
$Request->Restriction->And->IsGreaterThanOrEqualTo->FieldURI->FieldURI = 'item:DateTimeReceived';
$Request->Restriction->And->IsGreaterThanOrEqualTo->FieldURIOrConstant->Constant->Value = $start->format('c');

$Request->Restriction->And->Contains = new EWSType_ContainsExpressionType();
$Request->Restriction->And->Contains->FieldURI = new stdClass;
$Request->Restriction->And->Contains->FieldURI->FieldURI = 'item:Subject';
$Request->Restriction->And->Contains->Constant->Value = 'annual leave application';
$Request->Restriction->And->Contains->ContainmentMode = 'Substring';
$Request->Restriction->And->Contains->ContainmentComparison = 'Exact';

Hope this helps!



来源:https://stackoverflow.com/questions/10762337/getting-emails-after-a-specific-date-with-php-ews-exchange-web-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!