Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Set default from: address in BaseMailer so no need to calling it every time (#14294)

Kacper Mękarski3e4b53629450
diff --git a/spree/core/app/mailers/spree/admin_user_mailer.rb b/spree/core/app/mailers/spree/admin_user_mailer.rb
index 80b872d1c2..bba7da933b 100644
--- a/spree/core/app/mailers/spree/admin_user_mailer.rb
+++ b/spree/core/app/mailers/spree/admin_user_mailer.rb
@@ -11,7 +11,6 @@ module Spree
with_store_locale(store, preferred_locale(admin_user, store)) do
mail(
to: admin_user.email,
- from: from_address,
subject: "#{store.name} #{Spree.t('admin_user_mailer.password_reset_email.subject')}",
store_url: store.formatted_url
)
@@ -26,7 +25,6 @@ module Spree
with_store_locale(store, preferred_locale(admin_user, store)) do
mail(
to: admin_user.email,
- from: from_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.rb
index 37645f3643..186d1e6079 100644
--- a/spree/core/app/mailers/spree/base_mailer.rb
+++ b/spree/core/app/mailers/spree/base_mailer.rb
@@ -2,7 +2,7 @@ module Spree
class BaseMailer < ActionMailer::Base
helper Spree::ImagesHelper
- default reply_to: -> { reply_to_address }
+ default from: -> { from_address }, reply_to: -> { reply_to_address }
def current_store
@current_store ||= @order&.store.presence || Spree::Store.current || Spree::Store.default
diff --git a/spree/core/app/mailers/spree/export_mailer.rb b/spree/core/app/mailers/spree/export_mailer.rb
index a304a7ba43..1fe9f3ea82 100644
--- a/spree/core/app/mailers/spree/export_mailer.rb
+++ b/spree/core/app/mailers/spree/export_mailer.rb
@@ -7,7 +7,6 @@ module Spree
mail(
to: @export.user.email,
subject: Spree.t('export_mailer.export_done.subject', export_number: @export.number).to_s,
- from: from_address,
store_url: current_store.url
)
end
diff --git a/spree/core/app/mailers/spree/invitation_mailer.rb b/spree/core/app/mailers/spree/invitation_mailer.rb
index d3580d34c1..62c3c8840f 100644
--- a/spree/core/app/mailers/spree/invitation_mailer.rb
+++ b/spree/core/app/mailers/spree/invitation_mailer.rb
@@ -9,7 +9,6 @@ module Spree
@current_store = invitation.store
with_store_locale(invitation.store) do
mail(to: invitation.email,
- from: from_address,
subject: Spree.t('invitation_mailer.invitation_email.subject',
resource_name: invitation.resource&.name))
end
@@ -21,7 +20,6 @@ module Spree
@current_store = invitation.store
with_store_locale(invitation.store) do
mail(to: invitation.inviter.email,
- from: from_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.rb
index 50e612ca46..be782ca683 100644
--- a/spree/core/app/mailers/spree/report_mailer.rb
+++ b/spree/core/app/mailers/spree/report_mailer.rb
@@ -6,8 +6,7 @@ module Spree
with_store_locale(@report.store) do
mail(
to: @report.user.email,
- subject: Spree.t('report_mailer.report_done.subject', report_name: @report.human_name).to_s,
- from: from_address
+ subject: Spree.t('report_mailer.report_done.subject', report_name: @report.human_name).to_s
)
end
end
diff --git a/spree/core/app/mailers/spree/webhook_mailer.rb b/spree/core/app/mailers/spree/webhook_mailer.rb
index 6962dc3752..1920074347 100644
--- a/spree/core/app/mailers/spree/webhook_mailer.rb
+++ b/spree/core/app/mailers/spree/webhook_mailer.rb
@@ -9,7 +9,6 @@ module Spree
with_store_locale(webhook_endpoint.store) do
mail(
to: @current_store.new_order_notifications_email.presence || @current_store.mail_from_address,
- from: from_address,
subject: Spree.t('webhook_mailer.endpoint_disabled.subject', endpoint_name: @endpoint.name || @endpoint.url),
store_url: @current_store.formatted_url
)
diff --git a/spree/core/spec/mailers/spree/base_mailer_spec.rb b/spree/core/spec/mailers/spree/base_mailer_spec.rb
index 7d5ee89411..a983693512 100644
--- a/spree/core/spec/mailers/spree/base_mailer_spec.rb
+++ b/spree/core/spec/mailers/spree/base_mailer_spec.rb
@@ -6,6 +6,12 @@ class ReplyToProbeMailer < Spree::BaseMailer
end
end
+class FromProbeMailer < Spree::BaseMailer
+ def sample
+ mail(to: '[email protected]', subject: 'probe', body: 'body')
+ end
+end
+
describe Spree::BaseMailer, type: :mailer do
let!(:store) { @default_store }
@@ -31,6 +37,18 @@ describe Spree::BaseMailer, type: :mailer do
end
end
+ describe '#from_address' do
+ subject(:mailer) { described_class.new }
+
+ before { allow(mailer).to receive(:current_store).and_return(store) }
+
+ let(:store) { create(:store, mail_from_address: '[email protected]') }
+
+ it 'returns the store mail from address' do
+ expect(mailer.from_address).to eq('[email protected]')
+ end
+ end
+
describe '#reply_to_address' do
subject(:mailer) { described_class.new }
@@ -53,6 +71,14 @@ describe Spree::BaseMailer, type: :mailer do
end
end
+ describe 'default From header' do
+ let!(:store) { @default_store }
+
+ it 'is applied without the mailer passing from explicitly' do
+ expect(FromProbeMailer.sample.from).to eq([store.mail_from_address])
+ end
+ end
+
describe 'default Reply-To header' do
let!(:store) { @default_store }
diff --git a/spree/emails/app/mailers/spree/customer_mailer.rb b/spree/emails/app/mailers/spree/customer_mailer.rb
index 07aa4a8cd5..5273d3254a 100644
--- a/spree/emails/app/mailers/spree/customer_mailer.rb
+++ b/spree/emails/app/mailers/spree/customer_mailer.rb
@@ -12,7 +12,6 @@ module Spree
with_store_locale(store) do
mail(
to: user.email,
- from: from_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/newsletter_mailer.rb b/spree/emails/app/mailers/spree/newsletter_mailer.rb
index 4e6927b029..7d993e2759 100644
--- a/spree/emails/app/mailers/spree/newsletter_mailer.rb
+++ b/spree/emails/app/mailers/spree/newsletter_mailer.rb
@@ -9,7 +9,7 @@ module Spree
base_url = redirect_url.presence || store.storefront_url
@confirm_email_url = append_token(base_url, @subscriber.verification_token)
with_store_locale(store) do
- mail(to: @subscriber.email, from: from_address, subject: Spree.t('newsletter_mailer.email_confirmation.subject'))
+ mail(to: @subscriber.email, subject: Spree.t('newsletter_mailer.email_confirmation.subject'))
end
end
diff --git a/spree/emails/app/mailers/spree/order_mailer.rb b/spree/emails/app/mailers/spree/order_mailer.rb
index 788fc3b627..dd4f03aaae 100644
--- a/spree/emails/app/mailers/spree/order_mailer.rb
+++ b/spree/emails/app/mailers/spree/order_mailer.rb
@@ -7,7 +7,7 @@ module Spree
current_store = @order.store
with_store_locale(current_store, @order.locale) do
subject = 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)
+ mail(to: @order.email, subject: subject, store_url: current_store.storefront_url)
end
end
@@ -16,7 +16,7 @@ module Spree
current_store = @order.store
with_store_locale(current_store) do
subject = 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)
+ mail(to: current_store.new_order_notifications_email, subject: subject, store_url: current_store.storefront_url)
end
end
@@ -25,7 +25,7 @@ module Spree
current_store = @order.store
with_store_locale(current_store, @order.locale) do
subject = 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)
+ mail(to: @order.email, subject: subject, store_url: current_store.storefront_url)
end
end
@@ -35,7 +35,7 @@ module Spree
@checkout_payment_url = spree.checkout_state_url(@order.token, :payment, host: @current_store.storefront_url)
with_store_locale(@current_store, @order.locale) do
- mail(to: @order.email, from: from_address, subject: Spree.t('order_mailer.payment_link_email.subject', number: @order.number),
+ mail(to: @order.email, subject: Spree.t('order_mailer.payment_link_email.subject', number: @order.number),
store_url: @current_store.storefront_url)
end
end
diff --git a/spree/emails/app/mailers/spree/reimbursement_mailer.rb b/spree/emails/app/mailers/spree/reimbursement_mailer.rb
index ed94f4837c..2b2f751491 100644
--- a/spree/emails/app/mailers/spree/reimbursement_mailer.rb
+++ b/spree/emails/app/mailers/spree/reimbursement_mailer.rb
@@ -8,7 +8,7 @@ module Spree
current_store = @reimbursement.store || Spree::Store.default
with_store_locale(current_store, @order.locale) do
subject = 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)
+ mail(to: @order.email, subject: subject, store_url: current_store.storefront_url)
end
end
end
diff --git a/spree/emails/app/mailers/spree/shipment_mailer.rb b/spree/emails/app/mailers/spree/shipment_mailer.rb
index 3f9f196a49..afc509f28f 100644
--- a/spree/emails/app/mailers/spree/shipment_mailer.rb
+++ b/spree/emails/app/mailers/spree/shipment_mailer.rb
@@ -9,7 +9,7 @@ module Spree
current_store = @shipment.store
with_store_locale(current_store, @order.locale) do
subject = 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)
+ mail(to: @order.email, subject: subject, store_url: current_store.storefront_url)
end
end
end