Skip to content

Instantly share code, notes, and snippets.

@esseti
Last active August 29, 2015 13:56
Show Gist options
  • Save esseti/9304343 to your computer and use it in GitHub Desktop.
Save esseti/9304343 to your computer and use it in GitHub Desktop.
//labels: change the names accordingly to your needs.
//those labels have to be in the gmail.
var l_morning = GmailApp.getUserLabelByName("Delay/Morning");
var l_midday = GmailApp.getUserLabelByName("Delay/Midday");
var l_tonight = GmailApp.getUserLabelByName("Delay/Tonight");
var l_tomorrow = GmailApp.getUserLabelByName("Delay/Tomorrow");
var l_nextweek = GmailApp.getUserLabelByName("Delay/Nextweek");
var me = "YOUR EMAIL GOES HERE";
//array of all the labels: DO IT
var labels=[l_midday,l_tonight,l_tomorrow,l_nextweek];
//archive the labeled mails and mars them as read.
function markMailAsRead(){
//for all the labels
for(var i=0;i<labels.length;i++){
var l=labels[i];
var threads = l.getThreads();
//mark them as read and move to archive
if (threads.length>0){
GmailApp.markThreadsRead(threads);
GmailApp.moveThreadsToArchive(threads);
}
}
}
//move emails to inbox and make them as unread.
function inbox(threads, label){
//if there are threads
if (threads.length>0){
GmailApp.moveThreadsToInbox(threads);
//for all the threads
for(var i=0;i<threads.length;i++){
//get all the messages
var messages = threads[i].getMessages();
//get the last message
var lastM = messages[messages.length-1];
if (lastM.getFrom().indexOf(me)>=0)
lastM = messages[messages.length-2];
//mark only the last as unread.
GmailApp.markMessageUnread(lastM);
}
//remove the delay label from the threads.
label.removeFromThreads(threads);
}
}
//move the email from a label to another label
function move(label_from, label_to){
threads = label_from.getThreads();
//if there are threads
if (threads.length>0){
label_from.removeFromThreads(threads);
label_to.addToThreads(threads);
}
}
//at 08am:put email in inbox (from midday)
function morining(){
label = l_morning;
threads = label.getThreads();
inbox(threads,label);
}
//at midday:put email in inbox (from midday)
function midday(){
label = l_midday;
threads = label.getThreads();
inbox(threads,label);
}
//at 4pm: put email in inbox (from tonight)
function tonight(){
label = l_tonight;
threads = label.getThreads();
inbox(threads,label);
}
//everyday (at 00) move the mail from tomorrow to morning
function tomorrow(){
label_from = l_tomorrow;
label_to = l_morning;
move(label_from, label_to);
}
//on sunday at 7AM: move email from nextweek to tomorrow.
function nextweek(){
label_from = l_nextweek;
label_to = l_tomorrow;
move(label_from, label_to);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment