I have a working script for creating a new Gmail draft but when I use createDraftReplyAll
in an existing thread, it puts the sender of the last email of the thread as the recipient of the draft.
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.
Shown here (result of running the script below):
And normal behavior from clicking Reply All in Gmail:
This is the function I use to create the draft. The thread has already been found via the Subject and original message date:
function createDraftReplyInThread(emailThread, htmlTemplateFileName) {
Logger.log(">> IN createDraftReplyInThread");
Logger.log("htmlTemplateFileName: " 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(
"Plain text for draft reply (not used)",
{
'htmlBody': htmlBodyContent
}).getId();
Logger.log(`Draft created in thread; myReplyID: ${myReplyID}`); // just to show it's executed okay
return;
}
return null;
}
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?
// 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("new draft", "old draft");
var newRawB64 = Utilities.base64EncodeWebSafe(newRaw, Utilities.Charset.UTF_8);
var emailTo = '[email protected]';
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("Done");
}
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 with the correct recipient(s), however that may be achieved.
CodePudding user response:
This seems to be the expected behavior for the createDraftReplyAll
method.
If you check the documentation page here:
createDraftReplyAll(body)
Creates a draft message replying to the sender of the last message in this thread, using the reply-to address and all recipients of this message. The size of the email (including headers) is quota limited.
However, you can file a feature request on Issue Tracker if you would like to have a functionality which replicates the UI behavior. The form can be found at this link and make sure to fill in all the necessary information.