JoaMika
JoaMika

Reputation: 1827

Google Scripts Auto-Forward for Google Inbox

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 = '[email protected]';
  //  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});
    }
  }
}

Upvotes: 0

Views: 1890

Answers (0)

Related Questions