# Email-in replies thread wrongly

**URL:** https://meta.discourse.org/t/email-in-replies-thread-wrongly/59365
**Category:** Bug
**Created:** [March 17, 2017, 5:34pm UTC](https://meta.discourse.org/t/email-in-replies-thread-wrongly/59365 "2017-03-17T17:34:33Z")
**Posts on this page:** 1
**Showing post:** 5

<div class="post-metadata">

### Author: ![LeoMcA](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/leomca/32/87233_2.png) [@LeoMcA](https://meta.discourse.org/u/LeoMcA)
#### Post date: [May 10, 2017, 12:09am UTC](https://meta.discourse.org/t/email-in-replies-thread-wrongly/59365/5 "2017-05-10T00:09:25Z")

</div>

> [@elijah](#):
>
> So, yes, I think the threading issue might be related to the Message-ID getting replaced.

Yeah, I looked into this a bit more and it’s definitely because Amazon SES is replacing the `Message-ID` header with its own, like so:

```
Message-ID: <0100015bed8ae28f-b5d62ad9-[...]@email.amazonses.com>
In-Reply-To: <da30d2bd-ef4a-[...]@mozilla.com>
References: <da30d2bd-ef4a-[...]@mozilla.com>
 <topic/15495@discourse.mozilla-community.org>

```

Then, for the headers of an email-in reply, as per section 3.6.4. of [RFC 2822](https://www.ietf.org/rfc/rfc2822.txt):

> The “In-Reply-To:” field will contain the contents of the “Message-ID:” field of the message to which this one is a reply (the “parent message”).

And:

> The “References:” field will contain the contents of the parent’s “References:” field (if any) followed by the contents of the parent’s “Message-ID:” field (if any).

Which Discourse uses to determine which post an email-in is replying to:

> <https://github.com/discourse/discourse/blob/907f6cd76be55e6a9a0df8140921a4ce1a1d40d4/lib/email/receiver.rb#L479>

But, since the email-in has Amazon’s `Message-ID` in the `In-Reply-To` and `References` headers, Discourse is unable to find the correct post.

A quick and dirty fix would seem to be to optionally add the wanted `Message-ID` to the `References` header on the emails Discourse sends out, with something like this added to [`lib/email/sender.rb`](https://github.com/discourse/discourse/blob/907f6cd76be55e6a9a0df8140921a4ce1a1d40d4/lib/email/sender.rb#L116):

```plaintext
if SiteSetting.message_id_stripped?
  if post.post_number == 1
    @message.header['References'] = topic_message_id
  else
    @message.header['References'] = [referenced_post_message_ids, topic_message_id, post_message_id].flatten.compact.uniq
  end
end

```

This would then mean that the correct, Discourse-specified, `Message-ID` would be in the `References` header, and Discourse would be able to find the correct post to reply to.

However, I expect this would make whoever wrote the RFC cry a little. @zogstrip you seem to be the email master around here, what do you think?

---

_[View the full topic](https://meta.discourse.org/t/email-in-replies-thread-wrongly/59365)._
