0
votes

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!

1
Hi there! After testing this I have some questions. I assume that you want to look for all the threads with a product-related tag. Then I guess that you want to check which one of those has also a product-a tag. If my understanding is correct, then you need to update if (label == "product-a" && "product-related") { with GmailLabel.getName(). The goal should be to read all the tags of the product-related threads, and then check if they match product-a. Please test this approach and share your findings. - Jacques-Guzel Heron
Hello! Thanks for your answer. Actually, emails with both "product-related" and "product-a" labels should be forwarded to [email protected], and emails with both "product-related" and "product-b" should be forwarded to [email protected]. The thing is that I don't know if and how to get rid of the var label, because it is used in var threads = GmailApp.search('label:' + label); - user10879705
@Jacques-GuzelHeron any further help on this? Thank you very much in advance! - user10879705
Thank you for your support. I have been studying the code, but I don't see where you need help. Could you please share some examples? Also, please confirm what does «how to get rid of the var label» means. Why can't you delete that line? - Jacques-Guzel Heron
@Jacques-GuzelHeron The code does not work: no email is transferred. I don't know how to remove the label because it is used in var threads = GmailApp.search('label:' + label);. - user10879705

1 Answers

0
votes

I made it work (I think)! :)

function testforward1() {
 var threadsa = GmailApp.search('label: product-related label: product-a');
 for (var i = 0; i < threadsa.length; i++) {
  var recipient = '[email protected]';
  var messages = threadsa[i].getMessages();
  for (var j = 0; j < messages.length; j++) {
   var body = messages[j].getBody();
   messages[j].forward(recipient,{htmlBody: body});
  }
 }
var threadsb = GmailApp.search('label: product-related label: product-b');
for (var i = 0; i < threadsb.length; i++) {
 var recipient = '[email protected]';
 var messages = threadsb[i].getMessages();
 for (var j = 0; j < messages.length; j++) {
  var body = messages[j].getBody();
  messages[j].forward(recipient,{htmlBody: body});
 }
 }
}

Now I still have a few more functions to implement, but the basic forwarding function works, and it's the most important.