Skip to content

Instantly share code, notes, and snippets.

@schlos
Forked from donalod/DelayGmail
Created June 16, 2017 14:47
Show Gist options
  • Save schlos/6c7568abf5bb0d6542ad7ce6ee6f234e to your computer and use it in GitHub Desktop.
Save schlos/6c7568abf5bb0d6542ad7ce6ee6f234e to your computer and use it in GitHub Desktop.
GoogleAppScript_DelayedEmail
function moveDelayedMessages() {
receiveEmails("Delayed Messages");
Utilities.sleep(60000);
reArchive();
}
// receiveEmails() will take all threads in labelname (Default: 'Delayed messages')
// and place them into the mailbox. The intent is for a delayed retrieval of
// messages when combined with a script timer.
function receiveEmails(labelName) {
var runTime = new Date();
Logger.log("Merging emails at "+runTime.toLocaleTimeString());
if (labelName == null) {
// The default label to divert messages into
labelName = "Delayed Messages";
}
// Access the messages under that label
var label = GmailApp.getUserLabelByName(labelName);
// If the label doesn't exist, then create it
if (label == null) {
label = GmailApp.createLabel(labelName);
}
// Move each thread in this label into the inbox
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++)
{
var thread = threads[i];
// Remove thread from Delayed Messages label
thread.removeLabel(label);
// Move to inbox
thread.moveToInbox();
}
}
// Place the list of labels here in your search that you already archive so they don't come back to the inbox after moving back the delayed ones!
// I'm sure there's a more advanced and prettier way to do this in the logic for retrieving the "Delayed Messages"labe above. This is quick/dirty.
function reArchive () {
var rearchivethreads = GmailApp.search('newer_than:4d AND label:nerds OR label:noc OR label:orders OR label:telco-ops OR label:dublin.social OR label:didata OR label:reachability is:unread');
for (var i = 0; i < rearchivethreads.length; i++) {
var thread = rearchivethreads[i];
thread.moveToArchive();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment