Added guess routine for not parseable (not RfC conform) email headers like "To: Max Kohl | [example.com] <kohl@example.com>".

This commit is contained in:
Martin Edenhofer 2016-05-29 22:07:17 +02:00
parent 9e52358d57
commit 7a5f73c79a
4 changed files with 99 additions and 12 deletions

View file

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

13
test/fixtures/mail37.box vendored Normal file
View file

@ -0,0 +1,13 @@
To: Max Kohl | [example.com] <kohl@example.com>
Cc: Ingo Best <iw@example.com>
From: Example <info@example.com>
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!

View file

@ -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 <info@example.com>',
from_email: 'info@example.com',
from_display_name: 'Example',
subject: 'Example: Java 8 Neuerungen',
to: 'Max Kohl | [example.com] <kohl@example.com>',
cc: 'Ingo Best <iw@example.com>',
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|

View file

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