I’m trying to make a Google Apps Script that creates a draft reply in the same email thread. Right now my script does these things:
- Grabs the original email info
- Copies the To and CC addresses
- Adds some text to the body
- Saves it as a draft
But when my coworkers get the email it shows up as a new message instead of part of the thread. How can I fix this?
Here’s a simplified version of what I’m doing:
function makeDraftReply() {
var originalEmail = GmailApp.getMessageById(someId);
var ccList = originalEmail.getCc();
var newBody = 'My reply text\n\n' + originalEmail.getBody();
var draft = originalEmail.createDraftReply('', {
cc: ccList,
htmlBody: newBody
});
return draft;
}
What do I need to change to make sure the draft shows up in the same thread when sent? Thanks for any help!
hey there, i had a similar problem before. have u tried using the threadId parameter when creating the draft? it worked for me. just grab the thread ID from the original email and add it to ur createDraft() call. should keep everything in the same thread. good luck!
I’ve been working with Gmail APIs for a while now, and I’ve found that threading can be tricky. One thing that’s worked well for me is using the createDraftReplyAll() method instead of createDraftReply(). This method seems to handle threading more reliably.
Here’s how you might modify your code:
function makeDraftReply() {
var originalEmail = GmailApp.getMessageById(someId);
var newBody = 'My reply text\n\n' + originalEmail.getBody();
var draft = originalEmail.createDraftReplyAll('', {
htmlBody: newBody
});
return draft;
}
This approach should maintain the original recipients and keep the reply in the same thread. It’s been more consistent in my experience. If you’re still having issues, double-check that your Gmail settings aren’t set to always start new conversations.
I’ve encountered this issue before when working with Gmail’s API. The key is to ensure you’re properly setting the thread ID when creating the draft reply. Try modifying your code to explicitly set the threadId parameter:
function makeDraftReply() {
var originalEmail = GmailApp.getMessageById(someId);
var ccList = originalEmail.getCc();
var newBody = 'My reply text\n\n' + originalEmail.getBody();
var threadId = originalEmail.getThread().getId();
var draft = GmailApp.createDraft(originalEmail.getTo(), 'Re: ' + originalEmail.getSubject(), '', {
cc: ccList,
htmlBody: newBody,
threadId: threadId
});
return draft;
}
This approach should ensure your draft reply appears in the same thread when sent. Let me know if you run into any other issues!