Pievis
Pievis

Reputation: 1994

How to consistently get / calculate Gmail sending limits

I'm trying to figure out a way to get how many more emails a gmail / gsuite account can still send in the next 24h. In short I would like to have a consistent way to get gmail sending quota.

From my understanding the gmail / gsuite quota for sending can be summarized with the following (excluding MailApp):

Using GmailApp in app script, limits are:

Using "raw" Gmail Apis, the limits are:

Considering that in my implementation in appscript I could use GmailApp or the advanced gmail service, how can I consistently get the remaining quota to showcase the user and eventually stop emails ?

I was following this two roads but with no results:

1) in appscript I can use MailApp.getRemainingDailyQuota() However from my tests it looks like that this method doesn't return the real value, but just a "last time computed" value that is refreshed in long intervals (took 8h in my test)

this is the code I've used:

function logRemainingQuota(e) {
var emailQuotaRemaining = MailApp.getRemainingDailyQuota();
Logger.log("Remaining email quota now: " + emailQuotaRemaining);
}

I tried to send emails via gmail and some addon via the same account and the quota never reduced immediately, just hours later.

2) calculate the value using gmail api messages.list By making the following call I can get the number of emails sent in the last 24h in the field resultSizeEstimate or by simply counting the messages.

curl \
  'https://www.googleapis.com/gmail/v1/users/me/messages?q=from%3Ame%20newer_than%3A1d%20in%3Aanywhere&fields=resultSizeEstimate&key=[YOUR_API_KEY]' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --compressed

the query is: from:me newer_than:1d in:anywhere However this value doesn't seem to be true and is returning way more data. Also, there's no way to distinguish between an gsuite account that is in trial and one that is not, which makes it difficult to apply the calculation in a reliable way.

Summing up, I've the following questions:

Sorry for the long post and thank you for helping !

---- EDIT ----

So, I've updated my code to perform testing on my script:

function sendWithGmailApp(e) {

  var now = new Date();
  GmailApp.sendEmail("[email protected]", "current time", "The time is: " + now.toString());

  Logger.log("Gmail App used ");

  var emailQuotaRemaining = MailApp.getRemainingDailyQuota();
  Logger.log("Remaining email quota now Gmail App: " + emailQuotaRemaining);
}

function sendWithGmailAPI(e) {
  var draft = GmailApp.getMessageById('17139fda2e54af59');
  var raw = draft.getRawContent();
  var message = Gmail.newMessage();
  Gmail.Users.Messages.send(message, "me", Utilities.newBlob(raw, "message/rfc822"));
  Logger.log("Gmail Api used ");

  var emailQuotaRemaining = MailApp.getRemainingDailyQuota();
  Logger.log("Remaining email quota now Gmail Api: " + emailQuotaRemaining);
}

function showquota(){
var emailQuotaRemaining = MailApp.getRemainingDailyQuota();
  Logger.log("Remaining email quota now Gmail App: " + emailQuotaRemaining);
}

I've also made a copy of the script in order to determinate if the quota is user based or user/script based. Here is my conclusions:

So now my question is,

1) is there a way I can predict the quota of a user via gmail API if my application uses the advanced service ?

2) using the advanced service, will also affect URLFetch quota since it is a wrapper basically ?

Upvotes: 2

Views: 1477

Answers (1)

Andres Duarte
Andres Duarte

Reputation: 3340

The method MailApp.getRemainingDailyQuota() will return the remaining quota related to the use of MailApp and GmailApp, whose limits are stated here.

When making a messages.list the response includes messages from hangouts as well, this ones come with the "CHAT" label. In any case, you could filter the sent messages by the "SENT" label.

You may try to find a workaround using the messages.list method, the problem is you don't know at what time the quota for each user is being refreshed, as stated here:

Limits per day are applied over a rolling 24-hour period, not a set time of day.

In case you're going to try to develop some kind of close workaround, the Properties class that can be used to store values for each user using the script and time-based triggers , may be useful for you.

About your Advance Service question, their quotas are not specified in the documentation, but it makes sense to be limited by the URLFecthApp limits as both are treated as external APIs requests. Also, be aware that Gmail API has its own set of usage limits.

Upvotes: 3

Related Questions