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

3
在我的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
}

任何帮助都将不胜感激!
2个回答

3

EWS 中的限制条件确实有些棘手。您可以看一下 EWSWrapper 中它们的使用情况,这里是创建 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

1
我最近已经弄清楚了,但你提供的信息比我拥有的更多,所以谢谢 :) - Brandon
你好,我遇到了一个错误:“在...中从空值创建默认对象”(在使用 $start->format('c') 的行)。你能告诉我可能出了什么问题吗?是与交换机有关的某些区域设置吗? - user198003

2

感谢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';

希望这能帮到你!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接