pb_
pb_

Reputation: 492

GMail API: Fastest way to get the oldest email in inbox

I want to get the oldest email in GMail inbox. How can I do that with the minimum number of API calls?

One way to do it could be to get the total number of emails from Users:getProfile API and then use User.messages:list API to get the last page using "pageToken" query parameter by using the formula

pageToken = totalMessages/50

to get the list of messages from the last page of my inbox and then using the mid of the last message in the list to fetch the oldest email.

I am just wondering if there is a better way to do this? I could not figure out other ways to do it from the documentation. Any search filter that will fetch me the oldest email?

Upvotes: 4

Views: 2213

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117016

User.messages.list contains a paramter called q which can be used for searching.

https://www.googleapis.com/gmail/v1/users/userId/messagesq=before%3A2008%2F01%2F01&access_token={token}

Basicly before:2008/01/01 is sent just like searching in the gmail web app return all the emails before that date.

The trick here will be narrowing down your search. Gmail was released on 2004. Which gives yo possible start between 2018 and 2004 split it in half

before:2011/01/01  (there were mails split it in half again) 
before:2007/01/01  (there were no mails must be between 11 and 7)
before:2009/01/01  (...)

There is a term for this method of searching but i cant remember it right now. Basically you keep dividing it by two and finding out where the result must be.

Upvotes: 2

Related Questions