diff --git a/app/finders/email_channel_finder.rb b/app/finders/email_channel_finder.rb index 1c8eaeaf2..3fb7edd53 100644 --- a/app/finders/email_channel_finder.rb +++ b/app/finders/email_channel_finder.rb @@ -1,4 +1,6 @@ class EmailChannelFinder + include EmailHelper + def initialize(email_object) @email_object = email_object end @@ -7,7 +9,8 @@ class EmailChannelFinder channel = nil recipient_mails = @email_object.to.to_a + @email_object.cc.to_a recipient_mails.each do |email| - channel = Channel::Email.find_by('lower(email) = ? OR lower(forward_to_email) = ?', email.downcase, email.downcase) + 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 diff --git a/app/helpers/email_helper.rb b/app/helpers/email_helper.rb index 256a50387..46689ba00 100644 --- a/app/helpers/email_helper.rb +++ b/app/helpers/email_helper.rb @@ -3,4 +3,11 @@ module EmailHelper domain = email.split('@').last domain.split('.').first end + + # ref: https://www.rfc-editor.org/rfc/rfc5233.html + # This is not a mandatory requirement for email addresses, but it is a common practice. + # john+test@xyc.com is the same as john@xyc.com + def normalize_email_with_plus_addressing(email) + "#{email.split('@').first.split('+').first}@#{email.split('@').last}".downcase + end end diff --git a/spec/finders/email_channel_finder_spec.rb b/spec/finders/email_channel_finder_spec.rb index 75cae37a9..09f86d19f 100644 --- a/spec/finders/email_channel_finder_spec.rb +++ b/spec/finders/email_channel_finder_spec.rb @@ -24,6 +24,13 @@ describe ::EmailChannelFinder do channel = described_class.new(reply_mail.mail).perform expect(channel).to eq(channel_email) end + + it 'return channel with to+extension email' do + channel_email.update(email: 'test@example.com') + reply_mail.mail['to'] = 'test+123@example.com' + channel = described_class.new(reply_mail.mail).perform + expect(channel).to eq(channel_email) + end end end end diff --git a/spec/helpers/email_helper_spec.rb b/spec/helpers/email_helper_spec.rb new file mode 100644 index 000000000..1bc651e91 --- /dev/null +++ b/spec/helpers/email_helper_spec.rb @@ -0,0 +1,19 @@ +require 'rails_helper' + +describe EmailHelper, type: :helper do + describe '#normalize_email_with_plus_addressing' do + context 'when email is passed' do + it 'normalise if plus addressing is present' do + expect(helper.normalize_email_with_plus_addressing('john+test@acme.inc')).to eq 'john@acme.inc' + end + + it 'returns original if plus addressing is not present' do + expect(helper.normalize_email_with_plus_addressing('john@acme.inc')).to eq 'john@acme.inc' + end + + it 'returns downcased version of email' do + expect(helper.normalize_email_with_plus_addressing('JoHn+AAsdfss@acme.inc')).to eq 'john@acme.inc' + end + end + end +end