Search code examples
pythongmailgmail-api

Is it possible to get gmail from a time interval using Gmail API?


For example, I want to get all emails this week, or all emails from Monday to Friday. Will that be possible? And I'm using python Gmail API.


Solution

  • You can set a query on your listing of messages and use newer_than:

    ListMessagesResponse response = service.users().messages().list('me').setQ('newer_than:7d').execute();
    

    You can also use after and before with time since the epoch in seconds. If you would want e.g. the messages between Mon, 04 Sep 2017 00:00:00 GMT and Fri, 08 Sep 2017 23:59:59 GMT you would write:

    ListMessagesResponse response = service.users().messages().list('me').setQ('after:1504483200 before:1504915199').execute();