Skip to content

Instantly share code, notes, and snippets.

@KainokiKaede
Last active May 2, 2018 07:52
Show Gist options
  • Save KainokiKaede/2d175ed860ba22529fa011a3bd6c9d43 to your computer and use it in GitHub Desktop.
Save KainokiKaede/2d175ed860ba22529fa011a3bd6c9d43 to your computer and use it in GitHub Desktop.
Automatically sends Gmail drafts with specific label.
/*
* Inspired by: http://stackoverflow.com/a/27215474
*
* After copying the code to your own script, open Resources -> Advanced Google Services,
* 1. enable Gmail API (switch to "on"),
* 2. open the link for the Google Developers Console, and enable the Gmail API for your project.
*/
function sendMorningDrafts() {
sendLabeledDrafts("schedule/send-next-0700-0800");
}
function sendEveningDrafts() {
sendLabeledDrafts("schedule/send-next-1800-1900");
}
function sendLabeledDrafts(label_to_send){
var drafts = GmailApp.getDrafts();
if (!drafts) { return; } // No draft exists.
for (var i=0; i<drafts.length; i++) {
var draft = drafts[i];
var thread = draft.getMessage().getThread();
var sent_flag = false;
var labels = thread.getLabels();
for (var j=0; j<labels.length; j++) {
var label = labels[j];
if (label.getName() === label_to_send) {
var response = draft.send();
var sent_flag = true;
var new_thread = response.getThread();
new_thread.removeLabel(label); // remove label not to send the same message again.
}
if (sent_flag) { break; }
}
if (sent_flag) { continue; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment