Skip to content

Instantly share code, notes, and snippets.

@isotopp
Last active June 26, 2017 14:41
Show Gist options
  • Save isotopp/81e4baead16eb5e0bb99b0c8e8f9623e to your computer and use it in GitHub Desktop.
Save isotopp/81e4baead16eb5e0bb99b0c8e8f9623e to your computer and use it in GitHub Desktop.
function filterByHeader(search, hdr_regex, labelname) {
var label = GmailApp.getUserLabelByName(labelname);
var cnt = 0;
var threads = GmailApp.search(search);
for (var j=0; j<threads.length; j++) {
thd = threads[j];
msgs = thd.getMessages();
for (var i=0; i< msgs.length; i++) {
var m = msgs[i];
var raw = m.getRawContent();
var headers = raw.substring(0, raw.indexOf("\r\n\r\n"));
if (headers.search(hdr_regex) >= 0) {
cnt++;
thd.markUnimportant();
thd.markRead();
thd.moveToArchive();
thd.addLabel(label);
}
}
}
return cnt;
}
function hasParameter(e, key, def_value) {
if (e.parameter[key] && typeof(e.parameter[key]) == "string") {
return e.parameter[key];
} else{
return def_value;
}
}
function doGet(e) {
var search = "is:unread label:inbox newer_than:1d";
var hdr_regex = /^X-Trigger:/m;
var labelname = "kris-test";
return filterByHeader(search, hdr_regex, labelname);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment