Blog

How to send a reply in the same email thread from your app

· SyncYak team

  • mail-api
  • email-threading
  • gmail-api
  • microsoft-graph

Your app sends a follow-up on behalf of a user. The lead opens Gmail and sees a brand-new conversation with "Re:" in the subject and none of the earlier messages under it. In Outlook the same follow-up sits apart from the original, and when the lead answers, your app cannot tell which message they answered. The send worked. The thread did not.

This post explains what mail clients actually use to group messages, why a send from an API so often breaks the chain, and what a correct reply looks like, whether you talk to Gmail, Microsoft Graph or plain SMTP.

What a mail client uses to group messages

Every message carries a Message-ID header, a unique string set by whoever sent it. A reply names the message it answers in two more headers:

  • In-Reply-To: the Message-ID of the message being answered.
  • References: the Message-ID of every ancestor in the conversation, oldest first, ending with the one being answered.

Clients walk those headers to rebuild the tree. Gmail and Outlook also keep their own grouping key, which is where API senders get caught out.

Google's Gmail API documentation lists three conditions for a sent message to join an existing thread: the request must name the threadId, the References and In-Reply-To headers must be set as the email RFC requires, and the Subject headers must match (Gmail API guide to threads). Miss any one and Gmail starts a new conversation even though the headers look right.

Microsoft Graph groups by a conversationId on each message, and Outlook additionally reads a Thread-Index header that other clients ignore. A reply created through Graph's reply endpoints inherits the conversation; a message built from scratch and sent through the generic send endpoint often does not.

Why app replies break the chain

Three mistakes account for most broken threads.

You never stored the Message-ID of what you sent. Gmail and Graph replace the Message-ID on messages you submit through their APIs, so the value you wrote into the MIME is not the one that went out. Graph returns the final id as internetMessageId; Gmail does not return it at all, and you have to read the sent message back to learn it. If your database only holds your own generated id, nothing you put in In-Reply-To later will match.

You changed the subject. "Re: Showing on Friday" and "Following up: Showing on Friday" are different subjects to Gmail's threading rule. Keep the original subject, with at most a Re: prefix.

You set the headers but not the provider's key. On Gmail, a correct In-Reply-To without the threadId in the request is a new thread. On Graph, a message sent without a conversation link gets a fresh conversationId.

Plain SMTP is the one place where none of this bites. The server delivers the message you composed byte for byte, so the Message-ID you wrote survives, and In-Reply-To alone correlates replies reliably. Any app that supports both Gmail and IMAP mailboxes therefore needs two strategies: headers for SMTP, provider thread ids for Gmail and Microsoft.

A correct reply, step by step

If you are building this yourself against the provider APIs, the sequence is:

  1. When you send the first message, read back the provider's message id, the provider's thread or conversation id, and the final Message-ID. Store all three against the contact.
  2. When you reply, reuse the original subject, prefixed with Re: once.
  3. Set In-Reply-To to the stored Message-ID and References to the full chain, oldest first.
  4. On Gmail, send with threadId set to the stored thread. On Graph, create the reply through the reply endpoint on the original message, or set the conversation link, rather than composing a detached message.
  5. When mail arrives, match In-Reply-To and each entry of References against your stored Message-ID values, and fall back to the provider's thread id when the headers do not match.

Step 5 matters as much as the others. Gmail's parsed message format drops header ordering you need, and bounces quote the original message in a way that looks like a reply. Reading the raw message and checking for a delivery-status part before treating anything as a reply avoids filing "Undelivered Mail Returned to Sender" as the lead's answer.

One related change for teams on Microsoft 365: Microsoft has announced that from 12/31/2026, changing the subject, body or recipients of a non-draft message through Graph will need the new Mail-Advanced.ReadWrite permissions, which require tenant administrator consent (Microsoft 365 Developer Blog). If your app has been editing sent messages instead of sending proper replies, that shortcut is closing.

How SyncYak helps

SyncYak Mail does the bookkeeping above for every connected mailbox, so your code sends a reply with one field. Each sent message comes back with a thread_id, and a later send that names reply_to_message_id is threaded onto that message: SyncYak sets In-Reply-To and References, places it in the provider's thread or conversation, and, if you leave the subject empty, uses Re: plus the original subject.

curl -X POST https://api.syncyak.com/v3/grants/$GRANT_ID/messages/send \
  -H "Authorization: Bearer $SYNCYAK_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "to": [{ "email": "[email protected]", "name": "Sam" }],
    "subject": "",
    "body": "<p>Still good for Friday at 10?</p>",
    "reply_to_message_id": "Xk2…",
    "tracking_options": { "thread_replies": true, "label": "contact-42" }
  }'

The response is the sent message, including its thread_id. Listing messages with that thread_id returns the whole conversation for a lead's timeline. An unknown reply_to_message_id answers 400 rather than silently sending a detached message.

Detecting the answer works the same way on every provider. Send with thread_replies on and SyncYak correlates incoming mail against what it sent: by In-Reply-To and References where the Message-ID survived, and by the provider's thread id on Gmail and Microsoft 365, where it did not. Each reply, not only the first, posts a thread.replied webhook with the thread_id, the reply's message_id, the root_message_id and the label you set on the send, signed with HMAC-SHA256 in X-Nylas-Signature. Bounces are checked first and arrive as message.bounce_detected, so a delivery failure never counts as a reply. Reply detection changes nothing about the message itself and works on every plan, including Free, which connects 3 mailboxes and sends 1,000 messages a month. Open and click tracking is separate and starts on Hobby at $12 a month.

The request and webhook formats follow Nylas v3, so a team already sending through Nylas changes the API host, client ID and secret and keeps the rest. The Mail product page covers the hosted connect page for Gmail, Microsoft 365 and IMAP, the email webhooks page lists every event, the Mail docs have the field reference, and pricing has the full ladder.

A quick checklist before you ship

  • Store the provider's message id, thread id and final Message-ID for every send, or use an API that returns them.
  • Reuse the original subject with one Re: prefix.
  • Set In-Reply-To and References and the provider's thread or conversation id, not one or the other.
  • Match incoming mail on headers first, provider thread id second, and rule out bounces before calling anything a reply.
  • Test with a Gmail account, a Microsoft 365 account and one IMAP mailbox, because each fails differently.

Connect a mailbox on the free plan and send a threaded reply in a few minutes: create a SyncYak account.

Start free with SyncYak · All posts