0

i want to get the mail subject and body of only unread mail of my inbox from our exchange server using my mail id. below code shows all unread mail(if number of unread mail is greater than 2). my reuirement is 1. i want to read only one unread mail at a time and make it as read after seen. 2. only parse mail subject, from address and mail body. below is my code.

<?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.

    }

}

}
?>

i am getting below output.

" ["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) "?

but i need only mail subject, from address and mail body of a single unread mail from inbox at a time and make it seen..please help me to do it.

bKashOST
  • 149
  • 4
  • 11
  • **Do not post the same question twice**. Since I already edited the more recent one I'm voting to close this one as the duplicate. You may want to update that more recent one. – Jan Doggen Feb 15 '16 at 15:16
  • still i am unable to find a solution. can anyone please help me.. – bKashOST Feb 16 '16 at 04:35

0 Answers0