FIX: wasn't able to parse FROM email in the embedded email

This commit is contained in:
Régis Hanol 2016-12-01 18:34:47 +01:00
parent 8a0cf1b90e
commit 62763f025c
4 changed files with 77 additions and 4 deletions

View File

@ -244,14 +244,21 @@ module Email
address_field.decoded
from_address = address_field.address
from_display_name = address_field.display_name.try(:to_s)
return [from_address.downcase, from_display_name] if from_address["@"]
return [from_address&.downcase, from_display_name&.strip] if from_address["@"]
end
end
from_address = mail.from[/<([^>]+)>/, 1]
from_display_name = mail.from[/^([^<]+)/, 1]
if mail.from[/<[^>]+>/]
from_address = mail.from[/<([^>]+)>/, 1]
from_display_name = mail.from[/^([^<]+)/, 1]
end
[from_address.downcase, from_display_name]
if (from_address.blank? || !from_address["@"]) && mail.from[/\[mailto:[^\]]+\]/]
from_address = mail.from[/\[mailto:([^\]]+)\]/, 1]
from_display_name = mail.from[/^([^\[]+)/, 1]
end
[from_address&.downcase, from_display_name&.strip]
end
def subject
@ -376,6 +383,9 @@ module Email
def process_forwarded_email(destination, user)
embedded = Mail.new(@embedded_email_raw)
email, display_name = parse_from_field(embedded)
return false if email.blank? || !email["@"]
embedded_user = find_or_create_user(email, display_name)
raw = try_to_encode(embedded.decoded, "UTF-8").presence || embedded.to_s
title = embedded.subject.presence || subject
@ -387,6 +397,7 @@ module Email
raw: raw,
title: title,
archetype: Archetype.private_message,
target_usernames: [user.username],
target_group_names: [group.name],
is_group_message: true,
skip_validations: true,

View File

@ -383,6 +383,32 @@ describe Email::Receiver do
expect(Post.last.raw).to match(/discourse\.rb/)
end
it "handles forwarded emails" do
SiteSetting.enable_forwarded_emails = true
expect { process(:forwarded_email_1) }.to change(Topic, :count)
forwarded_post, last_post = *Post.last(2)
expect(forwarded_post.user.email).to eq("some@one.com")
expect(last_post.user.email).to eq("ba@bar.com")
expect(forwarded_post.raw).to match(/XoXo/)
expect(last_post.raw).to match(/can you have a look at this email below/)
end
it "handles weirdly forwarded emails" do
SiteSetting.enable_forwarded_emails = true
expect { process(:forwarded_email_2) }.to change(Topic, :count)
forwarded_post, last_post = *Post.last(2)
expect(forwarded_post.user.email).to eq("some@one.com")
expect(last_post.user.email).to eq("ba@bar.com")
expect(forwarded_post.raw).to match(/XoXo/)
expect(last_post.raw).to match(/can you have a look at this email below/)
end
end
context "new topic in a category" do

View File

@ -0,0 +1,18 @@
Message-ID: <58@foo.bar.mail>
From: Ba Bar <ba@bar.com>
To: Team <team@bar.com>
Date: Mon, 1 Dec 2016 13:37:42 +0100
Subject: FW: Discoursing much?
@team, can you have a look at this email below?
From: Some One <some@one.com>
To: Ba Bar <ba@bar.com>
Date: Mon, 1 Dec 2016 00:13:37 +0100
Subject: Discoursing much?
Hello Ba Bar,
Discoursing much today?
XoXo

View File

@ -0,0 +1,18 @@
Message-ID: <59@foo.bar.mail>
From: Ba Bar <ba@bar.com>
To: Team <team@bar.com>
Date: Mon, 1 Dec 2016 13:37:42 +0100
Subject: Re: Discoursing much?
@team, can you have a look at this email below?
From: Some One [mailto:some@one.com]
To: Ba Bar <ba@bar.com>
Date: Mon, 1 Dec 2016 00:13:37 +0100
Subject: Discoursing much?
Hello Ba Bar,
Discoursing much today?
XoXo