Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreineculau/f293405efdde209d21a3a7ef1f47b49d to your computer and use it in GitHub Desktop.
Save andreineculau/f293405efdde209d21a3a7ef1f47b49d to your computer and use it in GitHub Desktop.
Google Apps Script to forward email on header match
// 1. Add a Gmail "azure" filter to all Azure email for instance
// Matches: from:(azuredevops@microsoft.com)
// Do this: Skip Inbox, Apply label "azure"
// 2. Add a Gmail "azure-forwarded" filter to keep track of those threads that have forwarded messages.
// 3. Add this script to a new project at https://script.google.com
// 4. Replace "X-VSS-Scope: example" with your "X-VSS-Scope" header (open an email, then click "Show original")
// Replace "user@example.com" with your preferred forwarding email target.
// 4. Run it once. It will ask you for access to your Gmail.
// 5. Add a time trigger to run this function every minute, and notify you hourly of errors.
function main() {
var azureForwardedLabel = GmailApp.getUserLabelByName('azure-forwarded');
var threads = GmailApp.search('in:azure is:unread');
threads.forEach(function(thread) {
var messages = thread.getMessages();
var matched = false;
messages.forEach(function(message) {
var body = message.getRawContent();
if (!body.match(/X-VSS-Scope: example/g)) {
return;
}
matched = true;
message.forward('user@example.com');
message.markRead();
Logger.log({forwarded: message});
});
thread.addLabel(azureForwardedLabel);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment