Fixes #3145 - EmailParser (process_unprocessable_mail) ignores group routings.

This commit is contained in:
Rolf Schmidt 2020-10-27 10:30:05 +01:00 committed by Thorsten Eckel
parent 16872c3c89
commit 607c221b39
2 changed files with 35 additions and 1 deletions

View file

@ -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

View file

@ -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