0
votes

I have email address "[email protected]" send mail to email address "[email protected]". And i want reply mail to email address "a@gmail" and add more email address "[email protected]". I read google docs and followed but it didn't work. Just only email address "[email protected]" received, email address "[email protected]" have not received the mail. I don't know where I'm wrong ??? Thanks for your interest!

function replyToMutiAddress(){

  var thread = GmailApp.getThreadById("16dxxxxxxba6");
  var mgs = thread.getMessages()[0];
  var option = {    
      replyTo: "[email protected],[email protected]",
      cc: "",
      bcc: "",
      htmlBody: "[email protected] reply mail to [email protected] and add [email protected]"
  }
  mgs.reply("",option);

}
1

1 Answers

0
votes

How about this answer?

The official document of Sending Email says as follows.

If you're trying to send a reply and want the email to thread, make sure that:

  1. The Subject headers match
  2. The References and In-Reply-To headers follow the RFC 2822 standard.

For above situation, the email is replied using Gmail API. Before you run the script, please enable Gmail API at Advanced Google services.

Sample script:

function replyToMutiAdress(){
  var body = "sample body";
  var additionalEmailAddress = "[email protected]";
  var thread = GmailApp.getThreadById("16dxxxxxxba6");
  var mgs = thread.getMessages()[0];
  var message = Gmail.Users.Messages.get("me", mgs.getId());
  var head = 'Content-Type: text/plain; charset="UTF-8"\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: 7bit\r\n';
  var data = message.payload.headers.reduce(function(s, e) {
    if (e.name.toUpperCase() == "MESSAGE-ID") {
      s += "References: " + e.value + "\r\nIn-Reply-To: " + e.value + "\r\n";
    } else if (e.name == "Subject") {
      s += e.name + ": " + e.value + "\r\n";
    } else if (e.name == "From") {
      s += "To: " + e.value + "," + additionalEmailAddress + "\r\n";
    } else if (e.name == "To") {
      s += "From: " + e.value + "\r\n";
    }
    return s;
  }, head);
  data += "\r\n" + body;
  var res = Gmail.Users.Messages.send({raw: Utilities.base64EncodeWebSafe(data)}, "me");
}
  • In this sample script, References and In-Reply-To are included in the header.
  • [email protected] is added as the additional email address.

References: