From 22c5b24fb770326cd629a71c8f0c85cb76f5809a Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 15 Jan 2018 16:22:52 +0100 Subject: [PATCH] Fixed issue #1749 - Unable to send auto reply if from contains 2 or more senders with invalid email address. --- app/models/ticket.rb | 17 ++++- test/fixtures/mail65.box | 17 +++++ test/unit/email_process_test.rb | 32 +++++++++ test/unit/ticket_trigger_test.rb | 114 +++++++++++++++++++++++++++++++ 4 files changed, 177 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/mail65.box diff --git a/app/models/ticket.rb b/app/models/ticket.rb index d04ccd93c..fbc483607 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -884,14 +884,25 @@ perform changes on ticket next if skip_user # send notifications only to email adresses - next if !recipient_email + next if recipient_email.blank? next if recipient_email !~ /@/ # check if address is valid begin - recipient_email = Mail::Address.new(recipient_email).address + Mail::AddressList.new(recipient_email).addresses.each do |address| + recipient_email = address.address + break if recipient_email.present? && recipient_email =~ /@/ && !recipient_email.match?(/\s/) + end rescue - next # because unable to parse + if recipient_email.present? + if recipient_email !~ /^(.+?)<(.+?)@(.+?)>$/ + next # no usable format found + end + recipient_email = "#{$2}@#{$3}" + end + next if recipient_email.blank? + next if recipient_email !~ /@/ + next if recipient_email.match?(/\s/) end # do not sent notifications to this recipients diff --git a/test/fixtures/mail65.box b/test/fixtures/mail65.box new file mode 100644 index 000000000..96ee62707 --- /dev/null +++ b/test/fixtures/mail65.box @@ -0,0 +1,17 @@ +From: =?iso-8859-1?Q?B=FCrling,=20Andreas?= +Content-Type: text/plain; + charset=iso-8859-1 +Content-Transfer-Encoding: quoted-printable +Subject: =?iso-8859-1?Q?aa=E4=F6=FC=DFad_asd?= +X-Universally-Unique-Identifier: d12c15d2-e6d6-4ccd-86c7-abc2c3d0a2a2 +Date: Fri, 4 May 2012 14:01:03 +0200 +Message-Id: +To: metest@znuny.com +Mime-Version: 1.0 (Apple Message framework v1257) + +=E4=F6=FC=DF ad asd + +-Martin + +-- +Old programmers never die. They just branch to a new address. diff --git a/test/unit/email_process_test.rb b/test/unit/email_process_test.rb index afe0031f9..f289d618c 100644 --- a/test/unit/email_process_test.rb +++ b/test/unit/email_process_test.rb @@ -2699,6 +2699,38 @@ Some Text', ], }, }, + { + data: IO.binread('test/fixtures/mail65.box'), + success: true, + result: { + 0 => { + priority: '2 normal', + title: 'aaäöüßad asd', + }, + 1 => { + from: '=?iso-8859-1?Q?B=FCrling, =20Andreas?= ', + sender: 'Customer', + type: 'email', + body: 'äöüß ad asd + +-Martin + +-- +Old programmers never die. They just branch to a new address. +', + }, + }, + verify: { + users: [ + { + firstname: '=20Andreas?=', + lastname: '', + fullname: '=20Andreas?=', + email: 'smith@example.com', + }, + ], + }, + }, ] assert_process(files) end diff --git a/test/unit/ticket_trigger_test.rb b/test/unit/ticket_trigger_test.rb index c649c7c58..23eb4ebb8 100644 --- a/test/unit/ticket_trigger_test.rb +++ b/test/unit/ticket_trigger_test.rb @@ -3905,4 +3905,118 @@ class TicketTriggerTest < ActiveSupport::TestCase end + test 'trigger auto reply with umlaut in form' do + trigger1 = Trigger.create_or_update( + name: 'auto reply', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + }, + }, + perform: { + 'notification.email' => { + 'body' => 'some text
#{ticket.customer.lastname}
#{ticket.title}
#{article.body}', + 'recipient' => 'article_last_sender', + 'subject' => 'Thanks for your inquiry (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: 'test 1', + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'Sabine Schütz ', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: 'some message note hello ', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + ticket1.reload + assert_equal('test 1', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + assert_equal('Sabine Schütz ', ticket1.articles.first.from, 'ticket1.articles.first.from verify') + assert_equal([], ticket1.tag_list) + + Observer::Transaction.commit + + ticket1.reload + assert_equal('test 1', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + article1 = ticket1.articles.last + assert_match('Zammad ', article1.from) + assert_match('some_sender@example.com', article1.to) + assert_match('Thanks for your inquiry (test 1)!', article1.subject) + assert_match('some message', article1.body) + assert_match('> some message <b>note</b> hello', article1.body) + assert_equal('text/html', article1.content_type) + + end + + test 'trigger auto reply with 2 sender addresses in form' do + trigger1 = Trigger.create_or_update( + name: 'auto reply', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + }, + }, + perform: { + 'notification.email' => { + 'body' => 'some text
#{ticket.customer.lastname}
#{ticket.title}
#{article.body}', + 'recipient' => 'article_last_sender', + 'subject' => 'Thanks for your inquiry (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + ticket1, article1, user, mail = Channel::EmailParser.new.process({}, IO.binread('test/fixtures/mail65.box')) + + assert_equal('aaäöüßad asd', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + article1 = ticket1.articles.last + assert_match('Zammad ', article1.from) + assert_match('smith@example.com', article1.to) + assert_match('Thanks for your inquiry (aaäöüßad asd)!', article1.subject) + assert_match('some text

aaäöüßad asd', article1.body) + assert_equal('text/html', article1.content_type) + + end + end