0

I used this code for Gmail but now I am looking to do something similar for Google Inbox. The problem is that Google Inbox does not support labels, so I have to come up with another solution. Perhaps somehow check emails inside a container every 5 minutes and then forward them? Any other suggestions?

function autoForward() {
  var label = 'ForwardThis';
  var recipient = 'domain@forward.domain.com';
  //  if the script runs every 5 minutes; change otherwise
  var interval = 5;          
  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++) {
    var messages = threads[i].getMessages();
    var from = messages[i].getFrom();
    var subject = messages[i].getSubject();
    var to = messages[i].getTo();
    var attachment = messages[i].getAttachments();
    var body = messages[i].getBody();
    for (var j = 0; j < messages.length; j++) {
      var emailoptions = (
        "---------- Forwarded message ----------" + '<br>' +
        'From: ' + from + "<'" + from.replace(/^.+<([^>]+)>$/, "$1") + "'>" + '<br>' +
        'Date: ' + date + '<br>' +
        'Subject: ' + subject + '<br>' +
        'To: ' + to + "" + '<br>' + '<br>' + '<br>'
      );
      messages[j].forward(recipient,{htmlBody: body + emailoptions, Attachment: attachment});
    }
  }
}
tehhowch
  • 9,645
  • 4
  • 24
  • 42
JoaMika
  • 1,727
  • 6
  • 32
  • 61
  • Gmail treats IMAP folders as nested labels, e.g. label `Archive/2017` – tehhowch May 08 '18 at 18:32
  • so if I move an email in Google Inbox to "Trips" - the label I should use in the function should be "Archive/Trips" ? – JoaMika May 08 '18 at 18:41
  • The label would be `Trips` if there is no folder that Trips is nested in. My example above assumes `Archive` is a folder, containing folders `2017`, (and `2016`, `2015`, ...). If you log the results of thread / label queries, or open the Gmail web view to look at the same account you're using with `Inbox`, you should get a handle on this pretty quickly. – tehhowch May 08 '18 at 18:43
  • ok, I have tried this but I am getting an infinite loop of forwards - is there anything wrong with my function? – JoaMika May 08 '18 at 19:44
  • Your function says to forward each message in each thread that matched the search. While debugging I recommend simply logging to Stackdriver what matches, and how many times a given condition was matched. i.e. `console.log({query: 'label:' + label + ' after:' + timeFrom, numMatches: threads.length, totalMatchedMessages: (a counter you incremented in lieu of messages[j].forward)});` – tehhowch May 08 '18 at 19:56
  • Thanks for sharing your starter code. While debugging, I found a flaw in your loop. You use the variable i for iterating through the array of threads, but you also use it for messages. There should be a separate variable (in a nested loop) for interating through messages returned from each thread. – SW_user2953243 Nov 08 '21 at 01:25

0 Answers0