想要使用PHP中的EWS仅从Exchange服务器解析邮件主题和未读邮件正文

时间:2016-02-14 12:09:34

标签: php email exchange-server exchangewebservices

我想使用我的邮件ID从我们的Exchange服务器获取我收件箱中唯一未读邮件的邮件主题和正文。下面的代码显示所有未读邮件(如果未读邮件的数量大于2)。我的要求是 1.我想一次只阅读一封未读邮件,并在看完之后将其读取。 2.仅从地址和邮件正文解析邮件主题。下面是我的代码。

<?php
 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);exit;*/
if ($result->ResponseMessages->FindItemResponseMessage->ResponseCode == 'NoError' && $result->ResponseMessages->FindItemResponseMessage->ResponseClass == 'Success'){
$count = $result->ResponseMessages->FindItemResponseMessage->RootFolder->TotalItemsInView;
for ($i = 0; $i <= $count; $i++){
    $message_id = $result->ResponseMessages->FindItemResponseMessage->RootFolder->Items->Message[$i]->ItemId->Id;
    $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 = $message_id; 

    $response = $ews->GetItem($request);
    //print_r($response);exit;
    if( $response->ResponseMessages->GetItemResponseMessage->ResponseCode == 'NoError' &&
        $response->ResponseMessages->GetItemResponseMessage->ResponseClass == 'Success' ) {

        $message = $response->ResponseMessages->GetItemResponseMessage->Items->Message;
        var_dump($message);
        //echo $message["Subject"];

        //process the message data.

    }

}

}
?>

我的产量低于产量。

