mirror of
https://github.com/discourse/discourse.git
synced 2025-05-31 04:08:14 +08:00
FIX: Add random suffix to outbound Message-ID for email (#15179)
Currently the Message-IDs we send out for outbound email are not unique; for a post they look like: topic/TOPIC_ID/POST_ID@HOST And for a topic they look like: topic/TOPIC_ID@HOST This commit changes the outbound Message-IDs to also have a random suffix before the host, so the new format is like this: topic/TOPIC_ID/POST_ID.RANDOM_SUFFIX@HOST Or: topic/TOPIC_ID.RANDOM_SUFFIX@HOST This should help with email deliverability. This change is backwards-compatible, the old Message-ID format will still be recognized in the mail receiver flow, so people will still be able to reply using Message-IDs, In-Reply-To, and References headers that have already been sent. This commit also refactors Message-ID related logic to a central location, and adds judicious amounts of tests and documentation.
This commit is contained in:
@ -260,6 +260,7 @@ describe Email::Sender do
|
||||
end
|
||||
|
||||
context "email threading" do
|
||||
let(:random_message_id_suffix) { "5f1330cfd941f323d7f99b9e" }
|
||||
fab!(:topic) { Fabricate(:topic) }
|
||||
|
||||
fab!(:post_1) { Fabricate(:post, topic: topic, post_number: 1) }
|
||||
@ -271,14 +272,17 @@ describe Email::Sender do
|
||||
let!(:post_reply_2_4) { PostReply.create(post: post_2, reply: post_4) }
|
||||
let!(:post_reply_3_4) { PostReply.create(post: post_3, reply: post_4) }
|
||||
|
||||
before { message.header['X-Discourse-Topic-Id'] = topic.id }
|
||||
before do
|
||||
message.header['X-Discourse-Topic-Id'] = topic.id
|
||||
Email::MessageIdService.stubs(:random_suffix).returns(random_message_id_suffix)
|
||||
end
|
||||
|
||||
it "doesn't set the 'In-Reply-To' and 'References' headers on the first post" do
|
||||
message.header['X-Discourse-Post-Id'] = post_1.id
|
||||
|
||||
email_sender.send
|
||||
|
||||
expect(message.header['Message-Id'].to_s).to eq("<topic/#{topic.id}@test.localhost>")
|
||||
expect(message.header['Message-Id'].to_s).to eq("<topic/#{topic.id}.#{random_message_id_suffix}@test.localhost>")
|
||||
expect(message.header['In-Reply-To'].to_s).to be_blank
|
||||
expect(message.header['References'].to_s).to be_blank
|
||||
end
|
||||
@ -288,8 +292,8 @@ describe Email::Sender do
|
||||
|
||||
email_sender.send
|
||||
|
||||
expect(message.header['Message-Id'].to_s).to eq("<topic/#{topic.id}/#{post_2.id}@test.localhost>")
|
||||
expect(message.header['In-Reply-To'].to_s).to eq("<topic/#{topic.id}@test.localhost>")
|
||||
expect(message.header['Message-Id'].to_s).to eq("<topic/#{topic.id}/#{post_2.id}.#{random_message_id_suffix}@test.localhost>")
|
||||
expect(message.header['In-Reply-To'].to_s).to eq("<topic/#{topic.id}.#{random_message_id_suffix}@test.localhost>")
|
||||
end
|
||||
|
||||
it "sets the 'In-Reply-To' header to the newest replied post" do
|
||||
@ -297,8 +301,8 @@ describe Email::Sender do
|
||||
|
||||
email_sender.send
|
||||
|
||||
expect(message.header['Message-Id'].to_s).to eq("<topic/#{topic.id}/#{post_4.id}@test.localhost>")
|
||||
expect(message.header['In-Reply-To'].to_s).to eq("<topic/#{topic.id}/#{post_3.id}@test.localhost>")
|
||||
expect(message.header['Message-Id'].to_s).to eq("<topic/#{topic.id}/#{post_4.id}.#{random_message_id_suffix}@test.localhost>")
|
||||
expect(message.header['In-Reply-To'].to_s).to eq("<topic/#{topic.id}/#{post_3.id}.#{random_message_id_suffix}@test.localhost>")
|
||||
end
|
||||
|
||||
it "sets the 'References' header to the topic and all replied posts" do
|
||||
@ -307,9 +311,9 @@ describe Email::Sender do
|
||||
email_sender.send
|
||||
|
||||
references = [
|
||||
"<topic/#{topic.id}@test.localhost>",
|
||||
"<topic/#{topic.id}/#{post_3.id}@test.localhost>",
|
||||
"<topic/#{topic.id}/#{post_2.id}@test.localhost>",
|
||||
"<topic/#{topic.id}.#{random_message_id_suffix}@test.localhost>",
|
||||
"<topic/#{topic.id}/#{post_3.id}.#{random_message_id_suffix}@test.localhost>",
|
||||
"<topic/#{topic.id}/#{post_2.id}.#{random_message_id_suffix}@test.localhost>",
|
||||
]
|
||||
|
||||
expect(message.header['References'].to_s).to eq(references.join(" "))
|
||||
@ -328,7 +332,7 @@ describe Email::Sender do
|
||||
|
||||
references = [
|
||||
"<#{topic_incoming_email.message_id}>",
|
||||
"<topic/#{topic.id}/#{post_3.id}@test.localhost>",
|
||||
"<topic/#{topic.id}/#{post_3.id}.#{random_message_id_suffix}@test.localhost>",
|
||||
"<#{post_2_incoming_email.message_id}>",
|
||||
]
|
||||
|
||||
|
Reference in New Issue
Block a user