I am trying to create an Apps Script for Gmail, so that all messages labelled product-related and product-a are forwarded to the [email protected] address, and all messages labelled "product-related" and product-b are forwarded to the [email protected] address.
The script will be launched via a card, so there is no need for more automation.
Here's the code I did:
function testforward1() {
var label = "product-related";
//var interval = 2000;
//var date = new Date();
//var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval;
var threads = GmailApp.search('label:' + label);
for (var i = 0; i < threads.length; i++) {
if (label == "product-a" && "product-related") {
var recipient = '[email protected]';
var messages = threads[i].getMessages();
//var attachment = messages[i].getAttachments();
for (var j = 0; j < messages.length; j++) {
var body = messages[j].getBody();
messages[j].forward(recipient, {
htmlBody: body
});
}
}
if (label == "product-b" && "product-related") {
var recipient1 = '[email protected]';
var messages1 = threads[i].getMessages();
//var attachment1 = messages1[i].getAttachments();
for (var j = 0; j < messages1.length; j++) {
var body1 = messages1[j].getBody();
messages1[j].forward(recipient1, {
htmlBody: body1
});
}
}
}
}
I guess I did something wrong with the variables, but I'm a total beginner with Google Apps Scripts, and I already spent more than 10 hours on this, with no success.
I got no email transferred with this, but the execution gives no error. And I was wondering if the var label = "product-related"; should be replaced with something else?
I will be grateful if you could give me some help on this!
product-relatedtag. Then I guess that you want to check which one of those has also aproduct-atag. If my understanding is correct, then you need to updateif (label == "product-a" && "product-related") {withGmailLabel.getName(). The goal should be to read all the tags of theproduct-relatedthreads, and then check if they matchproduct-a. Please test this approach and share your findings. - Jacques-Guzel Heronvar threads = GmailApp.search('label:' + label);. - user10879705