From 7a5f73c79adb21b925a8ba960ecd49db24c3f534 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sun, 29 May 2016 22:07:17 +0200 Subject: [PATCH] Added guess routine for not parseable (not RfC conform) email headers like "To: Max Kohl | [example.com] ". --- app/models/channel/filter/identify_sender.rb | 43 ++++++++++++++++---- test/fixtures/mail37.box | 13 ++++++ test/unit/email_parser_test.rb | 19 +++++++-- test/unit/email_process_test.rb | 36 ++++++++++++++++ 4 files changed, 99 insertions(+), 12 deletions(-) create mode 100644 test/fixtures/mail37.box diff --git a/app/models/channel/filter/identify_sender.rb b/app/models/channel/filter/identify_sender.rb index 62cb8af6f..0ebfe310a 100644 --- a/app/models/channel/filter/identify_sender.rb +++ b/app/models/channel/filter/identify_sender.rb @@ -44,15 +44,40 @@ module Channel::Filter::IdentifySender def self.create_recipients(mail) ['raw-to', 'raw-cc'].each { |item| next if !mail[item.to_sym] - next if !mail[item.to_sym].addrs - items = mail[item.to_sym].addrs - items.each {|address_data| - user_create( - firstname: address_data.display_name, - lastname: '', - email: address_data.address, - ) - } + begin + next if !mail[item.to_sym].addrs + items = mail[item.to_sym].addrs + items.each {|address_data| + user_create( + firstname: address_data.display_name, + lastname: '', + email: address_data.address, + ) + } + rescue => e + # parse not parseable fields by mail gem like + # - Max Kohl | [example.com] + Rails.logger.error 'ERROR: ' + e.inspect + Rails.logger.error 'ERROR: try it by my self' + recipients = mail[item.to_sym].to_s.split(',') + recipients.each {|recipient| + address = nil + display_name = nil + if recipient =~ /<(.+?)>/ + address = $1 + end + if recipient =~ /^(.+?)<(.+?)>/ + display_name = ($1).strip + end + next if address.empty? + user_create( + firstname: display_name, + lastname: '', + email: address, + ) + } + end + } end diff --git a/test/fixtures/mail37.box b/test/fixtures/mail37.box new file mode 100644 index 000000000..001fb16ac --- /dev/null +++ b/test/fixtures/mail37.box @@ -0,0 +1,13 @@ +To: Max Kohl | [example.com] +Cc: Ingo Best +From: Example +Subject: Example: Java 8 Neuerungen +Message-ID: <566694F5.2070300@example.com> +Date: Tue, 8 Dec 2015 09:29:41 +0100 +User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 + Thunderbird/38.3.0 +MIME-Version: 1.0 +Content-Type: text/plain; charset=utf-8; format=flowed +Content-Transfer-Encoding: 8bit + +Tag Max / Ingo! diff --git a/test/unit/email_parser_test.rb b/test/unit/email_parser_test.rb index e0894da86..fad4a4c43 100644 --- a/test/unit/email_parser_test.rb +++ b/test/unit/email_parser_test.rb @@ -720,6 +720,19 @@ Um noch vertrauter zu werden, kannst Du mit einen externen E-Mail Account (z. B. [1] http://web.de', }, }, + { + data: IO.binread('test/fixtures/mail37.box'), + body_md5: 'dd67e5037a740c053c2bf91f67be072f', + params: { + from: 'Example ', + from_email: 'info@example.com', + from_display_name: 'Example', + subject: 'Example: Java 8 Neuerungen', + to: 'Max Kohl | [example.com] ', + cc: 'Ingo Best ', + body: "Tag Max / Ingo!\n", + }, + }, ] count = 0 @@ -727,13 +740,13 @@ Um noch vertrauter zu werden, kannst Du mit einen externen E-Mail Account (z. B. count += 1 #p "Count: #{count}" parser = Channel::EmailParser.new - data = parser.parse( file[:data] ) + data = parser.parse(file[:data]) #puts '++' + data[:body].to_s + '++' # check body - md5 = Digest::MD5.hexdigest( data[:body] ) + md5 = Digest::MD5.hexdigest(data[:body]) #puts "IS #{md5} / should #{file[:body_md5]}" - assert_equal( file[:body_md5], md5 ) + assert_equal(file[:body_md5], md5) # check params file[:params].each { |key, value| diff --git a/test/unit/email_process_test.rb b/test/unit/email_process_test.rb index d9a9e3777..6649cf02f 100644 --- a/test/unit/email_process_test.rb +++ b/test/unit/email_process_test.rb @@ -1985,6 +1985,42 @@ Some Text', ], } }, + { + data: IO.binread('test/fixtures/mail37.box'), + success: true, + result: { + 0 => { + priority: '2 normal', + title: 'Example: Java 8 Neuerungen', + }, + 1 => { + sender: 'Customer', + type: 'email', + }, + }, + verify: { + users: [ + { + firstname: 'Example', + lastname: '', + fullname: 'Example', + email: 'info@example.com', + }, + { + firstname: 'Ingo', + lastname: 'Best', + fullname: 'Ingo Best', + email: 'iw@example.com', + }, + { + firstname: 'Max', + lastname: 'Kohl | [example.com]', + fullname: 'Max Kohl | [example.com]', + email: 'kohl@example.com', + }, + ], + } + }, ] process(files) end