Skip to content

Instantly share code, notes, and snippets.

@lkam88
Created November 15, 2017 23:51
Show Gist options
  • Save lkam88/0f37c92cd83321b9a7080297acb3c500 to your computer and use it in GitHub Desktop.
Save lkam88/0f37c92cd83321b9a7080297acb3c500 to your computer and use it in GitHub Desktop.
This a Google Apps Script that automatically forwards email to a person give a search query. In this example, any new emails from Amazon concerning orders addressed to John Doe will get forwarded to him.
var searchQuery = 'from:*@amazon.com in:inbox John Doe'; // replace with actual query
var forwardAddress = 'john.doe@example.com'; // replace with actual email
function forwardEmails() {
var emailThreadsToFoward = GmailApp.search(searchQuery);
for (var i = 0; i < emailThreadsToFoward.length; i++) {
var emailThread = emailThreadsToFoward[i];
var email = emailThread.getMessages()[0];
try {
email.forward(forwardAddress);
email.markRead();
emailThread.moveToArchive();
} catch (e) {
Logger.log(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment