Getting unread mail from exchange web services via PHP - php

How do you get all unread mail in a users' exchange mailbox using PHP while using this class ?
I figured to first list a folders contents like this:
$ews = new ExchangeWebServices("mailserver.domain.local", "user", "pass");
$request = new EWSType_FindFolderType();
$request->FolderShape = new EWSType_FolderResponseShapeType();
$request->FolderShape->BaseShape = EWSType_DefaultShapeNamesType::DEFAULT_PROPERTIES;
$request->ParentFolderIds = new EWSType_NonEmptyArrayOfBaseFolderIdsType();
$request->ParentFolderIds->DistinguishedFolderId = new EWSType_DistinguishedFolderIdType();
$request->ParentFolderIds->DistinguishedFolderId->Id = EWSType_DistinguishedFolderIdNameType::INBOX;
$request->Traversal = new EWSType_FolderQueryTraversalType();
$result = $ews->FindFolder($request);
var_dump($result);
Only then I get this error:
Catchable fatal error: Object of class EWSType_FolderQueryTraversalType could not be converted to string
Is there anybody with experience with this class that can tell me what I'm doing wrong?
I do know that a string has to be passed, but it seems the class has just 3 constants without any functions or other properties..

I figured it out, in above example I had to use
$request->Traversal = EWSType_FolderQueryTraversalType::DEEP;
Since it only had the 3 constants.
But posting it here since I think it might be useful for anyone else looking to do the same, listing all mail in your inbox goes as follows:
$ews = new ExchangeWebServices("mailserver.domain.local", "user", "pass");
$request = new EWSType_FindItemType();
$request->ItemShape = new EWSType_ItemResponseShapeType();
$request->ItemShape->BaseShape = EWSType_DefaultShapeNamesType::DEFAULT_PROPERTIES;
$request->ParentFolderIds = new EWSType_NonEmptyArrayOfBaseFolderIdsType();
$request->ParentFolderIds->DistinguishedFolderId = new EWSType_DistinguishedFolderIdType();
$request->ParentFolderIds->DistinguishedFolderId->Id = EWSType_DistinguishedFolderIdNameType::INBOX;
$request->Traversal = EWSType_ItemQueryTraversalType::SHALLOW;
$result = $ews->FindItem($request);

Related

Why am I getting a The field type's enum value is invalid for this search message?

Here is the situation.
I'm trying to get the proper class name of the serialized inventory item, however, when I'm doing a search (see the code below):
$type = new SearchEnumMultiSelectField();
$type->operator = 'anyOf';
$type->searchValue = array('serializedInventoryItem');
$search->type = $type;
$invetoryRef = new RecordRef();
$invetoryRef->internalId = '522216';
$params = new SearchMultiSelectField();
$params->operator = 'anyOf';
$params->searchValue = array($invetoryRef);
$search->serializedInventoryItem = $params;
However, when I do a search, I'm getting the following message: The field type's enum value is invalid for this search.
Why would I be getting the following error message?
Thanks!
Kevin
Found a solution, instead of using the code above, I used the following code:
$service = new NetSuiteService();
$request = new GetRequest();
$request->baseRef = new RecordRef();
$request->baseRef->internalId = 522216;
$request->baseRef->type = 'serializedInventoryItem';
$getResponse = $service->get($request);
That resolved the issue.

PHP-EWS - Find Contacts in Contact Subfolders

how can I view contacs in self created "Contact Subfolders" with "PHP-EWS"?
With this Code:
$request = new FindItemType();
$request->ItemShape = new ItemResponseShapeType();
$request->ItemShape->BaseShape = DefaultShapeNamesType::ALL_PROPERTIES;
$request->ContactsView = new ContactsViewType();
$request->ContactsView->InitialName = 'a';
$request->ContactsView->FinalName = 'z';
$request->ParentFolderIds->DistinguishedFolderId = new DistinguishedFolderIdType();
$request->ParentFolderIds->DistinguishedFolderId->Id = DistinguishedFolderIdNameType::CONTACTS;
$request->Traversal = ItemQueryTraversalType::SHALLOW;
$response = $client->FindItem($request);
I can only view contacts in the "Contacts Root Folder" but no created Users in self created "Contact Subfolders".
How can i fix this? Please with a small example.
Thanks
DistinguishedFolderIdType means WellKnown in EWS. As your Folder is created by you...
$request = new FindItemType();
$request->ItemShape = new ItemResponseShapeType();
$request->ItemShape->BaseShape = DefaultShapeNamesType::ALL_PROPERTIES;
$request->ParentFolderIds = new NonEmptyArrayOfBaseFolderIdsType();
$request->ContactsView = new ContactsViewType();
$request->ParentFolderIds->FolderId = new FolderIdType();
$request->ParentFolderIds->FolderId->Id = $psFolderGuid;
$request->Traversal = ItemQueryTraversalType::SHALLOW;
so first get the $psFolderGuid /id

Not collected Exception: Wrong Version PHP-EWS

Im trying to connect with the PHP-EWS and my Exchange server.
I use the Script from https://github.com/jamesiarmes/php-ews/wiki
But everytime i load my script the browser tells me
Not collected Exception: Wrong Version
Here is my script (The Autoloader is in a extra File so dont worry it works)
$server = "***********";
$username="***********";
$password="*******";
$version= "2010"; // or Exchange 2010; Exchange 2010 SP1
$ews = new ExchangeWebServices($server, $username, $password, $version);
$request = new EWSType_FindFolderType();
$request->Traversal = EWSType_FolderQueryTraversalType::SHALLOW;
$request->FolderShape = new EWSType_FolderResponseShapeType();
$request->FolderShape->BaseShape = EWSType_DefaultShapeNamesType::ALL_PROPERTIES;
// configure the view
$request->IndexedPageFolderView = new EWSType_IndexedPageViewType();
$request->IndexedPageFolderView->BasePoint = 'Beginning';
$request->IndexedPageFolderView->Offset = 0;
// set the starting folder as the inbox
$request->ParentFolderIds = new EWSType_NonEmptyArrayOfBaseFolderIdsType();
$request->ParentFolderIds->DistinguishedFolderId = new EWSType_DistinguishedFolderIdType();
$request->ParentFolderIds->DistinguishedFolderId->Id = EWSType_DistinguishedFolderIdNameType::INBOX;
// make the actual call
$response = $ews->FindFolder($request);
?>
Does anybody know why i keep getting
Not collected Exception: Wrong Version
and knew what to do?
Your version is wrong, you should use one of the pre-defined constants in ExchangeWebServices.
E.g: $version = ExchangeWebServices::VERSION_2010
You should have a look at "ExchangeWebServices.php" in order to see which other versions are defined.

php-ews how to search calender event by Calender Subject

