Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Distinguish reply-to mail header + DRY (#14292)
Kacper Mękarski4e8aab4e3e3e
diff --git a/spree/core/app/mailers/spree/admin_user_mailer.rb b/spree/core/app/mailers/spree/admin_user_mailer.rbindex 2191b6875d..80b872d1c2 100644--- a/spree/core/app/mailers/spree/admin_user_mailer.rb+++ b/spree/core/app/mailers/spree/admin_user_mailer.rb@@ -12,7 +12,6 @@ module Spreemail(to: admin_user.email,from: from_address,- reply_to: reply_to_address,subject: "#{store.name} #{Spree.t('admin_user_mailer.password_reset_email.subject')}",store_url: store.formatted_url)@@ -28,7 +27,6 @@ module Spreemail(to: admin_user.email,from: from_address,- reply_to: reply_to_address,subject: "#{store.name} #{Spree.t('admin_user_mailer.confirmation_email.subject')}",store_url: store.formatted_url)diff --git a/spree/core/app/mailers/spree/base_mailer.rb b/spree/core/app/mailers/spree/base_mailer.rbindex 39e9e80331..37645f3643 100644--- a/spree/core/app/mailers/spree/base_mailer.rb+++ b/spree/core/app/mailers/spree/base_mailer.rb@@ -2,6 +2,8 @@ module Spreeclass BaseMailer < ActionMailer::Basehelper Spree::ImagesHelper+ default reply_to: -> { reply_to_address }+def current_store@current_store ||= @order&.store.presence || Spree::Store.current || Spree::Store.defaultend@@ -39,7 +41,7 @@ module Spreeenddef reply_to_address- current_store.mail_from_address+ current_store.customer_support_email.presence || current_store.mail_from_addressenddef money(amount, currency = nil)diff --git a/spree/core/app/mailers/spree/export_mailer.rb b/spree/core/app/mailers/spree/export_mailer.rbindex 24150fcfb5..a304a7ba43 100644--- a/spree/core/app/mailers/spree/export_mailer.rb+++ b/spree/core/app/mailers/spree/export_mailer.rb@@ -8,7 +8,6 @@ module Spreeto: @export.user.email,subject: Spree.t('export_mailer.export_done.subject', export_number: @export.number).to_s,from: from_address,- reply_to: reply_to_address,store_url: current_store.url)enddiff --git a/spree/core/app/mailers/spree/invitation_mailer.rb b/spree/core/app/mailers/spree/invitation_mailer.rbindex 50e5ff6dff..d3580d34c1 100644--- a/spree/core/app/mailers/spree/invitation_mailer.rb+++ b/spree/core/app/mailers/spree/invitation_mailer.rb@@ -10,7 +10,6 @@ module Spreewith_store_locale(invitation.store) domail(to: invitation.email,from: from_address,- reply_to: reply_to_address,subject: Spree.t('invitation_mailer.invitation_email.subject',resource_name: invitation.resource&.name))end@@ -23,7 +22,6 @@ module Spreewith_store_locale(invitation.store) domail(to: invitation.inviter.email,from: from_address,- reply_to: reply_to_address,subject: Spree.t('invitation_mailer.invitation_accepted.subject',invitee_name: invitation.invitee&.name,resource_name: invitation.resource&.name))diff --git a/spree/core/app/mailers/spree/report_mailer.rb b/spree/core/app/mailers/spree/report_mailer.rbindex 12f521af28..50e612ca46 100644--- a/spree/core/app/mailers/spree/report_mailer.rb+++ b/spree/core/app/mailers/spree/report_mailer.rb@@ -7,8 +7,7 @@ module Spreemail(to: @report.user.email,subject: Spree.t('report_mailer.report_done.subject', report_name: @report.human_name).to_s,- from: from_address,- reply_to: reply_to_address+ from: from_address)endenddiff --git a/spree/core/app/models/spree/store.rb b/spree/core/app/models/spree/store.rbindex 30444e2ac0..7044b4f7cd 100644--- a/spree/core/app/models/spree/store.rb+++ b/spree/core/app/models/spree/store.rb@@ -142,6 +142,7 @@ module Spreevalidates :preferred_stock_reservation_ttl_minutes, numericality: { only_integer: true, greater_than: 0 }validates :preferred_storefront_access, inclusion: { in: Spree::Channel::Gating::STOREFRONT_ACCESS }validates :mail_from_address, email: { allow_blank: false }+ validates :customer_support_email, email: { allow_blank: true }# FIXME: we should remove this condition in v5if !ENV['SPREE_DISABLE_DB_CONNECTION'] &&connected? &&diff --git a/spree/core/spec/mailers/spree/base_mailer_spec.rb b/spree/core/spec/mailers/spree/base_mailer_spec.rbindex b0f742f973..7d5ee89411 100644--- a/spree/core/spec/mailers/spree/base_mailer_spec.rb+++ b/spree/core/spec/mailers/spree/base_mailer_spec.rb@@ -1,5 +1,11 @@require 'spec_helper'+class ReplyToProbeMailer < Spree::BaseMailer+ def sample+ mail(to: '[email protected]', from: '[email protected]', subject: 'probe', body: 'body')+ end+end+describe Spree::BaseMailer, type: :mailer dolet!(:store) { @default_store }@@ -25,6 +31,36 @@ describe Spree::BaseMailer, type: :mailer doendend+ describe '#reply_to_address' do+ subject(:mailer) { described_class.new }++ before { allow(mailer).to receive(:current_store).and_return(store) }++ context 'when the store has a customer support email' do+ let(:store) { create(:store, customer_support_email: '[email protected]', mail_from_address: '[email protected]') }++ it 'returns the customer support email' do+ expect(mailer.reply_to_address).to eq('[email protected]')+ end+ end++ context 'when the customer support email is blank' do+ let(:store) { create(:store, customer_support_email: '', mail_from_address: '[email protected]') }++ it 'falls back to the mail from address' do+ expect(mailer.reply_to_address).to eq('[email protected]')+ end+ end+ end++ describe 'default Reply-To header' do+ let!(:store) { @default_store }++ it 'is applied without the mailer passing reply_to explicitly' do+ expect(ReplyToProbeMailer.sample.reply_to).to eq([store.customer_support_email])+ end+ end+describe '#with_store_locale' dosubject(:mailer) { described_class.new }diff --git a/spree/core/spec/mailers/spree/invitation_mailer_spec.rb b/spree/core/spec/mailers/spree/invitation_mailer_spec.rbindex de0846f0cb..d09c4a6b87 100644--- a/spree/core/spec/mailers/spree/invitation_mailer_spec.rb+++ b/spree/core/spec/mailers/spree/invitation_mailer_spec.rb@@ -43,8 +43,8 @@ RSpec.describe Spree::InvitationMailer, type: :mailer doexpect(mail.from).to eq([store.mail_from_address])end- it 'sets reply-to as the store mail from address' do- expect(mail.reply_to).to eq([store.mail_from_address])+ it 'sets reply-to as the store customer support email' do+ expect(mail.reply_to).to eq([store.customer_support_email])end# The link target depends on whether the legacy `spree_admin` Rails gem@@ -131,8 +131,8 @@ RSpec.describe Spree::InvitationMailer, type: :mailer doexpect(mail.from).to eq([store.mail_from_address])end- it 'sets reply-to as the store mail from address' do- expect(mail.reply_to).to eq([store.mail_from_address])+ it 'sets reply-to as the store customer support email' do+ expect(mail.reply_to).to eq([store.customer_support_email])endendenddiff --git a/spree/core/spec/mailers/spree/report_mailer_spec.rb b/spree/core/spec/mailers/spree/report_mailer_spec.rbindex b47f15ab9e..b53a133e6e 100644--- a/spree/core/spec/mailers/spree/report_mailer_spec.rb+++ b/spree/core/spec/mailers/spree/report_mailer_spec.rb@@ -27,8 +27,8 @@ RSpec.describe Spree::ReportMailer, type: :mailer doexpect(mail.from).to eq([store.mail_from_address])end- it 'sets reply-to as the store mail from address' do- expect(mail.reply_to).to eq([store.mail_from_address])+ it 'sets reply-to as the store customer support email' do+ expect(mail.reply_to).to eq([store.customer_support_email])endit 'includes download link in the body' dodiff --git a/spree/core/spec/models/spree/store_spec.rb b/spree/core/spec/models/spree/store_spec.rbindex 91f9d030fe..6bec8e2169 100644--- a/spree/core/spec/models/spree/store_spec.rb+++ b/spree/core/spec/models/spree/store_spec.rb@@ -266,6 +266,24 @@ describe Spree::Store, type: :model, without_global_store: true doexpect(store.code).to eq('default')endend++ describe '#customer_support_email' do+ it 'is valid when blank' do+ store = build(:store, customer_support_email: '')+ expect(store).to be_valid+ end++ it 'is valid with a properly formatted email' do+ store = build(:store, customer_support_email: '[email protected]')+ expect(store).to be_valid+ end++ it 'is invalid with a malformed email' do+ store = build(:store, customer_support_email: 'not-an-email')+ expect(store).not_to be_valid+ expect(store.errors[:customer_support_email]).to be_present+ end+ endendcontext 'Translations' dodiff --git a/spree/emails/app/mailers/spree/customer_mailer.rb b/spree/emails/app/mailers/spree/customer_mailer.rbindex 17a9194a1b..07aa4a8cd5 100644--- a/spree/emails/app/mailers/spree/customer_mailer.rb+++ b/spree/emails/app/mailers/spree/customer_mailer.rb@@ -13,7 +13,6 @@ module Spreemail(to: user.email,from: from_address,- reply_to: reply_to_address,subject: "#{store.name} #{Spree.t('customer_mailer.password_reset_email.subject')}",store_url: store.storefront_url)diff --git a/spree/emails/app/mailers/spree/order_mailer.rb b/spree/emails/app/mailers/spree/order_mailer.rbindex b6d50924ef..788fc3b627 100644--- a/spree/emails/app/mailers/spree/order_mailer.rb+++ b/spree/emails/app/mailers/spree/order_mailer.rb@@ -7,7 +7,7 @@ module Spreecurrent_store = @order.storewith_store_locale(current_store, @order.locale) dosubject = order_email_subject(current_store, Spree.t('order_mailer.confirm_email.subject'), @order.number, resend: resend)- mail(to: @order.email, from: from_address, subject: subject, store_url: current_store.storefront_url, reply_to: reply_to_address)+ mail(to: @order.email, from: from_address, subject: subject, store_url: current_store.storefront_url)endend@@ -16,7 +16,7 @@ module Spreecurrent_store = @order.storewith_store_locale(current_store) dosubject = Spree.t('order_mailer.store_owner_notification_email.subject', store_name: current_store.name)- mail(to: current_store.new_order_notifications_email, from: from_address, subject: subject, store_url: current_store.storefront_url, reply_to: reply_to_address)+ mail(to: current_store.new_order_notifications_email, from: from_address, subject: subject, store_url: current_store.storefront_url)endend@@ -25,7 +25,7 @@ module Spreecurrent_store = @order.storewith_store_locale(current_store, @order.locale) dosubject = order_email_subject(current_store, Spree.t('order_mailer.cancel_email.subject'), @order.number, resend: resend)- mail(to: @order.email, from: from_address, subject: subject, store_url: current_store.storefront_url, reply_to: reply_to_address)+ mail(to: @order.email, from: from_address, subject: subject, store_url: current_store.storefront_url)endend@@ -36,7 +36,7 @@ module Spreewith_store_locale(@current_store, @order.locale) domail(to: @order.email, from: from_address, subject: Spree.t('order_mailer.payment_link_email.subject', number: @order.number),- store_url: @current_store.storefront_url, reply_to: reply_to_address)+ store_url: @current_store.storefront_url)endendenddiff --git a/spree/emails/app/mailers/spree/reimbursement_mailer.rb b/spree/emails/app/mailers/spree/reimbursement_mailer.rbindex 405220889e..ed94f4837c 100644--- a/spree/emails/app/mailers/spree/reimbursement_mailer.rb+++ b/spree/emails/app/mailers/spree/reimbursement_mailer.rb@@ -8,7 +8,7 @@ module Spreecurrent_store = @reimbursement.store || Spree::Store.defaultwith_store_locale(current_store, @order.locale) dosubject = order_email_subject(current_store, Spree.t('reimbursement_mailer.reimbursement_email.subject'), @order.number, resend: resend)- mail(to: @order.email, from: from_address, subject: subject, store_url: current_store.storefront_url, reply_to: reply_to_address)+ mail(to: @order.email, from: from_address, subject: subject, store_url: current_store.storefront_url)endendenddiff --git a/spree/emails/app/mailers/spree/shipment_mailer.rb b/spree/emails/app/mailers/spree/shipment_mailer.rbindex c05166914b..3f9f196a49 100644--- a/spree/emails/app/mailers/spree/shipment_mailer.rb+++ b/spree/emails/app/mailers/spree/shipment_mailer.rb@@ -9,7 +9,7 @@ module Spreecurrent_store = @shipment.storewith_store_locale(current_store, @order.locale) dosubject = order_email_subject(current_store, Spree.t('shipment_mailer.shipped_email.subject'), @order.number, resend: resend)- mail(to: @order.email, from: from_address, subject: subject, store_url: current_store.storefront_url, reply_to: reply_to_address)+ mail(to: @order.email, from: from_address, subject: subject, store_url: current_store.storefront_url)endendenddiff --git a/spree/emails/spec/mailers/spree/order_mailer_spec.rb b/spree/emails/spec/mailers/spree/order_mailer_spec.rbindex dbdd919846..5876fd6851 100644--- a/spree/emails/spec/mailers/spree/order_mailer_spec.rb+++ b/spree/emails/spec/mailers/spree/order_mailer_spec.rb@@ -41,11 +41,11 @@ describe Spree::OrderMailer, type: :mailer doendcontext ':reply_to not set explicitly' do- it 'uses store mail from address' do+ it 'uses store customer support email' domessage = described_class.confirm_email(order)- expect(message.reply_to).to eq([store.mail_from_address])+ expect(message.reply_to).to eq([store.customer_support_email])message = described_class.cancel_email(order)- expect(message.reply_to).to eq([store.mail_from_address])+ expect(message.reply_to).to eq([store.customer_support_email])endenddiff --git a/spree/emails/spec/mailers/spree/reimbursement_mailer_spec.rb b/spree/emails/spec/mailers/spree/reimbursement_mailer_spec.rbindex 123828c9b6..1ddf91cbed 100644--- a/spree/emails/spec/mailers/spree/reimbursement_mailer_spec.rb+++ b/spree/emails/spec/mailers/spree/reimbursement_mailer_spec.rb@@ -15,9 +15,9 @@ describe Spree::ReimbursementMailer, type: :mailer doendcontext ':reply_to not set explicitly' do- it 'uses store mail from address' do+ it 'uses store customer support email' domessage = Spree::ReimbursementMailer.reimbursement_email(reimbursement)- expect(message.reply_to).to eq [@default_store.mail_from_address]+ expect(message.reply_to).to eq [@default_store.customer_support_email]endenddiff --git a/spree/emails/spec/mailers/spree/shipment_mailer_spec.rb b/spree/emails/spec/mailers/spree/shipment_mailer_spec.rbindex 36ea241e99..e59a85c0fe 100644--- a/spree/emails/spec/mailers/spree/shipment_mailer_spec.rb+++ b/spree/emails/spec/mailers/spree/shipment_mailer_spec.rb@@ -22,9 +22,9 @@ describe Spree::ShipmentMailer, type: :mailer doendcontext ':reply_to not set explicitly' do- it 'falls back to store mail from address' do+ it 'uses store customer support email' domessage = described_class.shipped_email(shipment)- expect(message.reply_to).to eq([store.mail_from_address])+ expect(message.reply_to).to eq([store.customer_support_email])endend