Skip to content

Instantly share code, notes, and snippets.

@schlos
Forked from pawurb/gmailDelayed.js
Created June 16, 2017 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schlos/652f4ce1251c6f8f68866478e85a38fa to your computer and use it in GitHub Desktop.
Save schlos/652f4ce1251c6f8f68866478e85a38fa to your computer and use it in GitHub Desktop.
Fighting email checking addiction ...
//TODO
//https://developers.google.com/apps-script/managing_triggers_programmatically
function moveDelayedMessages() {
receiveEmails("Delayed Messages");
}
// 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);
// Check that the message is not smart-labeled 'Bulk' before moving to the inbox
var labels = thread.getLabels();
var isBulk = false;
labels = thread.getLabels();
for (var j = 0; j < labels.length; j++) {
var l = labels[j];
var name = l.getName();
if ( name == "Bulk" || name == "Promotions" || name == "Social Updates" ) {
isBulk = true
}
}
if (!isBulk) {
thread.moveToInbox();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment