diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 567618a6e..6a74f056e 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -818,6 +818,7 @@ en: title: "The name of this site, as used in the title tag." site_description: "Describe this site in one sentence, as used in the meta description tag." contact_email: "Email address of key contact responsible for this site. Used for critical notifications such as unhandled flags, as well as on the /about contact form for urgent matters." + bounce_email: "Variable Email Return Path used for emails, example: bounce@example.com will cause us to generate bounce+GUID@example.com as the Retrun Path for emails we send. This feature allows us to automatically disable bouncing emails. Requires additional configurations, leave blank if unsure." contact_url: "Contact URL for this site. Used on the /about contact form for urgent matters." queue_jobs: "DEVELOPER ONLY! WARNING! By default, queue jobs in sidekiq. If disabled, your site will be broken." crawl_images: "Retrieve images from remote URLs to insert the correct width and height dimensions." diff --git a/config/site_settings.yml b/config/site_settings.yml index c14c096ad..5d2019d3e 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -26,6 +26,10 @@ required: client: true default: '' type: email + bounce_email: + default: '' + type: email + shadowed_by_global: true contact_url: client: true default: '' diff --git a/db/migrate/20160418065403_add_bounce_key_to_email_log.rb b/db/migrate/20160418065403_add_bounce_key_to_email_log.rb new file mode 100644 index 000000000..683a26ff8 --- /dev/null +++ b/db/migrate/20160418065403_add_bounce_key_to_email_log.rb @@ -0,0 +1,5 @@ +class AddBounceKeyToEmailLog < ActiveRecord::Migration + def change + add_column :email_logs, :bounce_key, :string + end +end diff --git a/lib/email/sender.rb b/lib/email/sender.rb index 7c68f55ba..4a682b20c 100644 --- a/lib/email/sender.rb +++ b/lib/email/sender.rb @@ -116,6 +116,18 @@ module Email @message.header['List-Post'] = "" end + unless SiteSetting.bounce_email.blank? + email_log.bounce_key = SecureRandom.hex + address,domain = SiteSetting.bounce_email.split('@') + address << (address =~ /[+]/ ? "-" : '+') + address << email_log.bounce_key + + # WARNING: RFC claims you can not set the Return Path header, this is 100% correct + # however Rails has special handling for this header and ends up using this value + # as the Envelope From address so stuff works as expected + @message.header[:return_path] = "#{address}@#{domain}" + end + email_log.post_id = post_id if post_id.present? email_log.reply_key = reply_key if reply_key.present? diff --git a/spec/components/email/sender_spec.rb b/spec/components/email/sender_spec.rb index cb67f8165..073890b2f 100644 --- a/spec/components/email/sender_spec.rb +++ b/spec/components/email/sender_spec.rb @@ -77,6 +77,28 @@ describe Email::Sender do email_sender.send end + context "adds return_path correctly when no plus addressing" do + before do + SiteSetting.bounce_email = 'bounce@test.com' + end + + When { email_sender.send } + Then { + expect(message.header[:return_path].to_s).to eq("bounce+#{EmailLog.last.bounce_key}@test.com") + } + end + + context "adds return_path correctly with plus addressing" do + before do + SiteSetting.bounce_email = 'bounce+meta@test.com' + end + + When { email_sender.send } + Then { + expect(message.header[:return_path].to_s).to eq("bounce+meta-#{EmailLog.last.bounce_key}@test.com") + } + end + context "adds a List-ID header to identify the forum" do before do category = Fabricate(:category, name: 'Name With Space')