在特定日期之后使用php-ews获取电子邮件(Exchange Web Services) [英] Getting emails after a specific date with php-ews (Exchange Web Services)
问题描述
在我的PHP脚本中,我需要弄清楚如何检索在指定消息ID之后或在特定日期之后的所有电子邮件(这两种方法都可以,我只需要检索自上次我以来的新电子邮件)抓取收件箱).
In my PHP script, I need to figure out how to retrieve all emails that are either after a specified message ID or after a specific date (Either will work, I just need to retrieve emails that are new since the last time I scraped the inbox).
此收件箱每天会收到数千封电子邮件,而且30天内我无法删除任何电子邮件.对于最初的导入,我只是在收件箱的开头做一个偏移,但是很明显,一旦我们开始清除电子邮件,该操作就不会起作用.
This inbox is getting thousands of emails a day, and I can't delete any emails for 30 days. For the initial import I was just doing an offset from the beginning of the inbox, but obviously that won't work once we start cleaning out emails.
我认为我必须设置类" EWSType_FindItemType ",但我认为 php-ews 中不存在必需的类一个>让我做到这一点.我尝试自己添加它们,但是对EWS或SOAP的了解还不够.
I think I have to set the $Restriction property of the class "EWSType_FindItemType", but I don't think the necessary classes exist in php-ews for me to do this. I've tried to add them myself, but I don't understand enough about EWS or SOAP.
到目前为止,我唯一想到的是:
So far the only thing I've come up with is this:
$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';
那行不通:(
这是我当前用于检索电子邮件的代码:
Here's the code I am currently using to retrieve email:
<?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限制以使它们之间的项目日期范围:
Restriction are tricky in EWS, true. You can take a look at haw they are used in EWSWrapper, here's example how to create AND restriction to get items in between date range:
//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-ews获取电子邮件(Exchange Web Services)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!