Email-in replies thread wrongly

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:

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:

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