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]>


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 ) {
    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});
        } catch(e) {
          MailApp.sendEmail("[email protected]","Forward failed", "Errormsg:\n" + e.message);
      } else { 
  if (iforwarded == im ) {
  • Hi, welcome to WebApps. Well, statement of the obvious... the IF statement in lines#11-14 references the variable im. But im isn't declared until line#16. Better to move rows#15-16 above the IF. so that you have a value for im.
    – Tedinoz
    Jun 1, 2022 at 0:19
  • This question has been bumped by Community user five times.
    – Rubén
    Oct 27 at 4:54

1 Answer 1


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]'
            {replyTo: email.getFrom(),
            subject: emaiul.getSubject()})
        Logger.log("DEBUG: Thread#"+t+", message#"+m+" -Email forwarded")
        // star this message
        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
    Logger.log("DEBUG: Thread#"+t+", `done` label added to thread")
    Logger.log("DEBUG: Thread#"+t+",`test` label removed from thread")
  }  // end of threads loop


  • 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 that you have read and understand our privacy policy and code of conduct.

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