我想获取收件箱中仅有未读邮件的邮件主题和正文。
以下代码显示了所有未读邮件的邮件ID。
require_once ('../mail3/php-ews-master/ExchangeWebServices.php');
require_once ('../mail3/php-ews-master/EWS_Exception.php');
require_once ('../mail3/php-ews-master/EWSType.php');
require_once ('../mail3/php-ews-master/NTLMSoapClient.php');
function __autoload($class_name)
{
// Start from the base path and determine the location from the class name,
$base_path = '../mail3/php-ews-master';
$include_file = $base_path . '/' . str_replace('_', '/', $class_name) . '.php';
return (file_exists($include_file) ? require_once $include_file : false);
}
$ews = new ExchangeWebServices("servername", "username", "password",ExchangeWebServices::VERSION_2010);
$request = new EWSType_FindItemType();
$itemProperties = new EWSType_ItemResponseShapeType();
$itemProperties->BaseShape = EWSType_DefaultShapeNamesType::ID_ONLY;
$itemProperties->BodyType = EWSType_BodyTypeResponseType::BEST;
$request->ItemShape = $itemProperties;
$fieldType = new EWSType_PathToUnindexedFieldType();
$fieldType->FieldURI = 'message:IsRead';
$constant = new EWSType_FieldURIOrConstantType();
$constant->Constant = new EWSType_ConstantValueType();
$constant->Constant->Value = "0";
$IsEqTo = new EWSType_IsEqualToType();
$IsEqTo->FieldURIOrConstant = $constant;
$IsEqTo->Path = $fieldType;
$request->Restriction = new EWSType_RestrictionType();
$request->Restriction->IsEqualTo = new EWSType_IsEqualToType();
$request->Restriction->IsEqualTo->FieldURI = $fieldType;
$request->Restriction->IsEqualTo->FieldURIOrConstant = $constant;
$request->IndexedPageItemView = new EWSType_IndexedPageViewType();
$request->IndexedPageItemView->BasePoint = 'Beginning';
$request->IndexedPageItemView->Offset = 0;
$request->ParentFolderIds = new EWSType_NonEmptyArrayOfBaseFolderIdsType();
$request->ParentFolderIds->DistinguishedFolderId = new EWSType_DistinguishedFolderIdType();
$request->ParentFolderIds->DistinguishedFolderId->Id = EWSType_DistinguishedFolderIdNameType::INBOX;
$request->Traversal = EWSType_ItemQueryTraversalType::SHALLOW;
$result = new EWSType_FindItemResponseMessageType();
$result = $ews->FindItem($request);
echo '<pre>';
print_r($result);
获取后如何将邮件标记为已读?
stdClass Object
(
[ResponseMessages] => stdClass Object
(
[FindItemResponseMessage] => stdClass Object
(
[ResponseCode] => NoError
[ResponseClass] => Success
[RootFolder] => stdClass Object
(
[Items] => stdClass Object
(
[Message] => Array
(
[0] => stdClass Object
(
[ItemId] => stdClass Object
(
[Id] => AAMkADM1NjQ4ZjU0LWI3OWYtNGZiMC1iYTgzLTU4N2E1MGMwYWNkMQBGAAAAAADANtAZyWYTTKe/pt+BZ+SXBwD+fIgCJQITS5O3LAEwY6+oAAAANbjBAAB51OTN2pqDQbTnOkGjBC0FAAGN2YkTAAA=
[ChangeKey] => CQAAABYAAAD+fIgCJQITS5O3LAEwY6+oAAC4WS4O
)
)
[1] => stdClass Object
(
[ItemId] => stdClass Object
(
[Id] => AAMkADM1NjQ4ZjU0LWI3OWYtNGZiMC1iYTgzLTU4N2E1MGMwYWNkMQBGAAAAAADANtAZyWYTTKe/pt+BZ+SXBwD+fIgCJQITS5O3LAEwY6+oAAAANbjBAAB51OTN2pqDQbTnOkGjBC0FAAGN2YkSAAA=
[ChangeKey] => CQAAABYAAAD+fIgCJQITS5O3LAEwY6+oAAC4WS35
)
)
)
)
[IndexedPagingOffset] => 2
[IncludesLastItemInRange] => 1
[TotalItemsInView] => 2
)
)
)
)
答案 0 :(得分:3)
我发现您正在使用jamesiarmes/php-ews
php-ews
版本的require_once "vendor/autoload.php";
use jamesiarmes\PEWS\API\Type;
use jamesiarmes\PEWS\Mail\MailAPI;
$api = MailAPI::withUsernameAndPassword('server', 'username', 'password');
$unreadMail = $api->getUnreadMailItems();
,所以我会尝试回答这个问题。我可能有点偏离,因为我不会使用该版本,并且会首先鼓励您升级到我维护和更新的分支,因为它更容易使用,您将获得更多支持,它的PSR-2和4兼容并仍然保持。它被称为garethp/php-ews
。我将首先给出我的答案,因为它简短易行,然后转到您使用的代码库
基本上有三个部分。读取,读取和标记为读取。第一种是从服务器获取未读的电子邮件,这样做是
$item = $unreadMail[0];
$item = $api->getItem($item->getItemId());
$subject = $item->getSubject();
$sender = $item->getSender()->getMailbox()->getEmailAddress();
$body = (string) $item->getBody();
解决方案的第二部分是读取该项目。当您获取邮件列表时,EWS不会返回邮件项的正文。它认为身体是第二类财产,因此您需要专门询问该邮件的信息以获取身体。因此,为了做到这一点,我们执行以下操作
$api->markMailAsRead($item->getItemId());
最后一部分是mark an item as read,这是完成的。
require_once "vendor/autoload.php";
use jamesiarmes\PEWS\API\Type;
use jamesiarmes\PEWS\Mail\MailAPI;
$api = MailAPI::withUsernameAndPassword('server', 'username', 'password');
$unreadMail = $api->getUnreadMailItems();
foreach ($unreadMail as $item) {
$item = $api->getItem($item->getItemId());
$subject = $item->getSubject();
$sender = $item->getSender()->getMailbox()->getEmailAddress();
$body = (string) $item->getBody();
$api->markMailAsRead($item->getItemId());
}
邮件项目现在应显示为已读。所以,如果我们将它们放在一起,它应该像
$result = $ews->FindItem($request);
foreach ($result->ResponseMessages->FindItemResponseMessage->RootFolder->Items->Message as $item) {
$request = new EWSType_GetItemType();
$request->ItemShape = new EWSType_ItemResponseShapeType();
$request->ItemShape->BaseShape = EWSType_DefaultShapeNamesType::ALL_PROPERTIES;
$request->ItemIds = new EWSType_NonEmptyArrayOfBaseItemIdsType();
$request->ItemIds->ItemId = new EWSType_ItemIdType();
$request->ItemIds->ItemId->Id = $item->ItemId->Id;
$response = $ews->GetItem($request);
//You may have to do a var_dump on the $response here. I'm only guessing that this is how you locate the message item, since I don't use this code base any more.
$item = $response->ResponseMessages->GetItemResponseMessage->Items->Message;
//You should do a var_dump on the $item to see how to get the body, subject and sender here. I'm not 100% sure how to do it on this one.
//Mark the item as read (hopefully)
$request = new EWSType_UpdateItemType();
$request->MessageDisposition = 'SaveOnly';
$request->ConflictResolution = 'AlwaysOverwrite';
$request->ItemChanges = [];
$change = new EWSType_ItemChangeType();
$change = new EWSType_ItemChangeType();
$change->ItemId = new EWSType_ItemIdType();
$change->ItemId->Id = $item->ItemId->Id;
$change->ItemId->ChangeKey = $item->ItemId->ChangeKey;
$change->Updates = new EWSType_NonEmptyArrayOfItemChangeDescriptionsType();
$change->Updates->SetItemField = array(); // Array of fields to be update
// Update Firstname (simple property)
$field = new EWSType_SetItemFieldType();
$field->FieldURI->FieldURI = 'message:IsRead';
$field->Message = new EWSType_MessageItemType();
$field->Message->IsRead = true;
$change->Updates->SetItemField[] = $field;
// Set all changes
$request->ItemChanges[] = $change;
// Send request
$response = $ews->UpdateItem($request);
echo '<pre>'.print_r($response, true).'</pre>';
}
这分为三个步骤(见上文):读取,读取和标记为已读。显然,你知道怎么去取。因此,我们将跳到另外两个部分。
t1 t2 t3 t4 t5 t6 t7
[1,] 0 0 0 0 1 1 0
[2,] 0 0 1 1 0 0 1
[3,] 1 1 0 0 0 0 0
[4,] 0 0 1 1 1 0 0
[5,] 0 0 1 1 0 0 0
[6,] 1 1 0 0 0 0 0
[7,] 0 0 0 0 0 0 1
[8,] 0 0 0 0 1 1 0
[9,] 1 1 0 0 1 1 0
[10,] 0 0 0 0 0 1 1
[11,] 1 1 0 0 0 0 0
[12,] 0 0 1 1 0 0 0
[13,] 0 0 0 0 0 0 0
[14,] 0 0 0 0 0 0 1
[15,] 0 0 0 0 0 0 0
如果没有对它进行实际测试,那应该大致就是如何做到的。你可能不得不玩它来使它工作。你可以看到为什么我建议使用我的叉子。