# 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:** 19
**Page:** 1

<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: [March 17, 2017, 5:34pm UTC](https://meta.discourse.org/t/email-in-replies-thread-wrongly/59365/1 "2017-03-17T17:34:33Z")

</div>

Frequently (possibly even always), replies by email show up in the web interface as a reply to the wrong post.

Here’s one such example:

 ![](https://global.discourse-cdn.com/meta/original/3X/1/5/15c88f8d1a81b3d456ee1e4a82beb1a5e29607cb.png)

The reply key of the email yousef was attempting to respond to here was: `d3ce04730acfc6ea2343a867dd5f171f`

And (some of) the raw text of that email:

```
Delivered-To: discourse+d3ce04730acfc6ea2343a867dd5f171f@mozilla-community.org
Subject: Re: [Testing] Test post of a conversation
To: Mozilla Discourse
 <discourse+d3ce04730acfc6ea2343a867dd5f171f@mozilla-community.org>
References: <topic/14479@discourse.mozilla-community.org>
 <0100015adc4df3e6-373a7dd3-d6d4-474e-916a-07ea85c7ab3d-000000@email.amazonses.com>
In-Reply-To: <0100015adc4df3e6-373a7dd3-d6d4-474e-916a-07ea85c7ab3d-000000@email.amazonses.com>
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit

And this is a response by email.

Thunderbird rules!

On 17/03/17 12:46, GeorgeRoter wrote:
> [george] george <https://discourse.mozilla-community.org/users/george>
> GeorgeRoter <https://discourse.mozilla-community.org/users/george>
> March 17
> 
> george:
> 
> [33] At vero eos et accusamus et iusto odio dignissimos ducimus, qui
> blanditiis praesentium voluptatum deleniti atque corrupti, quos
> dolores et quas molestias excepturi sint, obcaecati cupiditate non
> provident, similique sunt in culpa, qui officia deserunt mollitia
> animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis
> est et expedita distinctio. Nam libero tempore, cum soluta nobis est
> eligendi optio, cumque nihil impedit, quo minus id, quod maxime
> placeat, facere possimus, omnis voluptas assumenda est, omnis dolor
> repellendus. Temporibus autem quibusdam et aut officiis debitis aut
> rerum necessitatibus saepe eveniet, ut et voluptates repudiandae
> sint et molestiae non recusandae. Itaque earum rerum hic tenetur a
> sapiente delectus, ut aut reiciendis voluptatibus maiores
> 
> This is a response to this big block of text using the web interface.
> 
> ------------------------------------------------------------------------
> 
> Visit Topic
> <https://discourse.mozilla-community.org/t/test-post-of-a-conversation/14479/2>
> or reply to this email to respond.
> 

```

And here’s another:

 ![](https://global.discourse-cdn.com/meta/original/3X/d/6/d6f43140a330a237313f1cab9267938c0a80fc8f.png)

With the reply key of the email Yousef was attempting to reply to here being: `eb1cfa58abb6c528c7fd5519577b0d0f`

Again with (some of) the raw email:

```
Delivered-To: discourse+eb1cfa58abb6c528c7fd5519577b0d0f@mozilla-community.org
Subject: Re: [Testing] Test post of a conversation
To: Mozilla Discourse
 <discourse+eb1cfa58abb6c528c7fd5519577b0d0f@mozilla-community.org>
References: <...>
 <topic/14479@discourse.mozilla-community.org>
 <0100015adc5a4fec-69d3459d-059b-40ec-8bd2-29d8da582c94-000000@email.amazonses.com>
In-Reply-To: <0100015adc5a4fec-69d3459d-059b-40ec-8bd2-29d8da582c94-000000@email.amazonses.com>
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit

You should do this time

On 17/03/17 12:59, GeorgeRoter wrote:
> [george] george <https://discourse.mozilla-community.org/users/george>
> GeorgeRoter <https://discourse.mozilla-community.org/users/george>
> March 17
> 
> Do I get an email if I reply?
> 
> ------------------------------------------------------------------------
> 
> Visit Topic
> <https://discourse.mozilla-community.org/t/test-post-of-a-conversation/14479/4>
> or reply to this email to respond.
> 
> ------------------------------------------------------------------------
> 
> 
> In Reply To
> 
> [yousef] yousef <https://discourse.mozilla-community.org/users/yousef>
> Yousef Alam <https://discourse.mozilla-community.org/users/yousef> not a
> Community Sysadmin
> March 17
> 
> And this is a response by email. Thunderbird rules!
> 
> ------------------------------------------------------------------------
> 
> Visit Topic
> <https://discourse.mozilla-community.org/t/test-post-of-a-conversation/14479/4>
> or reply to this email to respond.
> 

```

Any ideas why this is happening?

---

<div class="post-metadata">

### Author: ![wesochuck](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wesochuck/32/108706_2.png) [@wesochuck](https://meta.discourse.org/u/wesochuck)
#### Post date: [March 17, 2017, 5:46pm UTC](https://meta.discourse.org/t/email-in-replies-thread-wrongly/59365/2 "2017-03-17T17:46:36Z")

</div>

Is it possible to see the header the inbound message (I think you can see that in the logs somewhere).

Just a guess here, but wondering if something is mangling or stripping the key in the reply address. You’d probably need to compare the inbound message with the reply-to information in the outbound discourse generated message.

This thread might have some helpful info too:

[https://meta.discourse.org/t/reply-keys-reply-email-addresses-not-always-used-mail-client-issues/38787](https://meta.discourse.org/t/reply-keys-reply-email-addresses-not-always-used-mail-client-issues/38787)

---

<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: [March 17, 2017, 6:07pm UTC](https://meta.discourse.org/t/email-in-replies-thread-wrongly/59365/3 "2017-03-17T18:07:35Z")

</div>

> [@wesochuck](#):
>
> Is it possible to see the header the inbound message (I think you can see that in the logs somewhere).

I didn’t want to expose anything private, but I’ve edited the original post to add a few more headers, and the reply keys of the posts Yousef was attempting to reply to, found from `admin/email/sent`. They all match up, so something weird is going on here.

Could it be something to do with these `References` and `In-Reply-To` headers?

---

<div class="post-metadata">

### Author: ![elijah](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/elijah/32/104055_2.png) [@elijah](https://meta.discourse.org/u/elijah)
#### Post date: [March 17, 2017, 6:32pm UTC](https://meta.discourse.org/t/email-in-replies-thread-wrongly/59365/4 "2017-03-17T18:32:09Z")

</div>

The References/In-Reply-To headers match each other, but not what I thought Discourse was using:

> [@Discourse Emails not threaded properly in some Email clients](https://meta.discourse.org/t/emails-not-threaded/44444/2):
>
> Here’s what we use when we send an email post\_identifier = "\<topic/#{topic\_id}/#{post\_id}@#{host}\>" @message.header['Message-ID'] = post\_identifier This looks correct to me, we set the Message-Id header to the Id of the post. Your mail client should use that as the In-Reply-To header.

Edit:

I looked through some of the mail I’ve gotten from Meta, and see that the message IDs match the description given above:

```plaintext
From: Jay Pfaffman <meta@discoursemail.com>
Reply-To: Discourse Meta <meta+785bdce[...]@discoursemail.com>
To: [my discourse email address]
Message-ID: <topic/59203/270905@meta.discourse.org>
In-Reply-To: <topic/59203/270854@meta.discourse.org>
References: <topic/59203/270854@meta.discourse.org>
 <topic/59203@meta.discourse.org>

```

So, yes, I think the threading issue might be related to the `Message-ID` getting replaced.

---

<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?

---

<div class="post-metadata">

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

</div>

> [@LeoMcA](#):
>
> 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:

Huh, they even document that:

> **[Amazon SES header fields - Amazon Simple Email Service](https://docs.aws.amazon.com/ses/latest/dg/header-fields.html)**
>
> Describes the header fields that Amazon SES accepts.

> **Note** : Amazon SES overrides any Message-ID header you provide.

I don’t think the RFCs _prohibit_ that behavior, but it is certainly not what I’d consider _expected_.

> [@LeoMcA](#):
>
> 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?

It makes me cry a little, and I have to wonder what threading aware mail clients would do with that. If possible a verp-style variable Reply-To address would be much cleaner.

---

<div class="post-metadata">

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

</div>

I don’t suppose avoiding Amazon’s Spam Enablement Service is an option? Seriously, rewriting message IDs is head-desk-grade stupid.

---

<div class="post-metadata">

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

</div>

Some senders DKIM sign the message-id header, for example gmail:

```plaintext
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=mime-version:from:date:message-id:subject:to;
        bh=X5y5VSxfoLUD+lLIECebvv3q3y3QpWBKH7gRs5bnXag=;
        b=y0xBqa28mfXxCYtJ0f4Lqv+e7Qe5rJU8/MSppGQXysTIm7dzZazLcpB7hsZ/UtfqPs

```

Checking it does not look like Discourse is among those senders:

```plaintext
DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=discoursemail.com;
        s=sjc1; t=1494307180;
        bh=IqUdzg/T+JaQf72JvmWPvxOpfssOWEuOq0KRnDr12bU=;
        h=Date:From:Reply-To:To:Subject:List-Unsubscribe;
        b=uDaKehxZeEYu7YHrKNDZfeFP+iUylNwgyPATymo2LDpFkJrvUWq/gH5ZyW5zux6oG

```

I wonder if Amazon could be persuaded not muck with message-IDs if they were signed.

---

<div class="post-metadata">

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

</div>

Mail sent from us shouldn’t go through SES, so DKIM signing isn’t going to help people stuck using SES.

---

<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, 3:19pm UTC](https://meta.discourse.org/t/email-in-replies-thread-wrongly/59365/10 "2017-05-10T15:19:36Z")

</div>

> [@mpalmer](#):
>
> I don’t suppose avoiding Amazon’s Spam Enablement Service is an option?

I’ve discussed it with my team, and we’re not immediately sure we have the budget for any other option. It’s cheap, but I guess there’s a reason why…

I also expect there’s a lot of other Discourse sites out there using SES, and it would be nice to come up with a fix for them too.

> [@elijah](#):
>
> If possible a verp-style variable Reply-To address would be much cleaner.

I’m not sure if this is what you were getting at, but it occurs to me that the reply key of each email is unique, and gets returned with every email-in. So if we were to (again, behind a pref, or conditionally on the SMTP creds being SES ones) on send store the reply key and associated post id in the db, and on receive fetch that post id from the db with the reply key, we’d be able to associate replies with their parents without making anyone at the IETF cry.

---

<div class="post-metadata">

### Author: ![elijah](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/elijah/32/104055_2.png) [@elijah](https://meta.discourse.org/u/elijah)
#### Post date: [May 10, 2017, 5:28pm UTC](https://meta.discourse.org/t/email-in-replies-thread-wrongly/59365/11 "2017-05-10T17:28:47Z")

</div>

> [@LeoMcA](#):
>
> I’m not sure if this is what you were getting at, but it occurs to me that the reply key of each email is unique, and gets returned with every email-in. So if we were to (again, behind a pref, or conditionally on the SMTP creds being SES ones) on send store the reply key and associated post id in the db, and on receive fetch that post id from the db with the reply key, we’d be able to associate replies with their parents without making anyone at the IETF cry.

That is almost exactly what I meant. I just thought you could stick a `+topic/ID/POST` in the address, so you could just parse it out of there, if needed.

---

<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, 6:56pm UTC](https://meta.discourse.org/t/email-in-replies-thread-wrongly/59365/12 "2017-05-10T18:56:57Z")

</div>

> [@LeoMcA](#):
>
> I’ve discussed it with my team, and we’re not immediately sure we have the budget for any other option. It’s cheap, but I guess there’s a reason why…

After doing some investigation into the prices of various services, this isn’t actually right. For the current numbers of emails we send (~60,000/month), SparkPost is _free_, and is cheaper than AWS up to ~120,000/month. Past that it still remains very competitive with AWS. Nothing else really comes close. [(All the data, and a slightly horrendous chart, can be found here.)](https://docs.google.com/spreadsheets/d/1fo39HsTr1ZSYB-k9AdgfgBNYjkzkoSsZc2NrJEy2KaE/edit?usp=sharing)

---

<div class="post-metadata">

### Author: ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)
#### Post date: [May 10, 2017, 7:33pm UTC](https://meta.discourse.org/t/email-in-replies-thread-wrongly/59365/13 "2017-05-10T19:33:52Z")

</div>

> [@LeoMcA](#):
>
> SparkPost is free

I highly recommend setting up this:

> [@Configure VERP to handle bouncing e-mails](https://meta.discourse.org/t/handling-bouncing-e-mails/45343):
>
> Setting up bounces handling Discourse uses the [Variable envelope return path (VERP)](https://en.wikipedia.org/wiki/Variable_envelope_return_path) technique to handle bouncing e-mails. To enable VERP, set the “reply by email address” site setting with an email address template that uses the + sign. The default of replies+%{reply\_key}@discourse.example.com … works fine. If you are using your own or your company’s email server, then, that’s it tada Your discourse instance must be able to receive email sent to this address. This should work via POP, bu…

Note that with all mail providers you will likely bump into deliverability issues, “free” services slightly more so - keep complaining at them if you do. Also consider breaking in a mail IP slowly if you do go that paid private IP route.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [May 10, 2017, 10:10pm UTC](https://meta.discourse.org/t/email-in-replies-thread-wrongly/59365/14 "2017-05-10T22:10:46Z")

</div>

That is fine, but I strongly recommend you pay for a dedicated IP in your mail services. Otherwise deliverability is a crapshoot.

---

<div class="post-metadata">

### Author: ![iva](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/iva/32/120762_2.png) [@iva](https://meta.discourse.org/u/iva)
#### Post date: [June 17, 2017, 4:56pm UTC](https://meta.discourse.org/t/email-in-replies-thread-wrongly/59365/15 "2017-06-17T16:56:26Z")

</div>

Can anyone please clarify/confirm this for me: is it possible at all to use Amazon SES for emails with Discourse and have reply-by-email working correctly, or is it known not to work because of the Message-IDs being rewritten?

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [June 17, 2017, 9:30pm UTC](https://meta.discourse.org/t/email-in-replies-thread-wrongly/59365/16 "2017-06-17T21:30:36Z")

</div>

Unless Amazon has changed something, it looks like this has graduated to Known Issue, Don’t Do That.

---

<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: [June 19, 2017, 11:28am UTC](https://meta.discourse.org/t/email-in-replies-thread-wrongly/59365/17 "2017-06-19T11:28:06Z")

</div>

There was a lot of resistance to move away from SES, so I went about finding a technical solution.

As it turns out, it was incredibly simple, with a very well placed `return if`:

[https://github.com/discourse/discourse/pull/4938](https://github.com/discourse/discourse/pull/4938)

> [@riking](#):
>
> Unless Amazon has changed something, it looks like this has graduated to Known Issue, Don’t Do That.

How about graduating this to Known Issue, Toggle This Conveniently Placed Setting?

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [June 22, 2017, 2:17pm UTC](https://meta.discourse.org/t/email-in-replies-thread-wrongly/59365/19 "2017-06-22T14:17:07Z")

</div>

![](https://global.discourse-cdn.com/meta/original/3X/0/3/030d613f96b9b021cbd936eca9130f95e03fc6c4.gif)

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [June 23, 2017, 4:00pm UTC](https://meta.discourse.org/t/email-in-replies-thread-wrongly/59365/20 "2017-06-23T16:00:00Z")

</div>

This topic was automatically closed after 25 hours. New replies are no longer allowed.
