Last active
August 3, 2017 09:28
-
-
Save ongspxm/f8aa5c5874938d115e3c2dd53d553f94 to your computer and use it in GitHub Desktop.
gmail scheduler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var label = 'schedule'; | |
function dpad(num){ | |
if(num<10){ | |
return "0"+num; | |
}else{ | |
return num; | |
} | |
} | |
function gmail_scheduler() { | |
// date2string | |
var date = new Date(); | |
var dstr = ''; | |
dstr += date.getFullYear(); | |
dstr += dpad(date.getMonth()+1); | |
dstr += dpad(date.getDate()) + '.'; | |
dstr += dpad(date.getHours()); | |
dstr += dpad(date.getMinutes()); | |
// getting the threads | |
var threads = GmailApp.search('in:drafts label:'+label); | |
var tlabel = GmailApp.getUserLabelByName(label); | |
for(var i=0; i<threads.length; i++){ | |
var msgs = threads[i].getMessages(); | |
var unsent = 0; | |
for(var j=0; j<msgs.length; j++){ | |
var msg = msgs[j]; | |
if(msg.isDraft()){ | |
unsent += 1; | |
var sub = msg.getSubject(); | |
if(dstr>sub){ | |
sub = sub.split(' ').slice(1).join(' '); | |
// cannot send, so we forward the message instead | |
msg.forward(msg.getTo(), { | |
cc: msg.getCc(), | |
bcc: msg.getBcc(), | |
subject: sub | |
}); | |
// fix subject error when reply received | |
Gmail.Users.Messages.remove('me', msg.getId()); | |
} | |
} | |
} | |
if(unsent==0){ | |
threads[i].removeLabel(tlabel); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment