编程语言
首页 > 编程语言> > 使用php-ews在特定日期之后收到电子邮件(Exchange Web服务)

使用php-ews在特定日期之后收到电子邮件(Exchange Web服务)

作者:互联网

在我的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中使用的山楂,这里的例子是如何创建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

标签:php,exchangewebservices,php-ews
来源: https://codeday.me/bug/20190902/1788620.html