diff --git a/app/mailers/conversation_reply_mailer.rb b/app/mailers/conversation_reply_mailer.rb index f9572ba374e41..127231b9ab4d4 100644 --- a/app/mailers/conversation_reply_mailer.rb +++ b/app/mailers/conversation_reply_mailer.rb @@ -67,6 +67,10 @@ def should_use_conversation_email_address? @inbox.inbox_type == 'Email' || inbound_email_enabled? end + def should_use_inbox_from? + ENV['MAILER_SENDER_INBOX_PREFERENCE'] == 'true' + end + def conversation_already_viewed? # whether contact already saw the message on widget return unless @conversation.contact_last_seen_at @@ -101,8 +105,7 @@ def business_name end def from_email - sender_inbox_preference = ENV.fetch('MAILER_SENDER_INBOX_PREFERENCE', 'false') == 'true' - return parse_email(@account.support_email) if should_use_conversation_email_address? && !sender_inbox_preference + return parse_email(@account.support_email) if should_use_conversation_email_address? && !should_use_inbox_from? parse_email(inbox_from_email_address) end diff --git a/app/mailers/conversation_reply_mailer_helper.rb b/app/mailers/conversation_reply_mailer_helper.rb index 0499fb75dce6c..3a41e99df5778 100644 --- a/app/mailers/conversation_reply_mailer_helper.rb +++ b/app/mailers/conversation_reply_mailer_helper.rb @@ -81,12 +81,15 @@ def email_reply_to # Use channel email domain or account email domain depending on preference configuration for custom message_id and in_reply_to def channel_email_domain - use_inbox_email = ENV['MAILER_INBOUND_INBOX_PREFERENCE'] == 'true' inbox_email_domain = @inbox.channel.try(:email)&.split('@')&.last - return inbox_email_domain if inbox_email_domain.present? && use_inbox_email + return inbox_email_domain if inbox_email_domain.present? && should_use_channel_domain? return @account.inbound_email_domain if @account.inbound_email_domain.present? return inbox_email_domain if inbox_email_domain.present? raise(StandardError, 'Account inbound email domain and channel email domain not present.') end + + def should_use_channel_domain? + ENV['MAILER_INBOUND_INBOX_PREFERENCE'] == 'true' + end end diff --git a/spec/mailers/conversation_reply_mailer_spec.rb b/spec/mailers/conversation_reply_mailer_spec.rb index e4bfac4139782..21eb32c5ccfa7 100644 --- a/spec/mailers/conversation_reply_mailer_spec.rb +++ b/spec/mailers/conversation_reply_mailer_spec.rb @@ -383,5 +383,37 @@ expect(mail.in_reply_to).to eq("account/#{conversation.account.id}/conversation/#{conversation.uuid}@#{domain}") end end + + context 'when prefer inbox emails configured' do + let(:new_account) { create(:account, domain: 'example.com') } + let!(:email_channel) { create(:channel_email, account: new_account, email: 'testing@channel.com') } + let!(:inbox) { create(:inbox, channel: email_channel, account: new_account, email_address: 'testing@inbox.com') } + let(:inbox_member) { create(:inbox_member, user: agent, inbox: inbox) } + let(:conversation) { create(:conversation, assignee: agent, inbox: inbox_member.inbox, account: new_account) } + let!(:message) { create(:message, conversation: conversation, account: new_account) } + let(:mail) { described_class.reply_with_summary(message.conversation, message.id).deliver_now } + let(:domain) { inbox.channel.email.split('@').last } + + before do + allow(class_instance).to receive(:should_use_channel_domain?).and_return(true) + allow(class_instance).to receive(:should_use_inbox_from?).and_return(true) + end + + it 'sets the correct custom message id' do + expect(mail.message_id).to eq("conversation/#{conversation.uuid}/messages/#{message.id}@#{domain}") + end + + it 'sets the correct in reply to id' do + expect(mail.in_reply_to).to eq("account/#{conversation.account.id}/conversation/#{conversation.uuid}@#{domain}") + end + + it 'sets reply to email to be based on the inbox domain' do + expect(mail.reply_to).to eq(["reply+#{message.conversation.uuid}@#{domain}"]) + end + + it 'sets from email to the inbox email address' do + expect(mail.from).to eq([inbox.email_address]) + end + end end end