From 3e0769795422687b9cab32ac71d58e79d03f46c4 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 10 Jan 2017 01:09:17 +0100 Subject: [PATCH] Improved email parsing. --- app/models/channel/email_parser.rb | 15 ++++++++++++--- test/fixtures/mail45.box | 18 ++++++++++++++++++ test/unit/email_parser_test.rb | 16 ++++++++++++++-- test/unit/email_process_test.rb | 28 +++++++++++++++++++++++++++- 4 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/mail45.box diff --git a/app/models/channel/email_parser.rb b/app/models/channel/email_parser.rb index 699f04ad1..568abd637 100644 --- a/app/models/channel/email_parser.rb +++ b/app/models/channel/email_parser.rb @@ -108,9 +108,18 @@ class Channel::EmailParser data[:from_display_name] = Mail::Address.new(from).display_name || (Mail::Address.new(from).comments && Mail::Address.new(from).comments[0]) rescue - data[:from_email] = from - data[:from_local] = from - data[:from_domain] = from + from.strip! + if from =~ /^(.+?)<(.+?)@(.+?)>$/ + data[:from_email] = "#{$2}@#{$3}" + data[:from_local] = $2 + data[:from_domain] = $3 + data[:from_display_name] = $1.strip + data[:from_display_name].delete!('"') + else + data[:from_email] = from + data[:from_local] = from + data[:from_domain] = from + end end # do extra decoding because we needed to use field.value diff --git a/test/fixtures/mail45.box b/test/fixtures/mail45.box new file mode 100644 index 000000000..57ba8c7c9 --- /dev/null +++ b/test/fixtures/mail45.box @@ -0,0 +1,18 @@ +To: James-Max ROGER , Support +Subject: Nouveau message contact élégibilité Zammad +X-PHP-Originating-Script: 111:contact.php +From: Ups Rémi +Message-Id: <20170111111111.8CEF15FB28@01.localdomain> +Date: Fri, 6 Jan 2017 19:23:41 +0100 (CET) + +Nouveau message : +============================= +Nom: Orberer +Prénom: Rémi +Téléphone: xxxx +Email: xxx@example.com +Societé: KG +Adresse: RdL +CP: 11111 +Ville: Olèchena + diff --git a/test/unit/email_parser_test.rb b/test/unit/email_parser_test.rb index 97d868018..65fb78afe 100644 --- a/test/unit/email_parser_test.rb +++ b/test/unit/email_parser_test.rb @@ -680,13 +680,25 @@ end body_md5: '2f0f5a21e4393c174d4670a188fc5548', params: { from: '"Clement.Si" ', - from_email: '"Clement.Si" ', - from_display_name: '', + from_email: 'Claudia.Shu@yahoo.com.', + from_display_name: 'Clement.Si', subject: '精益生产闪婚,是谁的责任', to: 'abuse@domain.com', cc: nil, }, }, + { + data: IO.binread('test/fixtures/mail45.box'), + body_md5: '1d847e3626145a9e886914ecf0d89368', + params: { + from: '"Ups Rémi" ', + from_email: 'r.ordonaud@example.com', + from_display_name: 'Ups Rémi', + subject: 'Nouveau message contact élégibilité Zammad', + to: 'James-Max ROGER , Support ', + cc: nil, + }, + }, ] count = 0 diff --git a/test/unit/email_process_test.rb b/test/unit/email_process_test.rb index 489001af4..42a1780de 100644 --- a/test/unit/email_process_test.rb +++ b/test/unit/email_process_test.rb @@ -2015,7 +2015,33 @@ Some Text', }, { data: IO.binread('test/fixtures/mail44.box'), - success: false, + success: true, + result: { + 0 => { + priority: '2 normal', + title: '精益生产闪婚,是谁的责任', + }, + 1 => { + sender: 'Customer', + type: 'email', + }, + }, + verify: { + users: [ + { + firstname: 'Clement.Si', + lastname: '', + fullname: 'Clement.Si', + email: 'claudia.shu@yahoo.com.', + }, + { + firstname: '', + lastname: '', + fullname: 'abuse@domain.com', + email: 'abuse@domain.com', + }, + ], + } }, ] assert_process(files)