Skip to content

Instantly share code, notes, and snippets.

@fbuys
Last active October 19, 2021 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fbuys/b9897096a5863ff754dce9457054edcd to your computer and use it in GitHub Desktop.
Save fbuys/b9897096a5863ff754dce9457054edcd to your computer and use it in GitHub Desktop.
Threaded emails: Final result
class NotificationMailer < ApplicationMailer
def review_notification(recipient, review)
@review = review
@recipient = recipient
mail_settings = {
to: "#{recipient.name} <#{recipient.email}>",
subject: "Review: #{review.book.title}"
}.merge(headers(review))
mail(mail_settings)
end
private
def headers(review)
{
in_reply_to: thread_id(review),
references: thread_id(review)
}.reject { |_, v| v.blank? }
end
def message_id(review)
return "" if review.nil?
"<notification-#{review.id}-#{review.created_at.to_i}@#{ENV["domain"]}>"
end
def reviews_in_thread(review)
Review.where.not(id: review.id).where("book_id", review.book_id).order(:created_at)
end
def thread_id(review)
first_review = reviews_in_thread(review).first
message_id(first_review)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment