0

I have found this question that is answered:

How can I automatically forward Gmail emails when a label is applied?

However I'm using that exact script and have replaced the label and recipient with my information, and it works intermittently, if at all.

I've tried formatting the label text string differently, changing the interval from 5 minutes to 1 minute and back, and if it does work, it misses a lot.

One note: I took out the "+'after:' +timeFrom" snippet and ran it once, and it was forwarding everything in the folder before it gave me a server error (which is understandable, there are about 6600 messages in the label) so I'm wondering if it might have something to do with the timing coding?

Another note is that I'm trying this on a G Suite email, rather than a regular Gmail account, so I'm not sure if that would affect anything.

Here's what I have currently:

  function autoForward() {
  var label = 'Questionnaires/Agent Logins';
  var recipient = 'slmquestionnaire+logins@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've tried reformatting the "label" variable to be:

Questionnaires/Agent_Logins

questionnaires-agent-logins, which is the format you see in the Gmail search bar when viewing the label.

I'm somewhat familiar with javascript and the general logic of programming, so this code more or less makes sense to me, and I don't see why it would be having trouble.

I would really really appreciate any help with this, as it could save my boss up to 3 hours a day if I could automate this for him.

P.S. I also have tried the IFTTT recipe mentioned and no dice either.

0

As I understand it you will have to iterate over the messages in the thread. This is my take in forwarding.

function Forward() {
  var imessagesForwarded = 0;
  var label = 'Relay';
  var doneLabel = GmailApp.getUserLabelByName('Relayed');
  var threads = GmailApp.getUserLabelByName(label).getThreads();

  var iforwarded = 0;
  for (var i = 0; i < threads.length; i++) {
    var it = threads.length;
    if (iforwarded == im ) {
      threads[i-1].addLabel(doneLabel);
      threads[i-1].removeLabel(GmailApp.getUserLabelByName(label));
    }
    var messages = threads[i].getMessages();
    var im = messages.length;
    for (var j = 0; j < messages.length; j++) {  
      var email = messages[j];
      if (!email.isStarred()) { //To keep track of processed emails, I star message when processed.
        try {
          forwardTo = 'test@nowhere.na';

          email.forward(forwardTo, {replyTo: email.getFrom(),subject: email.getSubject()});
          imessagesForwarded++;
          email.star();
          iforwarded++;
        } catch(e) {
          MailApp.sendEmail("my@email.addr","Forward failed", "Errormsg:\n" + e.message);
          continue;
        }
      } else { 
        iforwarded++;
      }
    }
  }  
  if (iforwarded == im ) {
    threads[i-1].addLabel(doneLabel);
    threads[i-1].removeLabel(GmailApp.getUserLabelByName(label));
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.