Ayo
Ayo

Reputation: 21

On Google Apps Script, is there a way to specify exactly what email address I'm sending a reply to when using the replyAll function?

I am utilizing google apps to make a script that goes through my inbox and finds every thread with a particular label. Then for each thread, I want to send in a reply, but instead of just sending a reply to the recipient of the last message, I want to be able to send a reply to the recipient of the original message in the thread (i.e the person in the "to:" section of the first message).

The problem is that the reply and replyAll functions send a message, by default, to the person in the "to" field for the last message in the thread. I get that I'm able to access the recipient in the original message using something like this:

var firstMessage = thread.getMessages()[0].getTo();

and then input firstMessage as the "cc:" email in the reply, but then that looks weird in the Gmail app when the recipient of the original message isn't the last person to send a message in the thread.

Is there any way I could input in a "to:" field in my replyAll function the way I can put in a "cc:" field?

function sendFollowUpEmails() {
  var query = 'label:lasso-fu-08-19-2023';
  var threads = GmailApp.search(query, 0, 50);

  var followUpMessage = `Hello Fren :)

  How are you doing today?
  `;
  followUpMessage = followUpMessage.replace(/\n/g, '<br>');

  for (var i = 0; i < threads.length; i++) {
    var thread = threads[i];
    var firstMessage = thread.getMessages()[0]; // Getting the first message in the thread
    var toField = firstMessage.getTo();
    var ccField = firstMessage.getCc() || ''; // Including the original CC addresses
    var allRecipients = toField + (ccField ? ',' + ccField : ''); // Combining TO and CC into one list

    thread.replyAll('', {
      htmlBody: followUpMessage,
      replyTo: toField
    });
  }

  Logger.log("Follow-up emails successfully sent to " + threads.length + " threads");
}

I expected the code above to send a new reply to the recipient of the first message, but it just sends the reply to whatever the reply-to field is for the last sent message. And it's my understanding that all the "replyTo: toField" line does is set the 'reply-to' email for this new message to the value of toField.

I took a look at this [question] (Sending a reply to a specific message in a Gmail thread using GmailApp in Google Apps Script and quoting the original message)on stackOverflow, but the fix was to use reply-to, and that doesn't resolve the problem.

Upvotes: 0

Views: 207

Answers (1)

Tedinoz
Tedinoz

Reputation: 8069

Though you are looping through threads, you don't need to reply to the thread (or use thread.replyAll).

You just need to send an email: sendEmail(recipient, subject, body, options) Doc Ref.

  • recipient: toField
  • subject: firstMessage.getFirstMessageSubject()
  • body: followUpMessage
  • options: {cc: ccField}

Say, MailApp.sendEmail(toField, firstMessage.getFirstMessageSubject(), followUpMessage, options);


Regarding replyTo this is the default reply-to address (default: the user's email address)`. i.e., this would be your email address.

Upvotes: 0

Related Questions