feat: Route emails based on x-original-to in email channel (#6901)

Fixes: #6608
ref: https://linear.app/chatwoot/issue/CW-30/emails-not-routed-based-on-x-original-to
This commit is contained in:
Tejaswini Chile
2023-04-13 14:52:12 +05:30
committed by GitHub
parent 44837aa657
commit 9ca21df9fd
2 changed files with 31 additions and 1 deletions

View File

@@ -7,12 +7,18 @@ class EmailChannelFinder
def perform def perform
channel = nil channel = nil
recipient_mails = @email_object.to.to_a + @email_object.cc.to_a
recipient_mails.each do |email| recipient_mails.each do |email|
normalized_email = normalize_email_with_plus_addressing(email) normalized_email = normalize_email_with_plus_addressing(email)
channel = Channel::Email.find_by('lower(email) = ? OR lower(forward_to_email) = ?', normalized_email, normalized_email) channel = Channel::Email.find_by('lower(email) = ? OR lower(forward_to_email) = ?', normalized_email, normalized_email)
break if channel.present? break if channel.present?
end end
channel channel
end end
def recipient_mails
recipient_addresses = @email_object.to.to_a + @email_object.cc.to_a + @email_object.bcc.to_a + [@email_object['X-Original-To'].try(:value)]
recipient_addresses.flatten.compact
end
end end

View File

@@ -31,6 +31,30 @@ describe ::EmailChannelFinder do
channel = described_class.new(reply_mail.mail).perform channel = described_class.new(reply_mail.mail).perform
expect(channel).to eq(channel_email) expect(channel).to eq(channel_email)
end end
it 'return channel with cc email' do
channel_email.update(email: 'test@example.com')
reply_mail.mail['to'] = nil
reply_mail.mail['cc'] = 'test@example.com'
channel = described_class.new(reply_mail.mail).perform
expect(channel).to eq(channel_email)
end
it 'return channel with bcc email' do
channel_email.update(email: 'test@example.com')
reply_mail.mail['to'] = nil
reply_mail.mail['bcc'] = 'test@example.com'
channel = described_class.new(reply_mail.mail).perform
expect(channel).to eq(channel_email)
end
it 'return channel with X-Original-To email' do
channel_email.update(email: 'test@example.com')
reply_mail.mail['to'] = nil
reply_mail.mail['X-Original-To'] = 'test@example.com'
channel = described_class.new(reply_mail.mail).perform
expect(channel).to eq(channel_email)
end
end end
end end
end end