Last active
May 2, 2018 07:52
-
-
Save KainokiKaede/2d175ed860ba22529fa011a3bd6c9d43 to your computer and use it in GitHub Desktop.
Automatically sends Gmail drafts with specific label.
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
/* | |
* 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