I am using https://github.com/jamesiarmes/php-ews for connecting php to exchange server so far no problem using the code below . i can connect to specific user mail box and can retrieve all his calender events . right now its pulling all the events what i want is to pull only those calendar events which has lets say 'Student Appt.' in Subject line.Is it possible . ?
require_once('bug/dBug.php');
require_once('EWSType.php');
require_once('ExchangeWebServices.php');
require_once('NTLMSoapClient.php');
require_once('NTLMSoapClient/Exchange.php');
require_once('EWS_Exception.php');
require_once('EWSType/FindItemType.php');
require_once('EWSType/ItemQueryTraversalType.php');
require_once('EWSType/ItemResponseShapeType.php');
require_once('EWSType/DefaultShapeNamesType.php');
require_once('EWSType/CalendarViewType.php');
require_once('EWSType/NonEmptyArrayOfBaseFolderIdsType.php');
require_once('EWSType/DistinguishedFolderIdType.php');
require_once('EWSType/DistinguishedFolderIdNameType.php');
require_once('EWSType/EmailAddressType.php');
require_once('EWSType/UserIdType.php');
require_once('EWSType/CalendarEventDetails.php');
$host = 'xxx';
$username = 'xxx';
$password = 'xxx';
$version = 'Exchange2010';
$start = " 2013-04-17T15:18:34+03:00";
$end = " 2013-04-30T15:18:34+03:00";
$ews = new ExchangeWebServices($host, $username, $password, $version);
//new dBug ($ews);
// Set init class
$request = new EWSType_FindItemType();
// Use this to search only the items in the parent directory in question or use ::SOFT_DELETED
// to identify "soft deleted" items, i.e. not visible and not in the trash can.
$request->Traversal = EWSType_ItemQueryTraversalType::SHALLOW;
// This identifies the set of properties to return in an item or folder response
$request->ItemShape = new EWSType_ItemResponseShapeType();
$request->ItemShape->BaseShape = EWSType_DefaultShapeNamesType::DEFAULT_PROPERTIES;
// Define the timeframe to load calendar items
$request->CalendarView = new EWSType_CalendarViewType();
$request->CalendarView->StartDate = $start ;// an ISO8601 date e.g. 2012-06-12T15:18:34+03:00
$request->CalendarView->EndDate = $end ; // an ISO8601 date later than the above
// Only look in the "calendars folder"
$request->ParentFolderIds = new EWSType_NonEmptyArrayOfBaseFolderIdsType();
$request->ParentFolderIds->DistinguishedFolderId = new EWSType_DistinguishedFolderIdType();
$request->ParentFolderIds->DistinguishedFolderId->Id = EWSType_DistinguishedFolderIdNameType::CALENDAR;
// if you want to get to someones folder
while($info = mysql_fetch_array( $call_pri_result )){
$EmailAddy = 'abc#exchangeserver.com';
$mailBox = new EWSType_EmailAddressType();
$mailBox->EmailAddress = $EmailAddy;
$request->ParentFolderIds->DistinguishedFolderId->Mailbox = $mailBox;
echo 'Now Looping for Consular ID '.$EmailAddy.'<br>' ;
// Send request
$response = $ews->FindItem($request);
// Loop through each item if event(s) were found in the timeframe specified
if ($response->ResponseMessages->FindItemResponseMessage->RootFolder->TotalItemsInView > 0){
$events = $response->ResponseMessages->FindItemResponseMessage->RootFolder->Items->CalendarItem;
foreach ($events as $event){
$id = $event->ItemId->Id;
$change_key = $event->ItemId->ChangeKey;
$start = $event->Start;
$end = $event->End;
$subject = $event->Subject;
//$location = $event->Location;
}
}
else {
// No items returned
}
}
From my digging you cannot add restrictions or sort order to a CalendarView. I added the code found here to my code to get calendar items and I got the following MessageText:
Restrictions and sort order may not be specified for a CalendarView.
It looks like the full list of FieldURI variables is listed at this MSDN page.
What I would do in your case is put a regular expression, strpos() or similar in your foreach() loop of events. Then if that case matches perform your function. Yes you will have extra events that you will do nothing with but you will at least be able to filter out your desired events.

How to connect to Exchange online API from PHP

I have a task at hand which requires me to connect Exchange Online account and list all the calendar entries in PHP.
I have read through many Microsoft help doc but it all refers to c# code. Can someone please guide me through steps to achieve this using PHP.
Try this:
$ews = new ExchangeWebServices($host, $username, $password);
$request = new EWSType_FindItemType();
$request->Traversal = EWSType_ItemQueryTraversalType::SHALLOW;
$request->ItemShape = new EWSType_ItemResponseShapeType();
$request->ItemShape->BaseShape =
EWSType_DefaultShapeNamesType::DEFAULT_PROPERTIES;
$request->CalendarView = new EWSType_CalendarViewType();
$request->CalendarView->StartDate = date('c', strtotime('01/01/2011 -00'));
$request->CalendarView->EndDate = date('c', strtotime('01/31/2011 -00'));
$request->ParentFolderIds = new EWSType_NonEmptyArrayOfBaseFolderIdsType();
$request->ParentFolderIds->DistinguishedFolderId =
new EWSType_DistinguishedFolderIdType();
$request->ParentFolderIds->DistinguishedFolderId->Id =
EWSType_DistinguishedFolderIdNameType::CALENDAR;
With this: https://github.com/jamesiarmes/php-ews

Categories