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:
- 1500 recipients per day for GSuite accounts
- 100 to 500 recipients per day for GSuite accounts in trial
- 100 recipients per day Gmail accounts
Using "raw" Gmail Apis, the limits are:
- 2000 messages per day for GSuite accounts
- 500 messages per day for GSuite accounts in trial
- 500 recipients per day Gmail accounts
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:
- is there a way that can be used to calculate / get the number of remaining quota for sending messages (or recipients) with gmail (much better if in app script) ?
- is there a way to understand if a gsuite account is in trial ?
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("example@gmail.com", "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:
- MailApp.getRemainingDailyQuota() works for emails sent via GmailApp
- quota is related to GmailApp / MailApp and is user based (so other scripts can consume the quota)
- using GmailApi (advanced service) will not consume this quota, so It falls back in the standard gmail quota.
- I've made my tests in the legacy GAE engine and it works
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 ?