在我的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
}
任何帮助都非常赞赏!
答案 0 :(得分:3)
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
答案 1 :(得分: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';
希望这有帮助!