1
votes

I'm using Google Apps Script to compose replies to Gmail messages.

function reply(messageId) {
  var message = GmailApp.getMessageById(messageId);
  message.reply("Thanks for the message!");
}

Pretty simple.

Except I want to quote the entire previous message thread, as would normally happen when you hit 'Reply' in your email client.

I would want the quoted threads to appear indented, like your email client would do.

Any suggestions?

Thanks!

1

1 Answers

1
votes
  • You want to reply an email to a message by adding the body of message from var message = GmailApp.getMessageById(messageId); as the quote.

If my understanding is correct, how about this answer?

Issue and workaround:

Unfortunately, in the current stage, there are not built-in method for directly replying the email by adding the previous message body as the quote. So in this answer, a simple method, I would like to propose to retrieve the previous message body and adding the reply email as the quote.

Modified script:

When your script is modified, it becomes as follows.

function reply(messageId) {
  var message = GmailApp.getMessageById(messageId);

  // I added the following script.
  const newMessage = "Thanks for the message!";
  const replyMessage = message.getPlainBody().split("\n").reduce((s, e) => (s += `> ${e}\n`)
  , `${newMessage}\n\n${message.getDate().toISOString()} ${message.getFrom()} wrote:\n\n`);
  message.reply(replyMessage);
}

Note:

  • In this modification, the text body is used.
  • And also, as a sample, the date of ISO 8601 type is used as the date of the previous message. So if you want to use the other date format, please modify for your actual situation.
  • Please use this modified script with enabling V8.

References: