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.
Web Applications Stack Exchange is a question and answer site for power users of web applications. It only takes a minute to sign up.
Sign up to join this communityHere 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":
var messages = threads[i].getMessages();
for (var j = 0; j < messages.length; j++) {
messages[j].forward(recipient);
}
var messages = threads[i].getMessages();
messages[messages.length - 1].forward(recipient);
after
parameter makes sure that the script only looks at the emails from the last 5 minutes, which is after the previous time it ran. As for old messages with that label, you can forward them by temporarily removing ' after:' + timeFrom
part, and running script manually once.
ForwardThis
and a nested label ForwardThis/Forwarded
. The script looks for any messages in ForwardThis
and forwards them, and removes the ForwardThis
label and applies the ForwardThis/Forwarded
label. Lets me stage any messages, regardless of age, for forwarding. Thanks for the idea!
Jan 12, 2017 at 16:23
Update: Support dropped due to Google's API change.
The 3rd-party service IFTTT (If This Then That) makes this possible. Use the first Gmail trigger "New email labeled...". It doesn't have to be a new email, just newly labeled. May take a bit to trigger.
No, sorry. The only automation available in Gmail by itself is Gmail Filters, which act on messages as they arrive. There's nothing built-in to Gmail that allows something to happen when a label is added to a message (unless it's part of a filter acting on an incoming message).