Search code examples
google-apps-scriptgoogle-sheetsgmailreply

Sending a reply to a specific message in a Gmail thread using GmailApp in Google Apps Script and quoting the original message


I am working on a Google Apps Script project where I need to send a reply to a specific message within a Gmail thread using the GmailApp service. However, I have encountered a couple of challenges that I'm seeking assistance with.

Firstly, I want to send the reply to the recipient of the first message in the thread, which was originally sent by me. I have tried using the GmailApp service, but it only sends the reply to the sender (me) by default. I am wondering if there is a way to write a snippet code to send the reply to the recipient instead, allowing me to send a follow-up reminder. I have the thread ID and will recall the reply email body from another sheet cell in Google Sheets. Secondly, I need to preserve the formatting of the reply email body, which is stored in a cell of another sheet. The body contains multiple paragraphs, and I want to avoid merging the lines of the email body and preserve the font size. I am unsure of the best approach to achieve this using GmailApp. Should I consider using HTML body or explore alternative methods?

Furthermore, I would like to quote the original message within the reply email, similar to how Gmail UI quotes the original message in a reply. Specifically, I want to include the original message below the email body of the reply, showing the sender, date, and the original message content. For example, something like:

"On Fri, Jun 23, 2023 at 4:32 PM Hamed [email protected] wrote:

Dear--------,

I hope this email finds you well."

I would appreciate any guidance to address these challenges and achieve the desired functionality within my Google Apps Script project.

Thank you in advance for your assistance!


Solution

  • Suggestion:

    Note: This may not totally resolve your problem because you haven't included your data, but it will serve as a starting point and guidance in doing so.

    The script below works as follows

    1. Retrieves the recipient of the first message on the thread using the GmailApp service
    2. Retrieve the first message body on the thread sent by you
    3. Extract the reply email body from a sample sheet I made with a sample paragraph using SpreadSheetApp Service
    4. Quoting the first message body similar to how Gmail UI quotes the original message in a reply
    5. Send the email to the recipient

    Script:

    function sendReplyToThread() {
    
      var firstThread = GmailApp.getInboxThreads(0, 11)[0]; //Get the first thread on your inbox
      var threadById = GmailApp.getThreadById(firstThread.getId()); //Get the ID of the thread
      var messages = threadById.getMessages();
      var firstMessage = messages[0]
      var firstEmail = firstMessage.getBody(); //The first message of the thread sent by you
      var recipient = firstMessage.getTo() // Email of the first recipient
    
      // Getting the reply email body from sheet
      var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      var data = ss.getRange(1, 1).getValue();
      var data = data.replace(/\n/g, "<br>");   // Replace line breaks with HTML line break tags
      var emailBody = "<html><body>" + data + "</body></html>"
    
      // Obtaining and quoting the original message
      var originalMessage = '<div style="border-left: 1px solid #ccc; padding-left: 10px; margin-left: 10px;">';
      originalMessage += '<p style="color: rgb(80, 0, 80);">On ' + formatDate(firstMessage.getDate()) + ' <a href="mailto:' + firstMessage.getFrom() + '">' + firstMessage.getFrom() + '</a> wrote:</p>';
      originalMessage += '<blockquote style="margin: 0 0 0 10px; border-left: 1px solid rgb(80, 0, 80); padding-left: 10px; color: rgb(80, 0, 80);">' + firstEmail + '</blockquote>';
      originalMessage += '</div>';
    
      //Sending the reply email
      var replyDraft = firstThread.reply("", {
        htmlBody: emailBody + "<br><br>" + originalMessage
        , replyTo: recipient});
    }
    //Function for date format
    function formatDate(date) {
      var formattedDate = Utilities.formatDate(date, Session.getScriptTimeZone(), "EEE, MMM d, yyyy 'at' h:mm a");
      return formattedDate;
    }
    

    Sample Thread:

    enter image description here

    Reply Email using the script:

    enter image description here

    References:

    Class GmailApp

    GetTo method

    CreateDraft method

    reply method