title: Apps Script Gmail createDraftReplyAll wrong recipient: Puts me as recipient instead of other party
body: <p>I have a working script for creating a new Gmail draft but when I use <code>createDraftReplyAll</code> in an existing thread, it puts the sender of the last email of the thread as the recipient of the draft.</p>
  <p>So if I sent the last email, I become the recipient of the reply, and the other party is Cc'd. And if the other person wrote the last email, they are the recipient but I get CC'd. This is not normal behavior compared with manually creating a draft in Gmail (i.e. from clicking Reply To All), in which case the recipient will always be the other party.</p>
  <p>Shown here (result of running the script below):</p>
  <p><a href="https://i.stack.imgur.com/DyT2Em.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DyT2Em.jpg" alt="" /></a></p>
  <p>And normal behavior from clicking Reply All in Gmail:</p>
  <p><a href="https://i.stack.imgur.com/GnJ5Xm.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GnJ5Xm.jpg" alt="" /></a></p>
  <p>This is the function I use to create the draft. The thread has already been found via the Subject and original message date:</p>
  <pre><code>  function createDraftReplyInThread(emailThread, htmlTemplateFileName) {
    Logger.log(&quot;&gt;&gt; IN createDraftReplyInThread&quot;);
    Logger.log(&quot;htmlTemplateFileName: &quot; + htmlTemplateFileName);
  
    if (emailThread) {
      let emailTo = emailThread.to;
      Logger.log('Thread TO: ' + emailTo);
      let threadMessages = emailThread.getMessages();
      let messageCount = threadMessages.length;
      let lastMessage = threadMessages[messageCount - 1];
  
      let htmlTemplate = HtmlService.createTemplateFromFile(htmlTemplateFileName);
      let htmlBodyContent = htmlTemplate.evaluate().getContent();
      var myReplyID = lastMessage.createDraftReplyAll(
        &quot;Plain text for draft reply (not used)&quot;,
        {
          'htmlBody': htmlBodyContent
        }).getId();
  
      Logger.log(`Draft created in thread; myReplyID: ${myReplyID}`);  // just to show it's executed okay
  
      return;
    }
  
    return null;
  }
  </code></pre>
  <p>It's probably best to ignore this, but I did try to use the limited existing update methods to copy the content, change the recipient and reinstate the the content in a new draft (pure guess work). Noting that draft.update() does work to change the recipient but it also requires a Message argument which I failed to get to work. Possible?</p>
  <pre><code>// based off stackoverflow: https://stackoverflow.com/questions/54745999/modify-only-the-subject-and-send-the-draft
  function _sandbox_draftModify() {
  
    var draft = GmailApp.getDrafts()[0];
    var draftAsMessage = draft.getMessage();
    var recipients = draftAsMessage.getTo();
    var subject = draftAsMessage.getSubject();
    var origRaw = draftAsMessage.getRawContent();
    var newRaw = origRaw.replace(&quot;new draft&quot;, &quot;old draft&quot;);
    var newRawB64 = Utilities.base64EncodeWebSafe(newRaw, Utilities.Charset.UTF_8);
  
    var emailTo = 'recipientsemail@gmail.com';  
    Logger.log(`recipients: ${recipients}`);
    Logger.log(`subject: ${subject}`);
    Logger.log(`newRaw: ${newRaw}`);
  
    draft.update(emailTo, subject, { message: { raw: newRawB64 } });
  
    var draftNew = GmailApp.getDrafts()[0];
    userId = 'me';
    Gmail.Users.Drafts.update({ message: { raw: newRawB64 } }, userId, draftNew.getId());
    Logger.log(&quot;Done&quot;);
  }
  </code></pre>
  <p>In the end all I need is to be able to create a draft email to an existing thread (which I can do already) but <em>with the correct recipient(s)</em>, however that may be achieved.</p>