From 185dcb2ca1216330cc502974dedbe345588cbabf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 9 Jan 2017 22:59:30 +0100 Subject: [PATCH] handle emails with localized headers :angry: --- lib/email/receiver.rb | 2 + spec/components/email/receiver_spec.rb | 51 +++++++++++++--------- spec/fixtures/emails/forwarded_email_3.eml | 18 ++++++++ 3 files changed, 50 insertions(+), 21 deletions(-) create mode 100644 spec/fixtures/emails/forwarded_email_3.eml diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb index 561757a5612..983dd954731 100644 --- a/lib/email/receiver.rb +++ b/lib/email/receiver.rb @@ -239,6 +239,8 @@ module Email end def parse_from_field(mail) + return unless mail[:from] + if mail[:from].errors.blank? mail[:from].address_list.addresses.each do |address_field| address_field.decoded diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb index 0a7b0a1bed0..47c7c4a1dac 100644 --- a/spec/components/email/receiver_spec.rb +++ b/spec/components/email/receiver_spec.rb @@ -383,37 +383,46 @@ 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) + context "with forwarded emails enabled" do + before { SiteSetting.enable_forwarded_emails = true } - forwarded_post, last_post = *Post.last(2) + it "handles forwarded emails" do + expect { process(:forwarded_email_1) }.to change(Topic, :count) - expect(forwarded_post.user.email).to eq("some@one.com") - expect(last_post.user.email).to eq("ba@bar.com") + forwarded_post, last_post = *Post.last(2) - expect(forwarded_post.raw).to match(/XoXo/) - expect(last_post.raw).to match(/can you have a look at this email below/) + expect(forwarded_post.user.email).to eq("some@one.com") + expect(last_post.user.email).to eq("ba@bar.com") - expect(last_post.post_type).to eq(Post.types[:regular]) - end + expect(forwarded_post.raw).to match(/XoXo/) + expect(last_post.raw).to match(/can you have a look at this email below/) - it "handles weirdly forwarded emails" do - group.add(Fabricate(:user, email: "ba@bar.com")) - group.save + expect(last_post.post_type).to eq(Post.types[:regular]) + end - SiteSetting.enable_forwarded_emails = true - expect { process(:forwarded_email_2) }.to change(Topic, :count) + it "handles weirdly forwarded emails" do + group.add(Fabricate(:user, email: "ba@bar.com")) + group.save - forwarded_post, last_post = *Post.last(2) + SiteSetting.enable_forwarded_emails = true + expect { process(:forwarded_email_2) }.to change(Topic, :count) - expect(forwarded_post.user.email).to eq("some@one.com") - expect(last_post.user.email).to eq("ba@bar.com") + forwarded_post, last_post = *Post.last(2) - expect(forwarded_post.raw).to match(/XoXo/) - expect(last_post.raw).to match(/can you have a look at this email below/) + 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/) + + expect(last_post.post_type).to eq(Post.types[:whisper]) + end + + # Who thought this was a good idea?! + it "doesn't blow up with localized email headers" do + expect { process(:forwarded_email_3) }.to change(Topic, :count) + end - expect(last_post.post_type).to eq(Post.types[:whisper]) end end diff --git a/spec/fixtures/emails/forwarded_email_3.eml b/spec/fixtures/emails/forwarded_email_3.eml new file mode 100644 index 00000000000..1f37822b0a7 --- /dev/null +++ b/spec/fixtures/emails/forwarded_email_3.eml @@ -0,0 +1,18 @@ +Message-ID: <60@foo.bar.mail> +From: Ba Bar +To: Team +Date: Mon, 9 Dec 2016 13:37:42 +0100 +Subject: Fwd: Ça Discourse ? + +@team, can you have a look at this email below? + +Objet: Ça Discourse ? +Date: 2017-01-04 11:27 +De: Un Français +À: ba@bar.com + +Bonjour, + +Ça Discourse bien aujourd'hui ? + +Bises