Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8026763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:35:45+00:00 2026-06-04T23:35:45+00:00

In my PHP script, I need to figure out how to retrieve all emails

  • 0

In my PHP script, I need to figure out how to retrieve all emails that are either after a specified message ID or after a specific date (Either will work, I just need to retrieve emails that are new since the last time I scraped the inbox).

This inbox is getting thousands of emails a day, and I can’t delete any emails for 30 days. For the initial import I was just doing an offset from the beginning of the inbox, but obviously that won’t work once we start cleaning out emails.

I think I have to set the $Restriction property of the class “EWSType_FindItemType“, but I don’t think the necessary classes exist in php-ews for me to do this. I’ve tried to add them myself, but I don’t understand enough about EWS or SOAP.

So far the only thing I’ve come up with is this:

$Request->Restriction = new EWSType_RestrictionType();
$Request->Restriction->IsGreaterThan = new stdClass;
$Request->Restriction->IsGreaterThan->FieldURIOrConstant = new stdClass;
$Request->Restriction->IsGreaterThan->FieldURIOrConstant->Constant = '2012-01-02T07:04:00Z';
$Request->Restriction->IsGreaterThan->FieldURI = new stdClass;
$Request->Restriction->IsGreaterThan->FieldURI->FieldURI = 'item:DateTimeReceived';

And that doesn’t work 🙁

Here’s the code I am currently using to retrieve email:

<?php
require( dirname( __FILE__ ) . '/ews/ExchangeWebServicesLoader.php' );

$ews = new ExchangeWebServices( EXCHANGE_HOSTNAME, EXCHANGE_USERNAME, EXCHANGE_PASSWORD, ExchangeWebServices::VERSION_2010_SP1 );

$Request = new EWSType_FindItemType();

$Request->ItemShape = new EWSType_ItemResponseShapeType();
$Request->ItemShape->BaseShape = EWSType_DefaultShapeNamesType::ALL_PROPERTIES;
$Request->ItemShape->BodyType = EWSType_BodyTypeResponseType::TEXT;
$Request->ItemShape->BodyTypeSpecified = true;

$Request->Traversal = EWSType_ItemQueryTraversalType::SHALLOW;

$Request->IndexedPageItemView = new EWSType_IndexedPageViewType();
$Request->IndexedPageItemView->MaxEntriesReturned = 25;
$Request->IndexedPageItemView->BasePoint = 'Beginning';
$Request->IndexedPageItemView->Offset = $offset;

$Request->ParentFolderIds = new EWSType_NonEmptyArrayOfBaseFolderIdsType();
$Request->ParentFolderIds->DistinguishedFolderId = new EWSType_DistinguishedFolderIdType();
$Request->ParentFolderIds->DistinguishedFolderId->Id = 'inbox';
$Request->ParentFolderIds->DistinguishedFolderId->Mailbox = new EWSType_EmailAddressType();
$Request->ParentFolderIds->DistinguishedFolderId->Mailbox->EmailAddress = 'sharedmailbox@company.org';

// sort order
$Request->SortOrder = new EWSType_NonEmptyArrayOfFieldOrdersType();
$Request->SortOrder->FieldOrder = array();
$order = new EWSType_FieldOrderType();
$order->FieldURI = new stdClass;
$order->FieldURI->FieldURI = 'item:DateTimeReceived';
$order->Order = 'Ascending';
$Request->SortOrder->FieldOrder[] = $order;

$response = $ews->FindItem($Request);
$items = $response->ResponseMessages->FindItemResponseMessage->RootFolder->Items->Message;

foreach ( $items as $item ) {
    // Do stuff
}

Any help would be greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:35:47+00:00Added an answer on June 4, 2026 at 11:35 pm

    Restriction are tricky in EWS, true. You can take a look at haw they are used in EWSWrapper, here’s example how to create AND restriction to get items in between date range:

    //create AND restrction
    $request->Restriction = new EWSType_RestrictionType();
    $request->Restriction->And = new EWSType_AndType();
    
    $request->Restriction->And->IsGreaterThanOrEqualTo = new EWSType_IsGreaterThanOrEqualToType();
    $request->Restriction->And->IsGreaterThanOrEqualTo->ExtendedFieldURI = new EWSType_PathToExtendedFieldType;
    $request->Restriction->And->IsGreaterThanOrEqualTo->ExtendedFieldURI->DistinguishedPropertySetId = "Task";
    $request->Restriction->And->IsGreaterThanOrEqualTo->ExtendedFieldURI->PropertyId = "33029";
    $request->Restriction->And->IsGreaterThanOrEqualTo->ExtendedFieldURI->PropertyType = "SystemTime";
    $request->Restriction->And->IsGreaterThanOrEqualTo->FieldURIOrConstant->Constant->Value = date('c', $start);
    
    $request->Restriction->And->IsLessThanOrEqualTo = new EWSType_IsLessThanOrEqualToType();
    $request->Restriction->And->IsLessThanOrEqualTo->ExtendedFieldURI = new EWSType_PathToExtendedFieldType;
    $request->Restriction->And->IsLessThanOrEqualTo->ExtendedFieldURI->DistinguishedPropertySetId = "Task";
    $request->Restriction->And->IsLessThanOrEqualTo->ExtendedFieldURI->PropertyId = "33029";
    $request->Restriction->And->IsLessThanOrEqualTo->ExtendedFieldURI->PropertyType = "SystemTime";
    $request->Restriction->And->IsLessThanOrEqualTo->FieldURIOrConstant->Constant->Value = date('c', $end);
    

    And the types used:

    class EWSType_RestrictionType extends EWSType {
    /**
     * SearchExpression property
     * 
     * @var EWSType_SearchExpressionType
     */
    public $SearchExpression;
    
    /**
     * Constructor
     */
    public function __construct() {
        $this->schema = array(
            array(
                'name' => 'SearchExpression',
                'required' => false,
                'type' => 'SearchExpressionType',
            ),
        ); // end $this->schema
    } // end function __construct()
    } // end class RestrictionType
    
    <?php
    
    class EWSType_AndType extends EWSType {
    /**
     * SearchExpression property
     * 
     * @var EWSType_MultipleOperandBooleanExpressionType
     */
    public $SearchExpression;
    
    /**
     * Constructor
     */
    public function __construct() {
        $this->schema = array(
            array(
                'name' => 'SearchExpression',
                'required' => false,
                'type' => 'MultipleOperandBooleanExpressionType',
            ),          
        ); // end $this->schema
    } // end function __construct()
    } // end class AndType
    class EWSType_IsLessThanOrEqualToType extends EWSType {
    /**
     * SearchExpression property
     * 
     * @var EWSType_TwoOperandExpressionType
     */
    public $SearchExpression;
    
    /**
     * Constructor
     */
    public function __construct() {
        $this->schema = array(
            array(
                'name' => 'SearchExpression',
                'required' => false,
                'type' => 'TwoOperandExpressionType',
            ),
        ); // end $this->schema
    } // end function __construct()
    } // end class IsLessThanOrEqualToType
    
    
    class EWSType_IsGreaterThanOrEqualToType extends EWSType {
    /**
     * SearchExpression property
     * 
     * @var EWSType_TwoOperandExpressionType
     */
    public $SearchExpression;
    
    /**
     * Constructor
     */
    public function __construct() {
        $this->schema = array(
            array(
                'name' => 'SearchExpression',
                'required' => false,
                'type' => 'TwoOperandExpressionType',
            ),
        ); // end $this->schema
    } // end function __construct()
    } // end class IsGreaterThanOrEqualToType
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the David Walsh PHP calendar script and need to format my date
I need to connect my php script to Gmail Atom feed to retrieve the
All the code/commands below are part of a PHP script that processes images from
I have a php script I need to run every 5 seconds (run, wait
i have a php script where i need to create a zip archive with
I have a PHP script with infinite loop. I need this script running forever.
I need a PHP script to decrypt the RSA encrypted string generated by this
I need a php script to read a .txt file. The content of the
I need to write a php script which is called by a setInterval( ajaxrequest('ftp.php',
I need to handle strings in my php script using regular expressions. But there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.