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));
  }
}
0

I have been using a well written script for this that was designed to forward email to an Evernote account but works well for other use cases. The script utilizes a Google Sheet to make the appropriate settings and then checks a Gmail label every 5 minutes using a trigger, forwards the emails it finds, and then moves them to the archive or whatever other tag you want to apply. I use it to forward e-mails to Evernote and Omnifocus.

You can find the explanatory web page and download the script here:https://googlescripts.harryonline.net/gmail-to-evernote

  • @pjmg, I think your response reflects more of a "this sucks" attitude than my answer. I searched for a long time to find a type of solution that would forward email when label applied in Gmail and couldn't find one. When I finally found one, I thought it might be helpful for others looking for the same solution. Very few resources or answers pop up with google search. So, my first contribution to StackExchange is tagged with a "this sucks" tag without explanation or even any personal interaction. You're found a great way to discourage participation from new contributors. Thanks! – user250771 Nov 3 at 18:43
  • Hey, please don't necessarily take it personally, I was the first to make this mistake on more than one occasion. I'm sorry you take it that way, but today if the link becomes a 404, your contribution will no longer be relevant. You can edit your answer and I'll delete my comment if this what you want. Don't be discouraged from continuing to contribute, but the rules must be respected. Maybe you're right and my judgment is not the right one, it may well be possible, I'm not an expert on the subject. Anyway, I thank you for taking the time to search for an answer. – pjmg Nov 4 at 7:33

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.