title: How to consistently get / calculate Gmail sending limits
body: <p>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.</p>
  
  <p>From my understanding the gmail / gsuite quota for sending can be summarized with the following (excluding MailApp):</p>
  
  <p>Using GmailApp in app script, <a href="https://developers.google.com/apps-script/guides/services/quotas" rel="nofollow noreferrer">limits are</a>:</p>
  
  <ul>
  <li>1500 recipients per day for <strong>GSuite accounts</strong> </li>
  <li>100 to 500 recipients per day for GSuite accounts in <strong>trial</strong></li>
  <li>100 recipients per day <strong>Gmail accounts</strong></li>
  </ul>
  
  <p>Using "raw" Gmail Apis, the <a href="https://support.google.com/a/answer/166852" rel="nofollow noreferrer">limits are</a>: </p>
  
  <ul>
  <li>2000 messages per day for <strong>GSuite accounts</strong> </li>
  <li>500 messages per day for GSuite accounts in <strong>trial</strong></li>
  <li>500 recipients per day <strong>Gmail accounts</strong></li>
  </ul>
  
  <p>Considering that in my implementation in appscript I could use GmailApp or the <a href="https://developers.google.com/apps-script/advanced/gmail" rel="nofollow noreferrer">advanced gmail service</a>, how can I consistently get the remaining quota to showcase the user and eventually stop emails ?</p>
  
  <p>I was following this two roads but with no results: </p>
  
  <p>1) in appscript I can use <a href="https://developers.google.com/apps-script/reference/mail/mail-app#getremainingdailyquota" rel="nofollow noreferrer">MailApp.getRemainingDailyQuota()</a>
  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)</p>
  
  <p>this is the code I've used:</p>
  
  <pre><code>function logRemainingQuota(e) {
  var emailQuotaRemaining = MailApp.getRemainingDailyQuota();
  Logger.log("Remaining email quota now: " + emailQuotaRemaining);
  }
  </code></pre>
  
  <p>I tried to send emails via gmail and some addon via the same account and the quota never reduced immediately, just hours later.</p>
  
  <p>2) calculate the value using gmail api <a href="https://developers.google.com/gmail/api/v1/reference/users/messages/list" rel="nofollow noreferrer">messages.list</a>
  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.</p>
  
  <pre><code>curl \
    'https://www.googleapis.com/gmail/v1/users/me/messages?q=from%3Ame%20newer_than%3A1d%20in%3Aanywhere&amp;fields=resultSizeEstimate&amp;key=[YOUR_API_KEY]' \
    --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
    --header 'Accept: application/json' \
    --compressed
  </code></pre>
  
  <p>the query is: <code>from:me newer_than:1d in:anywhere</code>
  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.</p>
  
  <p>Summing up, I've the following questions:</p>
  
  <ul>
  <li>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) ?</li>
  <li>is there a way to understand if a gsuite account is in trial ?</li>
  </ul>
  
  <p>Sorry for the long post and thank you for helping !</p>
  
  <p>---- <strong>EDIT</strong> ----</p>
  
  <p>So, I've updated my code to perform testing on my script: </p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>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:</p>
  
  <ul>
  <li>MailApp.getRemainingDailyQuota() works for emails sent via GmailApp</li>
  <li>quota is related to GmailApp / MailApp and is user based (so other scripts can consume the quota)</li>
  <li>using GmailApi (advanced service) will not consume this quota, so It falls back in the <a href="https://support.google.com/a/answer/166852" rel="nofollow noreferrer">standard gmail quota</a>.</li>
  <li>I've made my tests in the legacy GAE engine and it works</li>
  </ul>
  
  <p>So now my question is, </p>
  
  <p>1) is there a way I can predict the quota of a user via gmail API if my application uses the advanced service ?</p>
  
  <p>2) using the advanced service, will also affect URLFetch quota since it is a wrapper basically ?</p>