【问题标题】:Getting emails after a specific date with php-ews (Exchange Web Services)使用 php-ews (Exchange Web Services) 在特定日期后获取电子邮件
【发布时间】:2012-06-01 12:05:27
【问题描述】:

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

任何帮助都会非常感谢

【问题讨论】:

    标签: php exchangewebservices php-ews


    【解决方案1】:

    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
    

    【讨论】:

    • 我最近想通了,但实际上你提供的信息比我多,所以谢谢 :)
    • 你好,我收到错误“从 ... 中的空值创建默认对象”(在使用 $start->format('c') 的行中)。你能告诉我哪里有问题吗?可能有一些带有交换的区域设置?
    【解决方案2】:

    感谢 Maiiku 提供的代码示例!

    这就是我使用PHP Exchange Web Services library (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';
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 1970-01-01
      • 2016-10-14
      • 2011-10-14
      • 2018-04-29
      • 1970-01-01
      相关资源
      最近更新 更多