在特定日期之后使用 php-ews(Exchange Web Services)获取电子邮件


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

在我的PHP脚本中,我需要弄清楚如何检索指定邮件ID之后或特定日期之后的所有电子邮件(两者都可以,我只需要检索自上次抓取收件箱以来的新电子邮件)。

这个收件箱每天收到数千封电子邮件,我在 30 天内无法删除任何电子邮件。对于最初的导入,我只是从收件箱的开头做一个偏移,但显然一旦我们开始清理电子邮件,这将不起作用。

我想我必须设置类"EWSType_FindItemType"的$Restriction属性,但我认为 php-ews 中不存在必要的类来做到这一点。我尝试自己添加它们,但我对 EWS 或 SOAP 不够了解。

到目前为止,我唯一想到的是:

$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';

这:(

不起作用

这是我目前用于检索电子邮件的代码:

<?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
}

任何帮助将胜感激!

限制在 EWS 中很棘手,真的。您可以查看它们在 EWSWrapper 中使用的 haw,以下示例如何创建 AND 限制以在日期范围之间获取项目:

//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);

以及使用的类型:

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

感谢 Maiiku 提供的代码示例!

这就是我如何使用PHP Exchange Web Services库(php-ews)启用按日期和主题字段过滤的方式。

(在使用此示例之前,需要先require_once相关的 EWSType 库)。

$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';

希望这有帮助!