My Setup:
I’m working with two Gmail accounts (let’s call them User X and User Y) from different Google Workspace domains.
What I’m doing:
- I send an email from User X to User Y using Gmail API through Google Apps Script:
function createAndSendMessage(toAddress, emailSubject, messageBody) {
// Build the email message
var messageText = [
'To: ' + toAddress,
'Subject: ' + emailSubject,
'',
messageBody
].join('\r\n');
var encodedMessage = Utilities.base64EncodeWebSafe(messageText);
// Send using Gmail API
var response = Gmail.Users.Messages.send({
raw: encodedMessage
}, 'me');
return response.threadId
}
- User Y replies to this email through the regular Gmail web interface
- The reply shows up correctly in Gmail UI and appears threaded with the original message
- I try to fetch all messages in the thread using the threadId from step 1
The Issue:
When I call users.threads.get with the threadId, I only get back the original message I sent programmatically. The reply sent through Gmail UI doesn’t appear in the API response, even though it’s clearly part of the same thread in the Gmail interface.
My Question:
What do I need to include in my initial email to ensure that replies get properly threaded when retrieved via the API? Are there specific headers I should add when sending the original message?