From 4d889f2ef82c12e4eb8d548eb77e9f81418ecc9d Mon Sep 17 00:00:00 2001 From: zogstrip Date: Tue, 31 Dec 2024 15:04:42 +0100 Subject: [PATCH] FEATURE: add support for One-Click unsubscribe (RFC 8058) We were missing the "List-Unsubscribe-Post" header in emails we sent to allow Yahoo / GMail and others to automagically show a link to unsubscribe. Internal ref - t/144713 --- lib/email/message_builder.rb | 10 +++++++--- spec/lib/email/message_builder_spec.rb | 6 ++++++ spec/mailers/user_notifications_spec.rb | 2 ++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/email/message_builder.rb b/lib/email/message_builder.rb index eff1a4f9efd..1284ba93727 100644 --- a/lib/email/message_builder.rb +++ b/lib/email/message_builder.rb @@ -223,10 +223,14 @@ module Email def header_args result = {} + if @opts[:add_unsubscribe_link] - unsubscribe_url = - @template_args[:unsubscribe_url].presence || @template_args[:user_preferences_url] - result["List-Unsubscribe"] = "<#{unsubscribe_url}>" + if unsubscribe_url = @template_args[:unsubscribe_url].presence + result["List-Unsubscribe"] = "<#{unsubscribe_url}>" + result["List-Unsubscribe-Post"] = "List-Unsubscribe=One-Click" + else + result["List-Unsubscribe"] = "<#{@template_args[:user_preferences_url]}>" + end end result["X-Discourse-Post-Id"] = @opts[:post_id].to_s if @opts[:post_id] diff --git a/spec/lib/email/message_builder_spec.rb b/spec/lib/email/message_builder_spec.rb index 9b27835a2b9..cf3e0ee93f5 100644 --- a/spec/lib/email/message_builder_spec.rb +++ b/spec/lib/email/message_builder_spec.rb @@ -335,6 +335,12 @@ RSpec.describe Email::MessageBuilder do expect(message_with_unsubscribe.header_args["List-Unsubscribe"]).to be_present end + it "has the List-Unsubscribe-Post header" do + expect(message_with_unsubscribe.header_args["List-Unsubscribe-Post"]).to eq( + "List-Unsubscribe=One-Click", + ) + end + it "has the unsubscribe url in the body" do expect(message_with_unsubscribe.body).to match("/t/1234/unsubscribe") end diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb index 5ab50623a9c..7d072ea6a89 100644 --- a/spec/mailers/user_notifications_spec.rb +++ b/spec/mailers/user_notifications_spec.rb @@ -200,6 +200,7 @@ RSpec.describe UserNotifications do expect(email.html_part.body.to_s).to be_present expect(email.text_part.body.to_s).to be_present expect(email.header["List-Unsubscribe"].to_s).to match(/\/email\/unsubscribe\/\h{64}/) + expect(email.header["List-Unsubscribe-Post"].to_s).to eq("List-Unsubscribe=One-Click") expect(email.header["X-Discourse-Topic-Ids"].to_s).to eq( "#{another_popular_topic.id},#{popular_topic.id}", ) @@ -437,6 +438,7 @@ RSpec.describe UserNotifications do expect(email.header["List-Unsubscribe"].to_s).to match( /http:\/\/test.localhost\/forum\/email\/unsubscribe\/\h{64}/, ) + expect(email.header["List-Unsubscribe-Post"].to_s).to eq("List-Unsubscribe=One-Click") topic_url = "http://test.localhost/forum/t/#{popular_topic.slug}/#{popular_topic.id}" expect(html).to include(topic_url)