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:
Martin Brennan
2021-12-06 10:34:39 +10:00
committed by GitHub
parent 11d1c520ff
commit 3b13f1146b
12 changed files with 304 additions and 104 deletions

View File

@ -109,7 +109,7 @@ module Email
).pluck_first(:id)
# always set a default Message ID from the host
@message.header['Message-ID'] = "<#{SecureRandom.uuid}@#{host}>"
@message.header['Message-ID'] = Email::MessageIdService.generate_default
if topic_id.present? && post_id.present?
post = Post.find_by(id: post_id, topic_id: topic_id)
@ -121,15 +121,9 @@ module Email
return skip(SkippedEmailLog.reason_types[:sender_topic_deleted]) if topic.blank?
add_attachments(post)
first_post = topic.ordered_posts.first
topic_message_id = first_post.incoming_email&.message_id.present? ?
"<#{first_post.incoming_email.message_id}>" :
"<topic/#{topic_id}@#{host}>"
post_message_id = post.incoming_email&.message_id.present? ?
"<#{post.incoming_email.message_id}>" :
"<topic/#{topic_id}/#{post_id}@#{host}>"
topic_message_id = Email::MessageIdService.generate_for_topic(topic, use_incoming_email_if_present: true)
post_message_id = Email::MessageIdService.generate_for_post(post, use_incoming_email_if_present: true)
referenced_posts = Post.includes(:incoming_email)
.joins("INNER JOIN post_replies ON post_replies.post_id = posts.id ")
@ -141,9 +135,9 @@ module Email
"<#{referenced_post.incoming_email.message_id}>"
else
if referenced_post.post_number == 1
"<topic/#{topic_id}@#{host}>"
Email::MessageIdService.generate_for_topic(topic)
else
"<topic/#{topic_id}/#{referenced_post.id}@#{host}>"
Email::MessageIdService.generate_for_post(referenced_post)
end
end
end