Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Auto-management of receipts and shipping notifications
function handleStaged() {
Logger.log("Starting...")
handleStagedShipping();
handleStagedReceipts();
Logger.log("Done.")
}
function handleStagedReceipts() {
var getLabel = GmailApp.getUserLabelByName('_Stage/Receipts');
var setLabel = GmailApp.getUserLabelByName('Receipts');
var recipient = 'track@junecloud.com';
var threads = getLabel.getThreads();
var forwarded = 0;
for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
var message = thread.getMessages()[0];
var from = message.getFrom();
Logger.log("From: %s", from)
if (from.indexOf('auto-confirm@amazon.com') > 0) {
message.forward(recipient);
forwarded++;
}
thread.addLabel(setLabel);
thread.removeLabel(getLabel);
}
Logger.log("%s receipts handled, %s forwarded", threads.length, forwarded);
}
function handleStagedShipping() {
var getLabel = GmailApp.getUserLabelByName('_Stage/Shipping');
var setLabel = GmailApp.getUserLabelByName('Shipping');
var recipient = 'track@junecloud.com';
var threads = getLabel.getThreads();
var forwarded = 0;
for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
var message = thread.getMessages()[0];
var from = message.getFrom();
if (from.indexOf('ship-confirm@amazon.com') === -1) {
message.forward(recipient);
forwarded++;
}
thread.addLabel(setLabel);
thread.removeLabel(getLabel);
}
Logger.log("%s shipping emails handled, %s forwarded", threads.length, forwarded);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.