How to Automatically Forward Gmail Emails with Labels

gmailgmail-filters

Within Gmail, is there any way to automatically forward an email when I apply a label?

Gmail filters appear to only work with new emails. For my needs, applying the label is a manual process after the email was received.

Best Answer

Here is an Apps Script solution. Save it, changing the label and recipient, and set a trigger to run this function every 5 minutes.

It searches for threads with the given label that were created after the last time the script ran. In each, it forwards the first message to the given address.

function autoForward() {
  var label = 'forwardthis';
  var recipient = 'forward@gmail.com';
  var interval = 5;          //  if the script runs every 5 minutes; change otherwise
  var date = new Date();
  var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval;
  var threads = GmailApp.search('label:' + label + ' after:' + timeFrom);
  for (var i = 0; i < threads.length; i++) {
    threads[i].getMessages()[0].forward(recipient);  // only the 1st message
  }
}

I add some variations, replacing the line with "only the 1st message":

Forward every message in the thread

var messages = threads[i].getMessages();
for (var j = 0; j < messages.length; j++) {
  messages[j].forward(recipient);
}

Forward the last message in the thread

var messages = threads[i].getMessages();
messages[messages.length - 1].forward(recipient);