Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Make Gmail arrive on a schedule
var LABEL_NAME = 'inbox-holding'
// Each time must have the format 'HOUR:MINUTE'
var schedule = [['13:00', '17:00'], // Sunday
['9:30', '13:00', '17:00'], // Monday
['9:30', '13:00', '17:00'], // Tuesday
['9:30', '13:00', '17:00'], // Wednesday
['9:30', '13:00', '17:00'], // Thursday
['9:30', '13:00', '17:00'], // Friday
['8:00', '13:00', '17:00']]; // Saturday
function setup() {
// Create a label to put paused messages in
// First go through all the labels and check that it doesn't already exist
var label = null;
allLabels = Gmail.Users.Labels.list('me');
for (var i = 0; i < allLabels.labels.length; i++) {
if(allLabels.labels[i].name == LABEL_NAME){
label = allLabels.labels[i];
break; // Found it, just quit now
}
}
if(label == null){ // Didn't find it, so create it
label = Gmail.Users.Labels.create({'name':LABEL_NAME,
'labelListVisibility':'labelHide'},
'me');
}
// TODO: check if filter already exists
// Add a filter that automatically puts all incoming messages into this label
// and archives them so they don't appear in the inbox
var filter = Gmail.Users.Settings.Filters.create({'id':'schedule_inbox_filter',
'criteria':{
'from':'*',
'query':'label:inbox'
},
'action':{
'addLabelIds':[label.id],
'removeLabelIds':['INBOX']
}},
'me');
ScriptApp.newTrigger("check")
.timeBased()
.everyMinutes(10)
.create();
}
function check() {
var now = new Date();
var schedToday = schedule[now.getDay()];
for(var i = 0; i < schedToday.length; i++){ // For each of today's deliveries,
hour = Number(schedToday[i].split(":")[0]); // parse the time
minute = Number(schedToday[i].split(":")[1]);
target = new Date(now);
target.setHours(hour, minute);
// Deliver if we're within 12 minutes of the desired time
// This ensures we always get at least one delivery if we run every 10 minutes plus or minus a few seconds
// TODO: we might also sometimes get 2; add logic to fix this
if(target - now > -12*60*1000 && target - now < 0){
deliver();
}
}
}
// Deliver mail stored in the special label to the inbox
// Scheduled for specific times of day, and will occur +/- 15 minutes of that time
function deliver() {
// Find the label
allLabels = Gmail.Users.Labels.list('me');
var label = null;
for (var i = 0; i < allLabels.labels.length; i++) {
if(allLabels.labels[i].name == LABEL_NAME){
label = allLabels.labels[i];
break;
}
}
if(label != null){
// Get all the threads with the label
allThreads = Gmail.Users.Threads.list('me', { 'labelIds':[label.id] } );
// If there are no messages, we're done
if(allThreads.resultSizeEstimate == 0){
return;
}
Logger.log(new Date() + ": " + allThreads.threads.length + " messages delivered");
// Now move each of these threads to the inbox and remove the label
for (var i = 0; i < allThreads.threads.length; i++) {
Gmail.Users.Threads.modify({'addLabelIds': ['INBOX'],
'removeLabelIds': [label.id]},
'me', allThreads.threads[i].id);
}
}
else{
Logger.log("Couldn't find scheduled inbox label!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.