diff --git a/app/finders/email_channel_finder.rb b/app/finders/email_channel_finder.rb index 3fb7edd53..41cd8e910 100644 --- a/app/finders/email_channel_finder.rb +++ b/app/finders/email_channel_finder.rb @@ -7,12 +7,18 @@ class EmailChannelFinder def perform channel = nil - recipient_mails = @email_object.to.to_a + @email_object.cc.to_a + recipient_mails.each do |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) + break if channel.present? end channel 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 diff --git a/spec/finders/email_channel_finder_spec.rb b/spec/finders/email_channel_finder_spec.rb index 09f86d19f..17e7c0115 100644 --- a/spec/finders/email_channel_finder_spec.rb +++ b/spec/finders/email_channel_finder_spec.rb @@ -31,6 +31,30 @@ describe ::EmailChannelFinder do channel = described_class.new(reply_mail.mail).perform expect(channel).to eq(channel_email) 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