Improved email sender parsing with multiple senders in from.

This commit is contained in:
Martin Edenhofer 2017-06-19 22:24:22 +02:00
parent 8b72ce5933
commit a548009b86
4 changed files with 98 additions and 6 deletions

View file

@ -641,12 +641,22 @@ returns
data = {} data = {}
begin begin
data[:from_email] = Mail::Address.new(from).address list = Mail::AddressList.new(from)
data[:from_local] = Mail::Address.new(from).local list.addresses.each { |address|
data[:from_domain] = Mail::Address.new(from).domain data[:from_email] = address.address
data[:from_display_name] = Mail::Address.new(from).display_name || data[:from_local] = address.local
(Mail::Address.new(from).comments && Mail::Address.new(from).comments[0]) data[:from_domain] = address.domain
rescue data[:from_display_name] = address.display_name ||
(address.comments && address.comments[0])
break if data[:from_email].present? && data[:from_email] =~ /@/
}
rescue => e
if from =~ /<>/ && from =~ /<.+?>/
data = sender_properties(from.gsub(/<>/, ''))
end
end
if data.empty? || data[:from_email].blank?
from.strip! from.strip!
if from =~ /^(.+?)<(.+?)@(.+?)>$/ if from =~ /^(.+?)<(.+?)@(.+?)>$/
data[:from_email] = "#{$2}@#{$3}" data[:from_email] = "#{$2}@#{$3}"

24
test/fixtures/mail58.box vendored Normal file
View file

@ -0,0 +1,24 @@
From: "Yangzhou ABC Lighting Equipment " <bob@example.com>, "LTD" <ly@example.com>
Reply-To: zsm@example.com
To: "verkauf" <verkauf@example.de>
Subject: new design solar street lights
Message-ID: <201609141249219194555@example.com>
Date: Wed, 14 Sep 2016 12:49:21 +0800
X-Mailer: Foxmail 6, 10, 201, 20 [cn]
MIME-Version: 1.0
X-Priority: 3
X-CM-TRANSID:iOCowAC39vrU1thXd4NnAQ--.234S2
X-Coremail-Antispam: 1Uf129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73
VFW2AGmfu7bjvjm3AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjxUg3xhUUUUU
X-Originating-IP: [121.233.254.237]
X-CM-SenderInfo: to16vxpkrqw63pof0z/1tbiTBXk7ldp+Z4oIwAAsB
Content-Type: text/plain;
charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
=E4=F6=FC=DF ad asd
-Martin
--
Old programmers never die. They just branch to a new address.

24
test/fixtures/mail59.box vendored Normal file
View file

@ -0,0 +1,24 @@
From: "Yangzhou ABC Lighting Equipment " <>, "LTD" <ly@example.com>
Reply-To: zsm@example.com
To: "verkauf" <verkauf@example.de>
Subject: new design solar street lights
Message-ID: <201609141249219194555@example.com>
Date: Wed, 14 Sep 2016 12:49:21 +0800
X-Mailer: Foxmail 6, 10, 201, 20 [cn]
MIME-Version: 1.0
X-Priority: 3
X-CM-TRANSID:iOCowAC39vrU1thXd4NnAQ--.234S2
X-Coremail-Antispam: 1Uf129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73
VFW2AGmfu7bjvjm3AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjxUg3xhUUUUU
X-Originating-IP: [121.233.254.237]
X-CM-SenderInfo: to16vxpkrqw63pof0z/1tbiTBXk7ldp+Z4oIwAAsB
Content-Type: text/plain;
charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
=E4=F6=FC=DF ad asd
-Martin
--
Old programmers never die. They just branch to a new address.

View file

@ -1157,6 +1157,40 @@ Bob Smith
', ',
}, },
}, },
{
data: IO.binread('test/fixtures/mail58.box'),
body_md5: '548917e0bff0806f9b27c09bbf23bb38',
params: {
from: 'Yangzhou ABC Lighting Equipment <bob@example.com>, LTD <ly@example.com>',
from_email: 'bob@example.com',
from_display_name: 'Yangzhou ABC Lighting Equipment',
subject: 'new design solar street lights',
content_type: 'text/plain',
body: "äöüß ad asd
-Martin
--
Old programmers never die. They just branch to a new address."
},
},
{
data: IO.binread('test/fixtures/mail59.box'),
body_md5: '548917e0bff0806f9b27c09bbf23bb38',
params: {
from: '"Yangzhou ABC Lighting Equipment " <>, "LTD" <ly@example.com>',
from_email: 'ly@example.com',
from_display_name: 'LTD',
subject: 'new design solar street lights',
content_type: 'text/plain',
body: "äöüß ad asd
-Martin
--
Old programmers never die. They just branch to a new address."
},
},
] ]
count = 0 count = 0