diff --git a/app/presenters/mail_presenter.rb b/app/presenters/mail_presenter.rb index 62cb0ed9a..cfd503976 100644 --- a/app/presenters/mail_presenter.rb +++ b/app/presenters/mail_presenter.rb @@ -176,7 +176,9 @@ class MailPresenter < SimpleDelegator def notification_email_from_chatwoot? # notification emails are send via mailer sender email address. so it should match configured_sender = Mail::Address.new(ENV.fetch('MAILER_SENDER_EMAIL', 'Chatwoot ')).address - original_sender.to_s.casecmp?(configured_sender) + actual_sender = parse_mail_address(@mail[:from]&.value)&.address + + actual_sender.to_s.casecmp?(configured_sender) end private diff --git a/spec/presenters/mail_presenter_spec.rb b/spec/presenters/mail_presenter_spec.rb index f5ed27537..2643561c3 100644 --- a/spec/presenters/mail_presenter_spec.rb +++ b/spec/presenters/mail_presenter_spec.rb @@ -257,6 +257,23 @@ RSpec.describe MailPresenter do expect(presenter.notification_email_from_chatwoot?).to be(true) end end + + it 'detects Chatwoot notification emails even when reply_to points to the conversation reply address' do + notification_mail = Mail.new do + from 'Chatwoot ' + reply_to 'reply+5f85d369-8486-41c0-87ce-5a914a00b721@reply.chatwoot.com' + to 'Customer ' + header['Subject'] = '[#81209] New messages on this conversation' + body 'Thank you for your understanding.' + end + + with_modified_env MAILER_SENDER_EMAIL: 'Chatwoot ' do + presenter = described_class.new(notification_mail) + + expect(presenter.original_sender).to eq('reply+5f85d369-8486-41c0-87ce-5a914a00b721@reply.chatwoot.com') + expect(presenter.notification_email_from_chatwoot?).to be(true) + end + end end end end