" ["BodyType"]=> string(4) "HTML" } ["Attachments"]=> object(stdClass)#30 (1) { ["FileAttachment"]=> object(stdClass)#31 (8) { ["AttachmentId"]=> object(stdClass)#32 (1) { ["Id"]=> string(160) "AQMkADM1NjQ4ZjU0LWI3OWYtNGZiMC1iYTgzLTU4N2E1MGMwYWNkMQBGAAADwDbQGclmE0ynv6bfgWfklwcA/nyIAiUCE0uTtywBMGOvqAAAATW4wQAAAHnU5M3amoNBtOc6QaMELQUAAY3ZiQ8AAAABBgAEAAAD" } ["Name"]=> string(12) "image001.png" ["ContentType"]=> string(9) "image/png" ["ContentId"]=> string(30) "image001.png@01D16750.1B18A6A0" ["Size"]=> int(5924) ["LastModifiedTime"]=> string(19) "2016-02-14T11:49:52" ["IsInline"]=> bool(true) ["IsContactPhoto"]=> bool(false) } } ["DateTimeReceived"]=> string(20) "2016-02-14T11:49:52Z" ["Size"]=> int(23434) ["Importance"]=> string(6) "Normal" ["InReplyTo"]=> string(0) "" ["IsSubmitted"]=> bool(false) ["IsDraft"]=> bool(false) ["IsFromMe"]=> bool(true) ["IsResend"]=> bool(false) ["IsUnmodified"]=> bool(true) ["InternetMessageHeaders"]=> object(stdClass)#33 (1) { ["InternetMessageHeader"]=> array(18) { [0]=> object(stdClass)#34 (2) { ["_"]=> string(155) "from BKASH-MBXLV-01.bKash.com ([fe80::882:1136:f891:8c3a]) by bKash-CASLV-01.bKash.com ([::1]) with mapi id 14.03.0123.003; Sun, 14 Feb 2016 17:49:52 +0600" ["HeaderName"]=> string(8) "Received" } [1]=> object(stdClass)#35 (2) { ["_"]=> string(19) "application/ms-tnef" ["HeaderName"]=> string(12) "Content-Type" } [2]=> object(stdClass)#36 (2) { ["_"]=> string(6) "binary" ["HeaderName"]=> string(25) "Content-Transfer-Encoding" } [3]=> object(stdClass)#37 (2) { ["_"]=> string(8) "RE: Test" ["HeaderName"]=> string(7) "Subject" } [4]=> object(stdClass)#38 (2) { ["_"]=> string(4) "Test" ["HeaderName"]=> string(12) "Thread-Topic" } [5]=> object(stdClass)#39 (2) { ["_"]=> string(64) "AdFnBm1WIIFTBh8YS6+gnYfxlIL57AABQTBAAADP50AAAF2CYAAAe3+QAALujbA=" ["HeaderName"]=> string(12) "Thread-Index" } [6]=> object(stdClass)#40 (2) { ["_"]=> string(31) "Sun, 14 Feb 2016 17:49:52 +0600" ["HeaderName"]=> string(4) "Date" } [7]=> object(stdClass)#41 (2) { ["_"]=> string(67) "" ["HeaderName"]=> string(10) "Message-ID" } [8]=> object(stdClass)#42 (2) { ["_"]=> string(5) "en-US" ["HeaderName"]=> string(15) "Accept-Language" } [9]=> object(stdClass)#43 (2) { ["_"]=> string(5) "en-US" ["HeaderName"]=> string(16) "Content-Language" } [10]=> object(stdClass)#44 (2) { ["_"]=> string(3) "yes" ["HeaderName"]=> string(15) "X-MS-Has-Attach" } [11]=> object(stdClass)#45 (2) { ["_"]=> string(2) "-1" ["HeaderName"]=> string(30) "X-MS-Exchange-Organization-SCL" } [12]=> object(stdClass)#46 (2) { ["_"]=> string(67) "" ["HeaderName"]=> string(20) "X-MS-TNEF-Correlator" } [13]=> object(stdClass)#47 (2) { ["_"]=> string(3) "1.0" ["HeaderName"]=> string(12) "MIME-Version" } [14]=> object(stdClass)#48 (2) { ["_"]=> string(24) "bKash-CASLV-01.bKash.com" ["HeaderName"]=> string(37) "X-MS-Exchange-Organization-AuthSource" } [15]=> object(stdClass)#49 (2) { ["_"]=> string(8) "Internal" ["HeaderName"]=> string(33) "X-MS-Exchange-Organization-AuthAs" } [16]=> object(stdClass)#50 (2) { ["_"]=> string(2) "04" ["HeaderName"]=> string(40) "X-MS-Exchange-Organization-AuthMechanism" } [17]=> object(stdClass)#51 (2) { ["_"]=> string(14) "[10.21.30.178]" ["HeaderName"]=> string(16) "X-Originating-IP" } } } ["DateTimeSent"]=> string(20) "2016-02-14T11:49:52Z" ["DateTimeCreated"]=> string(20) "2016-02-14T11:49:52Z" ["ResponseObjects"]=> object(stdClass)#52 (3) { ["ReplyToItem"]=> object(stdClass)#53 (0) { } ["ForwardItem"]=> object(stdClass)#54 (0) { } ["ReplyAllToItem"]=> object(stdClass)#55 (0) { } } ["ReminderIsSet"]=> bool(false) ["ReminderMinutesBeforeStart"]=> string(1) "0" ["DisplayCc"]=> string(0) "" ["DisplayTo"]=> string(51) "Basudev Kumar Sadhu (Assistant Manager, Technology)" ["HasAttachments"]=> bool(false) ["Culture"]=> string(5) "en-US" ["EffectiveRights"]=> object(stdClass)#56 (6) { ["CreateAssociated"]=> bool(false) ["CreateContents"]=> bool(false) ["CreateHierarchy"]=> bool(false) ["Delete"]=> bool(true) ["Modify"]=> bool(true) ["Read"]=> bool(true) } ["LastModifiedName"]=> string(51) "Basudev Kumar Sadhu (Assistant Manager, Technology)" ["LastModifiedTime"]=> string(20) "2016-02-14T11:50:26Z" ["IsAssociated"]=> bool(false) ["WebClientReadFormQueryString"]=> string(146) "?

但我只需要邮件主题,一次从收件箱中的一封未读邮件的地址和邮件正文,并让它看到..请帮我做。

0 个答案:

没有答案