diff --git a/app/mailers/concerns/mailer_headers_configurable_concern.rb b/app/mailers/concerns/mailer_headers_configurable_concern.rb new file mode 100644 index 000000000..41eec28cd --- /dev/null +++ b/app/mailers/concerns/mailer_headers_configurable_concern.rb @@ -0,0 +1,26 @@ +module MailerHeadersConfigurableConcern + extend ActiveSupport::Concern + + included do + def configure_defaults_for_user(user) + I18n.locale = user.locale + + if user.preferred_domain_demarches_gouv_fr? + Current.application_name = "demarches.gouv.fr" + Current.host = ENV.fetch("APP_HOST") + Current.contact_email = "contact@demarches.gouv.fr" + Current.no_reply_email = NO_REPLY_EMAIL.sub("demarches-simplifiees.fr", "demarches.gouv.fr") # rubocop:disable DS/ApplicationName + else + Current.application_name = APPLICATION_NAME + Current.host = ENV["APP_HOST_LEGACY"] || ENV.fetch("APP_HOST") # _LEGACY is optional, fallbagck to default when unset + Current.contact_email = CONTACT_EMAIL + Current.no_reply_email = NO_REPLY_EMAIL + end + + from = "#{Current.application_name} <#{Current.contact_email}>" + self.class.default from: from, reply_to: from + self.class.default_url_options = { host: Current.host } + self.class.asset_host = Current.application_base_url + end + end +end diff --git a/app/mailers/devise_user_mailer.rb b/app/mailers/devise_user_mailer.rb index 4c910341f..a37931415 100644 --- a/app/mailers/devise_user_mailer.rb +++ b/app/mailers/devise_user_mailer.rb @@ -5,6 +5,7 @@ class DeviseUserMailer < Devise::Mailer include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url` include MailerDolistConcern include MailerMonitoringConcern + include MailerHeadersConfigurableConcern include BalancedDeliveryConcern include PriorityDeliveryConcern @@ -15,7 +16,10 @@ class DeviseUserMailer < Devise::Mailer end def confirmation_instructions(record, token, opts = {}) - opts[:from] = NO_REPLY_EMAIL + configure_defaults_for_user(record) + + opts[:from] = Current.no_reply_email + opts[:reply_to] = Current.no_reply_email @procedure = opts[:procedure_after_confirmation] || nil @prefill_token = opts[:prefill_token] super diff --git a/app/models/current.rb b/app/models/current.rb index 62d42747c..4b18adc10 100644 --- a/app/models/current.rb +++ b/app/models/current.rb @@ -1,5 +1,10 @@ class Current < ActiveSupport::CurrentAttributes - attribute :user, :request_id, :browser, - :host, :application_name, :contact_email, - :application_base_url + attribute :application_base_url + attribute :application_name + attribute :browser + attribute :contact_email + attribute :host + attribute :no_reply_email + attribute :request_id + attribute :user end diff --git a/spec/mailers/devise_user_mailer_spec.rb b/spec/mailers/devise_user_mailer_spec.rb index e0093028c..4f92bd4be 100644 --- a/spec/mailers/devise_user_mailer_spec.rb +++ b/spec/mailers/devise_user_mailer_spec.rb @@ -16,8 +16,37 @@ RSpec.describe DeviseUserMailer, type: :mailer do context 'when perform_later is called' do it 'enqueues email in default queue for high priority delivery' do - expect { subject.deliver_later }.to have_enqueued_job.on_queue(Rails.application.config.action_mailer.deliver_later_queue_name) -end + expect { subject.deliver_later }.to have_enqueued_job.on_queue(Rails.application.config.action_mailer.deliver_later_queue_name) + end end end + + describe 'headers for user' do + context "confirmation email" do + subject { described_class.confirmation_instructions(user, token, opts = {}) } + + context "legacy domain" do + it "respect preferred domain" do + expect(header_value("From", subject.message)).to eq(NO_REPLY_EMAIL) + expect(header_value("Reply-To", subject.message)).to eq(NO_REPLY_EMAIL) + expect(subject.message.to_s).to include("#{ENV.fetch("APP_HOST_LEGACY")}/users/confirmation") + end + end + + context "new domain" do + let(:user) { create(:user, preferred_domain: :demarches_gouv_fr) } + + it "respect preferred domain" do + expect(header_value("From", subject.message)).to eq("Ne pas répondre ") + expect(header_value("Reply-To", subject.message)).to eq("Ne pas répondre ") + expect(subject.message.to_s).to include("#{ENV.fetch("APP_HOST")}/users/confirmation") + expect(subject.message.to_s).to include("//#{ENV.fetch("APP_HOST")}/assets/mailer/republique") + end + end + end + end + + def header_value(name, message) + message.header.fields.find { _1.name == name }.value + end end