0

I am trying to figure out how we can forward the original message and include the recipient email, full chain, so for example:


---------- Forwarded message ---------
From: Ram Sam <[email protected]>
Date: Mon, Apr 4, 2022 at 5:42 PM
Subject: RE: about nurse appreciation day
To: [email protected] <[email protected]>


Receiver-

Sure, that sounds great! Also, share any flyers, brochures, etc. We do currently use

reward programs and this may be similar.
 

Thank you,

 

Ram Sam, PHR, SHRM-CP, FLWCS| Vice President of X

2033 Main St, Address| X Healthcare Management, LLC

I am also labeling the ones that were forwarded so they don't get reforwarded on the second run. Here is what I have so far

function Forward() {
  var imessagesForwarded = 0;
  var label = 'LeadTest';
  var doneLabel = GmailApp.getUserLabelByName('LeadFinished');
  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 = '[email protected]';
          email.forward(forwardTo, {replyTo: email.getFrom(),subject: email.getSubject()});
    messages[m].forward(rtm_email,{htmlBody: emailoptions + body , Attachment: attachment});
          imessagesForwarded++;
          email.star();
          iforwarded++;
        } catch(e) {
          MailApp.sendEmail("[email protected]","Forward failed", "Errormsg:\n" + e.message);
          continue;
        }
      } else { 
        iforwarded++;
      }
    }
  }  
  if (iforwarded == im ) {
    threads[i-1].addLabel(doneLabel);
    threads[i-1].removeLabel(GmailApp.getUserLabelByName(label));
  }
}
1
  • Can you edit you question to include a problem to be solved? The question doesn't describe what is or isn't working, any error essage you are receiving. Please review How to Ask. Questions should have value for future readers as well as OP.
    – Blindspots
    Commented Jun 22 at 17:10

1 Answer 1

0

Try this code:

function Forward() {
  
  // create variables for label names and get/create labels
  var labelName = 'LeadTest' //'LeadTest'
  var label = GmailApp.getUserLabelByName(labelName) || GmailApp.createLabel(labelName)

  var doneLabelName = "LeadFinished" // 'LeadFinished'
  var doneLabel = GmailApp.getUserLabelByName(doneLabelName) || GmailApp.createLabel(doneLabelName)
  
  //get all the threads containing the labelName
  var threads = label.getThreads()
  var tLen = threads.length
  
  // loop through the threads
  for (var t = 0; t < tLen; t++) {
    
    Logger .log("DEBUG: Thread status: t:"+t+" of "+tLen)    

    // get the messages for this thread
    var messages = threads[t].getMessages();
    var mLen = messages.length;
    Logger.log("DEBUG: Thread#"+t+" -Number of messages (mLen) = "+mLen)  

    // loop through messages in this thread
    for (var m = 0; m < mLen; m++) {

      var email = messages[m]
      
      //To keep track of processed emails, I star message when processed.
      // test if this message is NOT starred
      if (!email.isStarred()) { 

        // this message is NOT starred-forward the email
         forwardTo = '[email protected]'
        
        email.forward(
            forwardTo, 
            {replyTo: email.getFrom(),
            subject: emaiul.getSubject()})
        
        Logger.log("DEBUG: Thread#"+t+", message#"+m+" -Email forwarded")
        
        // star this message
        email.star()
        Logger.log("DEBUG: Thread#"+t+", message#"+m+" -Star message")

      } else { 
        
        // this message IS starred, so do nothing 
        // DEBUG: Left this "ELSE" for the sake of completeness/clarity
        
      }

    } // end of message loop


    // for all messages in this thread
    // delete 'Test' label and relace with 'Done' label
    threads[t].addLabel(doneLabel)
    Logger.log("DEBUG: Thread#"+t+", `done` label added to thread")
    threads[t].removeLabel(label)
    Logger.log("DEBUG: Thread#"+t+",`test` label removed from thread")
    
  
  }  // end of threads loop
 
}

Comments

  • the label names are defined
  • each label is defined as either getUserLabelByName() or createLabel. This will stops the error message Exception: Invalid argument: label
  • neither of the variables (iforwarded or imessagesForwarded) are required.
  • the variables for threads loop (t) and messages loop (m) were changed so that they were more intuitive
  • your code was sending forwarding each email twice.
  • the code to remove the 'test' label and add the 'Done' label is moved to follow the message loop WITHIN each thread.
  • the logic statement if (iforwarded == im ) is unnecessary this is the default outcome at the end of the messages loop.
  • I deleted the try/catch code since it complicated the logic flow/sequence. You can add it back now that the sequence is more clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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