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.

Fix store branding in invitation and newsletter emails

Damian Legawiec8413e6756e59
diff --git a/spree/core/app/mailers/spree/invitation_mailer.rb b/spree/core/app/mailers/spree/invitation_mailer.rb
index 35862f944a..50e5ff6dff 100644
--- a/spree/core/app/mailers/spree/invitation_mailer.rb
+++ b/spree/core/app/mailers/spree/invitation_mailer.rb
@@ -3,6 +3,10 @@ module Spree
# invitation email, sending email to the invited to let them know they have been invited to join a store/account/vendor
def invitation_email(invitation)
@invitation = invitation
+ # The shared header/footer and from/reply-to addresses read
+ # current_store — without this, background delivery falls back to the
+ # default store's branding on multi-store installs.
+ @current_store = invitation.store
with_store_locale(invitation.store) do
mail(to: invitation.email,
from: from_address,
@@ -15,6 +19,7 @@ module Spree
# sending email to the inviter to let them know the invitee has accepted the invitation
def invitation_accepted(invitation)
@invitation = invitation
+ @current_store = invitation.store
with_store_locale(invitation.store) do
mail(to: invitation.inviter.email,
from: from_address,
diff --git a/spree/core/spec/mailers/spree/invitation_mailer_spec.rb b/spree/core/spec/mailers/spree/invitation_mailer_spec.rb
index 3c4acb2d81..de0846f0cb 100644
--- a/spree/core/spec/mailers/spree/invitation_mailer_spec.rb
+++ b/spree/core/spec/mailers/spree/invitation_mailer_spec.rb
@@ -11,6 +11,24 @@ RSpec.describe Spree::InvitationMailer, type: :mailer do
describe '#invitation_email' do
subject(:mail) { described_class.invitation_email(invitation) }
+ context 'for an invitation on a non-default store' do
+ let(:other_store) do
+ create(:store, name: 'Second Store', mail_from_address: '[email protected]',
+ url: 'second.example.com')
+ end
+ let(:invitation) do
+ create(:invitation, email: '[email protected]', inviter: inviter, resource: other_store, skip_email: true)
+ end
+
+ # Regression: the shared header/footer and from address read
+ # current_store, which used to fall back to the default store when
+ # delivering from a background job.
+ it 'brands the email with the invitation store, not the default store' do
+ expect(mail.from).to eq(['[email protected]'])
+ expect(mail.body.encoded).to include('Second Store')
+ end
+ end
+
it 'renders the subject' do
expect(mail.subject).to eq(
Spree.t('invitation_mailer.invitation_email.subject', resource_name: store.name)
diff --git a/spree/emails/app/mailers/spree/newsletter_mailer.rb b/spree/emails/app/mailers/spree/newsletter_mailer.rb
index ccbe87e1f0..4e6927b029 100644
--- a/spree/emails/app/mailers/spree/newsletter_mailer.rb
+++ b/spree/emails/app/mailers/spree/newsletter_mailer.rb
@@ -3,6 +3,9 @@ module Spree
def email_confirmation(subscriber, redirect_url: nil)
@subscriber = subscriber
store = subscriber.store || Spree::Current.store || Spree::Store.default
+ # The shared header/footer and from address read current_store — set it
+ # to the subscriber's store so multi-store installs brand correctly.
+ @current_store = store
base_url = redirect_url.presence || store.storefront_url
@confirm_email_url = append_token(base_url, @subscriber.verification_token)
with_store_locale(store) do
diff --git a/spree/emails/spec/mailers/spree/newsletter_mailer_spec.rb b/spree/emails/spec/mailers/spree/newsletter_mailer_spec.rb
index e6fcdf1a88..b40ed3205f 100644
--- a/spree/emails/spec/mailers/spree/newsletter_mailer_spec.rb
+++ b/spree/emails/spec/mailers/spree/newsletter_mailer_spec.rb
@@ -5,6 +5,24 @@ describe Spree::NewsletterMailer, type: :mailer do
let(:subscriber) { create(:newsletter_subscriber, :unverified, email: '[email protected]', store: store) }
describe '#email_confirmation' do
+ context 'for a subscriber of a non-default store' do
+ let(:other_store) do
+ create(:store, name: 'Second Store', mail_from_address: '[email protected]',
+ url: 'second.example.com')
+ end
+ let(:subscriber) { create(:newsletter_subscriber, :unverified, email: '[email protected]', store: other_store) }
+
+ # Regression: the shared header/footer and from address read
+ # current_store, which used to fall back to the default store when
+ # delivering from a background job.
+ it 'brands the email with the subscriber store, not the default store' do
+ message = described_class.email_confirmation(subscriber)
+
+ expect(message.from).to eq(['[email protected]'])
+ expect(message.body.encoded).to include('Second Store')
+ end
+ end
+
context 'when a redirect_url is provided' do
it 'builds the confirmation link from the redirect URL with the verification token appended' do
message = described_class.email_confirmation(subscriber, redirect_url: 'https://storefront.example.com/newsletter/confirm')