1

Information

I have been running this code for like a year and everything was working fine until yesterday. See error message below. The code does fetch only the numbers of unread emails in the Inbox. Of the number increases it will also play a sound on the client who is running the code. My Exchange-server is running Exchange Server 2013 CU12 but it has always been working with ExchangeWebServices::VERSION_2010 (see code bolow).

Error message

Fatal error: Uncaught SoapFault exception: [Client] Class 'EWS_Exception' not found in /var/www/html/php-ews/NTLMSoapClient.php:87 Stack trace: #0 /var/www/html/php-ews/NTLMSoapClient.php(87): NTLMSoapClient::__doRequest() #1 [internal function]: NTLMSoapClient->__doRequest('https://mail.se...', 'http://schemas....', 1, 0) #2 /var/www/html/php-ews/ExchangeWebServices.php(552): SoapClient->__call('FindItem', Array) #3 /var/www/html/php-ews/ExchangeWebServices.php(552): NTLMSoapClient_Exchange->FindItem(Object(EWSType_FindItemType)) #4 /var/www/html/mail.php(104): ExchangeWebServices->FindItem(Object(EWSType_FindItemType)) #5 {main} thrown in /var/www/html/php-ews/NTLMSoapClient.php on line 87

My code is based on the one found here: http://litphp.info/want_to_print_unread_mail_body_and_subject_using_ews_from_exchange_server_in_php

My code

function __autoload($class_name)
{
    // Start from the base path and determine the location from the class name,
    $base_path = 'php-ews';
    $include_file = $base_path . '/' . str_replace('_', '/', $class_name) . '.php';

    return (file_exists($include_file) ? require_once $include_file : false);
}

$ews = new ExchangeWebServices("mail.server.path.com", $mailuser, $mailpass, 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->Mailbox = new StdClass;
$request->ParentFolderIds->DistinguishedFolderId->Mailbox->EmailAddress = 'emailaddress@test.com';
$request->ParentFolderIds->DistinguishedFolderId->Id = EWSType_DistinguishedFolderIdNameType::INBOX;

$request->Traversal = EWSType_ItemQueryTraversalType::SHALLOW;

$result = new EWSType_FindItemResponseMessageType();
$result = $ews->FindItem($request);

if ($result->ResponseMessages->FindItemResponseMessage->ResponseCode == 'NoError' && $result->ResponseMessages->FindItemResponseMessage->ResponseClass == 'Success'){

    // Need this variable to check for the new value.
    $count_new = $result->ResponseMessages->FindItemResponseMessage->RootFolder->TotalItemsInView;

    // Play sound if value has increased.
    if($_SESSION['count_previous'] < $count_new) {
        echo '<script type="text/javascript">play_sound();</script>';
    }

    // Saving the value for later usage.
    $count = $result->ResponseMessages->FindItemResponseMessage->RootFolder->TotalItemsInView;

    // Saving the current count to a session. Need it to compair with the new value ($count_new).
    $_SESSION['count_previous'] = $count;

    if($count > 0) {
        echo '<script type="text/javascript">document.body.style.backgroundColor = "#fc2828";</script>';
        echo "<h1>" . $count . "</h1><br>";
        echo '<img src="img/mail.png" height="165px">';
    } else {
        echo '<h1 class="paddingH12 test">No mail</h1>';
    }

}

After a while I thought it could be that the code stopped working "like" VERSION_2010 since the server is 2013 CU12. I tried the following solution but it didn't worked: https://github.com/jamesiarmes/php-ews/issues/195

Does anyone know how this issue could be solved? It's a bit frustrating since the code worked for like a year and now it doesn't. Nothing was changed on the server side.

Thanks in advance.

Treps
  • 780
  • 3
  • 12
  • 28
  • 2
    I would highly suggest you move away from his library, it's old and unmaintained. I'd suggest trying out my fork at http://github.com/garethp/php-ews, which includes a simple example on how to get unread emails easily enough – Gareth Parker Apr 29 '16 at 13:19
  • Aha, thanks a lot mate. I'll check it out. – Treps Apr 29 '16 at 13:34

1 Answers1

0

So sorry for this post. It's now working again. I don't know what the issue was. Probably high load on the Exchange server or the network. Feel free to use this code to only get the unread emails.

I'm also refreshing the page after 60 seconds (otherwise the sounds doesn't work):

<meta http-equiv="refresh" content="60">

JavaScript for sound (in the header):

<script type="text/javascript">
    function play_sound() {
        var audioElement = document.createElement('audio');
        audioElement.setAttribute('src', '/sound/mail.mp3');
        audioElement.setAttribute('autoplay', 'autoplay');
        audioElement.load();
        audioElement.play();
    }
</script>
Treps
  • 780
  • 3
  • 12
  • 28
  • The code is working for Exchange 2013, 2013 CU11 and 2013 CU12. Even if it says 2010. ;) – Treps Apr 29 '16 at 10:18