Get last mail from Gmail

I want to get the last mail from my Gmail mailbox. I’m not a newbie on Make, I’m trying to optimize the task.

Let’s imagine I have 10’000 mails. and I only want to get the last one.

Method 1 (takes long time, but 1 operation) : The email ‘get emails’ module gets all mails, starting from the OLDEST to most RECENT, and then sorts them by date. example : Limit 100 → It’ll get 100 oldest mails, and then put the most recent in this list as Bundle 1. I’ll have to search Limit 10’000 → Then I’ll have the most recent mail as Bundle 1 → Mission accomplished.

Method 2 (doesn’t take long, 1 operation, but doesn’t work 100% of the time) : limit 10000, with query ‘newer_than=30d’. This will effectively get all mails from last month and get me the last one as Bundle 1. Takes less time, because I don’t actually have 10k mail last month. Problem : I might have last mail dating 3 months ago, and ‘Last mail’ won’t show up. I’d be forced to use repeater function to go back 30d more, and then search again, and again, until i get at least 1 bundle (this consumes time and operations, worse than method 1).

Anyone has a better method to get just ‘Last mail’ ? I don’t want to use ‘Watch mail’ module. I have also tried gmail API to search through 10k mails faster, it’s very fast, but I’m having a hard time working with some parameters, UID is not the same as ID, etc.

Thanks !

Resolved :

Multiple API call and 2 iterators just to reach the target… Much faster than fetching all mails with email module, but consumes operations. This way I’m also sure that I’m getting the last mail and it’s pretty flexible.

get last mail in this label.json (136.2 KB)

Optimized solution (http mainly) with 3 operations :

get last mail in this label (optimized).json (27.8 KB)

Optimization 2 : We only needed a last date from the mail, so instead of reusing the “get email” native module, I added parameters to second HTTP URL to only include Header called “Date”.

URL : https://gmail.googleapis.com/gmail/v1/users/me/messages/1***d4?format=metadata&metadataHeaders=Date

P.S. Needs some formatting to get proper date though. Formula is

{{formatDate(addHours(parseDate(trim(first(split(25.data.payload.headers.value; “+”))); “ddd, D MMM YYYY HH:mm:ss”); 1); “DD/MM/YYYY HH:mm”)}}

1 Like