'How to forward a Gmail email and add to the message body using GmailApp in Google Apps Script?

The GmailMessage.forward only has an htmlBody option. But if you use that, then the forwarded email does not include the original email's body/content. It only includes what you put in htmlBody.

How can I forward the email, add text, and include the original body?

var message = GmailApp.search(`...`)[0].getMessages()[0];

message.forward("...", {
  "htmlBody" : "hi"
});


Solution 1:[1]

You would need to get the body of the message you are looking to forward:

function myFunction() {
    const message = GmailApp.search(`to:[email protected]`)[0].getMessages()[0];
    const forwarded = message.getBody(); // that is the body of the message we are forwarding.

    message.forward("[email protected]", {
      "htmlBody" : "My email test?<br><br>" //adds your message to the body
            +
            "<div style='text-align: center;'>---------- Forwarded message ----------</div><br>" + forwarded, //centers
    });
    }

I made a sample code based on yours by searching messages that were sent to a specific email address and forwarding to a hotmail email address.

Edit:

Reference code and the importance of getting the body of the email can be found over this thread

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Salomon