{"title":"Keeping an Email Conversation in One Slack Thread","html_url":"https://deallocatedobjects.com/posts/keeping-an-email-conversation-in-one-slack-thread","source_url":"","markdown_content":"If you post incoming email into Slack and let people answer from the thread, posting the first email is the easy part. The hard part is the second one: when the customer writes back, their reply has to land under the first message, in the same thread, and never as a new message at the top of the channel.\n\nI ran into this building [RelayMail](https://relaymail.app/?ref=deallocated-post), and the fix turned out to be sitting in every email already.\n\n### Email Already Solves This\n\nEvery message has a `Message-ID`. A reply carries `In-Reply-To`, the message it answers, and `References`, the chain so far, oldest first. Mail clients build a reply's `References` from the parent's `References` (or its `In-Reply-To` if it has none) plus the parent's `Message-ID`. So a reply to a reply still names the very first message.\n\nThat gives you a stable key. I store each Slack thread against the customer's first `Message-ID`, and every later email in the conversation can find it.\n\n### Matching an Inbound Email\n\nWhen an email arrives, I collect the ids it refers to, most specific first:\n\n1. `In-Reply-To`\n2. `References`, read newest to oldest\n\nThen I drop duplicates, skip the email's own `Message-ID`, and look each id up until one maps to a thread.\n\nTwo small things matter here. Providers send header names in different casing, so I read them case-insensitively. And `References` can fold across several lines, so I pull ids out with a pattern like `<[^<>\\s]+>` instead of splitting on spaces.\n\nOne rule matters more than it looks: a match only counts in the channel the inbox posts to right now. If a stored mapping points at an old channel, or at a different inbox, posting the email there puts it somewhere nobody is watching. In that case I start a new thread instead.\n\n### Sending the Reply\n\nWhen someone answers in Slack and I send that answer out as an email, I set both headers on it:\n\n- `In-Reply-To`: the customer's message id\n- `References`: the same id\n\n`References` is the one that's easy to skip. Some clients build the next `References` only from the parent's `References`. If mine were empty, the customer's next email could arrive without the original id anywhere in it, and the thread would break on the third message.\n\n### Forwarding Keeps the Headers\n\nAll of this works through a plain forwarding rule. When mail reaches RelayMail through Gmail forwarding, an Outlook rule, a Google Group or an alias, these headers come along unchanged. So I never needed access to anyone's mailbox to thread correctly. Everything the matching needs travels inside the message itself.\n\nThat's the whole system: one id stored per thread, `In-Reply-To` then `References` on the way in, both headers set on the way out, and a match only counts in the inbox's own channel.","html_content":"<p>If you post incoming email into Slack and let people answer from the thread, posting the first email is the easy part. The hard part is the second one: when the customer writes back, their reply has to land under the first message, in the same thread, and never as a new message at the top of the channel.</p>\n<p>I ran into this building <a href=\"https://relaymail.app/?ref=deallocated-post\">RelayMail</a>, and the fix turned out to be sitting in every email already.</p>\n<h2 id=\"email-already-solves-this\" data-level=\"3\">Email Already Solves This</h2>\n<p>Every message has a <code>Message-ID</code>. A reply carries <code>In-Reply-To</code>, the message it answers, and <code>References</code>, the chain so far, oldest first. Mail clients build a reply’s <code>References</code> from the parent’s <code>References</code> (or its <code>In-Reply-To</code> if it has none) plus the parent’s <code>Message-ID</code>. So a reply to a reply still names the very first message.</p>\n<p>That gives you a stable key. I store each Slack thread against the customer’s first <code>Message-ID</code>, and every later email in the conversation can find it.</p>\n<h2 id=\"matching-an-inbound-email\" data-level=\"3\">Matching an Inbound Email</h2>\n<p>When an email arrives, I collect the ids it refers to, most specific first:</p>\n<ol>\n<li><code>In-Reply-To</code></li>\n<li><code>References</code>, read newest to oldest</li>\n</ol>\n<p>Then I drop duplicates, skip the email’s own <code>Message-ID</code>, and look each id up until one maps to a thread.</p>\n<p>Two small things matter here. Providers send header names in different casing, so I read them case-insensitively. And <code>References</code> can fold across several lines, so I pull ids out with a pattern like <code>&lt;[^&lt;&gt;\\s]+&gt;</code> instead of splitting on spaces.</p>\n<p>One rule matters more than it looks: a match only counts in the channel the inbox posts to right now. If a stored mapping points at an old channel, or at a different inbox, posting the email there puts it somewhere nobody is watching. In that case I start a new thread instead.</p>\n<h2 id=\"sending-the-reply\" data-level=\"3\">Sending the Reply</h2>\n<p>When someone answers in Slack and I send that answer out as an email, I set both headers on it:</p>\n<ul>\n<li><code>In-Reply-To</code>: the customer’s message id</li>\n<li><code>References</code>: the same id</li>\n</ul>\n<p><code>References</code> is the one that’s easy to skip. Some clients build the next <code>References</code> only from the parent’s <code>References</code>. If mine were empty, the customer’s next email could arrive without the original id anywhere in it, and the thread would break on the third message.</p>\n<h2 id=\"forwarding-keeps-the-headers\" data-level=\"3\">Forwarding Keeps the Headers</h2>\n<p>All of this works through a plain forwarding rule. When mail reaches RelayMail through Gmail forwarding, an Outlook rule, a Google Group or an alias, these headers come along unchanged. So I never needed access to anyone’s mailbox to thread correctly. Everything the matching needs travels inside the message itself.</p>\n<p>That’s the whole system: one id stored per thread, <code>In-Reply-To</code> then <code>References</code> on the way in, both headers set on the way out, and a match only counts in the inbox’s own channel.</p>","created_at":"2026-11-14T14:00:00.000Z","updated_at":"2026-11-14T14:00:00.000Z"}