Gian Ferreira
Gian Ferreira

Reputation: 37

How do I get the remaining daily mail quota of google apps scripts service to work consistently?

I did a project on google apps scripts to send automatic emails after a form response was sent. However, when I check the daily quota of remaining emails with the MailApp.getRemainingDailyQuota() method, the quota responses vary with each script execution.

So I created another project just to test the quota, using the MailApp.getRemainingDailyQuota() method and even so, the quota response varied with each execution.


Code used to test:

function testeDeCota() { 
  let cota;

  cota = MailApp.getRemainingDailyQuota();
  Logger.log("Cota de emails restantes: " + cota);

  cota = MailApp.getRemainingDailyQuota();
  Logger.log("Cota de emails restantes: " + cota);

}

I am using a workspace account that has a quota of 1500 emails/day.

This is a screeshot of my executions. Note that there are 3 consecutive executions and without sending any email, even so the quota responses varied. The responses were:

1394; 1399; 1390.

Whenever I try to send an email or get the quota information the number just varies randomly.

printscreen of the script executions

Upvotes: 2

Views: 1544

Answers (1)

Iamblichus
Iamblichus

Reputation: 19309

This seems to be intended behavior.

Apart from the older issue Marios mentioned (Gmail SendEmail Quota - Decrementing Bug/issue), this was reported recently in Issue Tracker, and closed as Intended Behaviour:

See comments #6 and #8:

This small fluctuation in the value returned by this method is to be expected.

This behaviour is caused by how quotas are internally handled by Apps Script. It's expected behaviour.

Different executions vs Loop:

Interestingly, this discrepancy can only be seen when calling this method in separate invocations. When calling this method in a loop instead, no fluctuation is shown:

for(i=1; i<=n; i++) {
  SpreadsheetApp.openById(documentID).getSheetByName(sheetName).appendRow([new Date(), MailApp.getRemainingDailyQuota()]);
  Utilities.sleep(1000);
}

File a feature request:

As suggested in the referenced issue, if this is impacting your workflow somehow, you could consider filing a feature request.

Upvotes: 3

Related Questions