mirror of
https://github.com/discourse/discourse.git
synced 2025-09-06 10:50:21 +08:00
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:
parent
b01a4c0ada
commit
fcd352e089
3 changed files with 26 additions and 0 deletions
|
@ -64,6 +64,7 @@ module Email
|
||||||
DistributedMutex.synchronize(@message_id) do
|
DistributedMutex.synchronize(@message_id) do
|
||||||
begin
|
begin
|
||||||
return if IncomingEmail.exists?(message_id: @message_id)
|
return if IncomingEmail.exists?(message_id: @message_id)
|
||||||
|
ensure_valid_address_lists
|
||||||
@from_email, @from_display_name = parse_from_field(@mail)
|
@from_email, @from_display_name = parse_from_field(@mail)
|
||||||
@incoming_email = create_incoming_email
|
@incoming_email = create_incoming_email
|
||||||
process_internal
|
process_internal
|
||||||
|
@ -77,6 +78,16 @@ module Email
|
||||||
end
|
end
|
||||||
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?
|
def is_blacklisted?
|
||||||
return false if SiteSetting.ignore_by_title.blank?
|
return false if SiteSetting.ignore_by_title.blank?
|
||||||
Regexp.new(SiteSetting.ignore_by_title, Regexp::IGNORECASE) =~ @mail.subject
|
Regexp.new(SiteSetting.ignore_by_title, Regexp::IGNORECASE) =~ @mail.subject
|
||||||
|
|
|
@ -886,4 +886,12 @@ describe Email::Receiver do
|
||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
|
|
7
spec/fixtures/emails/unparsable_email_addresses.eml
vendored
Normal file
7
spec/fixtures/emails/unparsable_email_addresses.eml
vendored
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue