google-apps-scriptgmailgmail-apireply

Sending a reply to the recipient of the first message in a Gmail thread using GmailApp or MailApp in Google Apps Script


I am trying to write a code in Google Apps Script to send a reply to the recipient of the first message in a Gmail thread. The thread consists of an email that was previously sent by me to someone else. Initially, I tried using GmailApp but it only sends the reply to the sender (me) by default. Then, I switched to using the Gmail API, which allows me to send the reply to the recipient successfully.

However, I encountered another issue. It seems that the Gmail API only supports plain text body for the reply (according this source, and I would like to include HTML formatting in the email body, similar to what is possible with GmailApp.

Here's a snippet of the code I'm currently using:

var messageId = originalMessage.getHeader("Message-ID");
var data = [
  "MIME-Version: 1.0\n",
  `In-Reply-To: ${messageId}\n`,
  `Subject: Re:${originalMessage.getSubject()}\n`,
  `From: ${senderName} <${senderEmail}>\n`,
  `To: ${recipientEmail}\n\n`,
  emailBody,
].join("");
var draftReplyRequestBody = {
  message: {
    threadId: existingThreadId,
    raw: Utilities.base64Encode(data),
  }
};
var draftReplyResponse = Gmail.Users.Drafts.create(draftReplyRequestBody, "me");
var draftReplyId = draftReplyResponse.id;
var sendReplyParams = { id: draftReplyId };

Gmail.Users.Drafts.send(sendReplyParams, "me");

My main question is: Is it possible to modify this code to use GmailApp or MailApp instead of the Gmail API in order to send the reply to the recipient of the first message in the thread, similar to how a reminder email works? Additionally, is there any way to include HTML formatting in the email body using GmailApp or MailApp? Generally, which method is reliable and efficient? using Gmail API or GmailApp or MailApp?

Any insights or alternative approaches would be greatly appreciated. Thank you in advance!


Solution

  • From your following reply,

    Yes if you could modify the above code in order to send HTML body instead of plain text, the problem is solved.

    I believe your goal is as follows.

    In this case, please modify the value of data as follows.

    From:

    var data = [
      "MIME-Version: 1.0\n",
      `In-Reply-To: ${messageId}\n`,
      `Subject: Re:${originalMessage.getSubject()}\n`,
      `From: ${senderName} <${senderEmail}>\n`,
      `To: ${recipientEmail}\n\n`,
      emailBody,
    ].join("");
    

    To:

    var htmlBody = "<u><b>sample HTML body</b></u>"; // This is a sample HTML body.
    var data = [
      "MIME-Version: 1.0\n",
      `In-Reply-To: ${messageId}\n`,
      `Subject: Re:${originalMessage.getSubject()}\n`,
      `From: ${senderName} <${senderEmail}>\n`,
      `To: ${recipientEmail}\n`,
      "Content-type: multipart/alternative; boundary=sampleBoundary\n\n",
      "--sampleBoundary\n",
      "Content-type: text/plain; charset=UTF-8\n\n",
      emailBody,
      "\n\n--sampleBoundary\n",
      "Content-type: text/html; charset=UTF-8\n",
      "Content-Transfer-Encoding: quoted-printable\n\n",
      htmlBody,
      "\n\n--sampleBoundary--",
    ].join("");
    

    By this modification, when the mail client can see the HTML body, the HTML body can be seen. And, when the mail client cannot see the HTML body, the text body can be seen.

    If you want to include only the HTML body, how about the following modification?

    var htmlBody = "<u><b>sample HTML body</b></u>"; // This is a sample HTML body.
    var data = [
      "MIME-Version: 1.0\n",
      `In-Reply-To: ${messageId}\n`,
      `Subject: Re:${originalMessage.getSubject()}\n`,
      `From: ${senderName} <${senderEmail}>\n`,
      `To: ${recipientEmail}\n`,
      "Content-type: text/html; charset=UTF-8\n",
      "Content-Transfer-Encoding: quoted-printable\n\n",
      htmlBody,
    ].join("");