FIX: Try fixing unparsable email addresses

The mail gem returns `UnstructuredField` when it fails to parse email addresses, but the `Receiver` always expects an `AddressList`.
This commit is contained in:
Gerhard Schlager 2018-03-27 18:28:37 +02:00
parent b01a4c0ada
commit fcd352e089
3 changed files with 26 additions and 0 deletions

View File

@ -64,6 +64,7 @@ module Email
DistributedMutex.synchronize(@message_id) do
begin
return if IncomingEmail.exists?(message_id: @message_id)
ensure_valid_address_lists
@from_email, @from_display_name = parse_from_field(@mail)
@incoming_email = create_incoming_email
process_internal
@ -77,6 +78,16 @@ module Email
end
end
def ensure_valid_address_lists
[:to, :cc, :bcc].each do |field|
addresses = @mail[field]
if addresses&.errors.present?
@mail[field] = addresses.to_s.scan(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/i)
end
end
end
def is_blacklisted?
return false if SiteSetting.ignore_by_title.blank?
Regexp.new(SiteSetting.ignore_by_title, Regexp::IGNORECASE) =~ @mail.subject

View File

@ -886,4 +886,12 @@ describe Email::Receiver do
end
end
end
it "tries to fix unparsable email addresses in To, CC and BBC headers" do
expect { process(:unparsable_email_addresses) }.to raise_error(Email::Receiver::BadDestinationAddress)
email = IncomingEmail.last
expect(email.to_addresses).to eq("foo@bar.com")
expect(email.cc_addresses).to eq("bob@example.com;carol@example.com")
end
end

View File

@ -0,0 +1,7 @@
Date: 27 Mar 2018 11:51:04 +0200
From: alice@example.com
To: foo@bar.com.
CC: bob@example.com., carol@example.com.
Subject: Email addresses ending with dot
Lorem ipsum