Created
November 15, 2017 23:51
-
-
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.
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
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