Email-in replies thread wrongly

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:

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:

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?

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

3 Likes

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?

1 Like

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

Edit:

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

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.

3 Likes

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:

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:

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:

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?

3 Likes

Huh, they even document that:

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.

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.

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

6 Likes

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

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:

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.

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

3 Likes

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.

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.

2 Likes

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.

1 Like

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.)

5 Likes

I highly recommend setting up this:

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.

1 Like

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

3 Likes

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?

1 Like

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

3 Likes

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:

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

7 Likes

3 Likes

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