Reputation: 187
I'm attempting to test a script that I'm working on. The script is fine, it executes successfully when I trigger it manually. When I add a time driven script of every minute interval the scrips starts throwing an exception after couple of hrs .
Exception: Service invoked too many times for one day: gmail
I checked the daily quota of email and found that i still have mail quota left
var quota = MailApp.getRemainingDailyQuota();
Logger.log(quota);
Also I am able to receive the try catch email but the mails are not forwarded .
Is this because of the execution time quota associated with the trigger? Below is the code
function MailForward() {
try{
var glabel = createLabel_("Mail-Forwarded");
var rtm_email = '[email protected]';
var from_email = Session.getActiveUser().getEmail();
var threads = GmailApp.search('in:inbox is:unread newer_than:1d');
var mForward = 0;
for (var i=0;i<threads.length;i++) {
var messages=threads[i].getMessages();
for (var m = 0; m < messages.length; m++){
if (messages[m].isUnread()){
mForward = 0;
var mlabels = threads[i].getLabels();
for (var j = 0; j < mlabels.length; j++) {
Logger.log(mlabels[j].getName());
if (mlabels[j].getName() === "Mail-Forwarded") {
mForward = 1;
}
}
if (mForward===0) {
// Logger.log(messages.length)
// Logger.log(messages[m].getFrom());
var from = messages[m].getFrom();
//Logger.log(messages[m].getDate());
var date = messages[m].getDate();
// Logger.log(messages[m].getSubject());
var subject = messages[m].getSubject();
// Logger.log(messages[m].getTo());
var to = messages[m].getTo();
var body = messages[m].getBody();
var attachment = messages[m].getAttachments();
var emailoptions = ("---------- Forwarded message ----------" +'<br>'+'From: '+from+ "<'" + from.replace(/^.+<([^>]+)>$/, "$1") +"'>"+'<br>'+ 'Date: '+date+'<br>'+ 'Subject: ' +subject+'<br>'+
'To: ' +to+ "<'" + to.replace(/^.+<([^>]+)>$/, "$1") +"'>"+'<br>'+'<br>'+'<br>');
messages[m].forward(rtm_email,{htmlBody: emailoptions + body , Attachment: attachment});
glabel.addToThread(threads[i]);
Logger.log(glabel.getName());
messages[m].markRead();
mForward = 1;
}
}
}
}
} catch(e) {
MailApp.sendEmail("[email protected]", "Exception found in Sript", e );
Logger.log(e);
}
}
Upvotes: 2
Views: 353
Reputation:
You checked quota using MailApp.getRemainingDailyQuota();
not GmailApp
. These are two different services.
The quota method returns only "the number of remaining emails a user can send for the rest of the day." But the limit you are hitting is the number of times you invoked the service, for whatever purpose.
You are using GmailApp
a lot to access existing messages, not so much to send new ones. In particular, you are checking every message in every thread from today, and do it every minute. That's a lot of API calls: getMessages
, isUnread
, etc.
One way to reduce the number of API calls is to have more targeted search. The after:
search parameter accepts Unix timestamp, which makes it possible to do the following:
function doSomethingWithNewEmail() {
var interval = 5; // if the script runs every 5 minutes; change to 1 if it runs every minute
var date = new Date();
var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval;
var threads = GmailApp.search('is:inbox after:' + timeFrom);
for (var i = 0; i < threads.length; i++) {
// do something
}
}
I successfully used the above approach with 5 minute interval. It may work with 1 minute too, since most of the time search
will be the only API call made by the script.
Upvotes: 1