diff --git a/app/models/channel/email_parser.rb b/app/models/channel/email_parser.rb index 11908e981..803c3ce31 100644 --- a/app/models/channel/email_parser.rb +++ b/app/models/channel/email_parser.rb @@ -226,6 +226,11 @@ returns group = nil if channel[:group_id] group = Group.lookup(id: channel[:group_id]) + else + mail_to_group = self.class.mail_to_group(mail[:to]) + if mail_to_group.present? + group = mail_to_group + end end if group.blank? || group.active == false group = Group.where(active: true).order(id: :asc).first @@ -322,6 +327,20 @@ returns [ticket, article, session_user, mail] end + def self.mail_to_group(to) + begin + to = Mail::AddressList.new(to)&.addresses&.first&.address + rescue + Rails.logger.error 'Can not parse :to field for group destination!' + end + return if to.blank? + + email = EmailAddress.find_by(email: to) + return if email&.channel.blank? + + email.channel&.group + end + def self.check_attributes_by_x_headers(header_name, value) class_name = nil attribute = nil diff --git a/spec/models/channel/email_parser_spec.rb b/spec/models/channel/email_parser_spec.rb index c2744b6fa..2eef079db 100644 --- a/spec/models/channel/email_parser_spec.rb +++ b/spec/models/channel/email_parser_spec.rb @@ -124,6 +124,21 @@ RSpec.describe Channel::EmailParser, type: :model do expect(Ticket.last.state.name).to eq('new') end + context 'when no channel is given but a group with the :to address exists' do + let!(:email_address) { create(:email_address, email: 'baz@qux.net', channel: nil) } + let!(:group) { create(:group, name: 'baz headquarter', email_address: email_address) } + let!(:channel) do + channel = create(:email_channel, group: group) + email_address.update(channel: channel) + channel + end + + it 'sets the group based on the :to field' do + described_class.new.process({}, raw_mail) + expect(Ticket.last.group.id).to eq(group.id) + end + end + context 'when from address matches an existing agent' do let!(:agent) { create(:agent, email: 'foo@bar.com') } @@ -147,7 +162,7 @@ RSpec.describe Channel::EmailParser, type: :model do From: foo@bar.com To: myzammad@example.com Subject: [#{Setting.get('ticket_hook') + Setting.get('ticket_hook_divider') + ticket.number}] test - + Lorem ipsum dolor RAW