Home > Software engineering >  How to forward a Gmail email and add to the message body using GmailApp in Google Apps Script?
How to forward a Gmail email and add to the message body using GmailApp in Google Apps Script?

Time:05-14

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

CodePudding user response:

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

function myFunction() {
  var message = GmailApp.search(`to:[email protected]`)[0].getMessages()[0];

      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

  • Related