From fd504e741fc8fa7147be0710898d8f5f88d04625 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Fri, 28 Mar 2014 09:57:12 -0400 Subject: [PATCH] FIX: reply by email can handle emails with attachments. Attachments are still ignored, but a post or topic can be created from the email now. --- lib/email/receiver.rb | 15 +++++++-------- spec/components/email/receiver_spec.rb | 22 ++++++++++++---------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb index 7ce6102ca..30eecb076 100644 --- a/lib/email/receiver.rb +++ b/lib/email/receiver.rb @@ -37,7 +37,7 @@ module Email def process raise EmptyEmailError if @raw.blank? - @message = Mail::Message.new(@raw) + @message = Mail.new(@raw) # First remove the known discourse stuff. @@ -92,13 +92,11 @@ module Email # If the message is multipart, find the best type for our purposes if @message.multipart? - @message.parts.each do |p| - if p.content_type =~ /text\/plain/ - @body = p.charset ? p.body.decoded.force_encoding(p.charset).encode("UTF-8").to_s : p.body.to_s - return @body - elsif p.content_type =~ /text\/html/ - html = p.charset ? p.body.decoded.force_encoding(p.charset).encode("UTF-8").to_s : p.body.to_s - end + if p = @message.text_part + @body = p.charset ? p.body.decoded.force_encoding(p.charset).encode("UTF-8").to_s : p.body.to_s + return @body + elsif p = @message.html_part + html = p.charset ? p.body.decoded.force_encoding(p.charset).encode("UTF-8").to_s : p.body.to_s end end @@ -109,6 +107,7 @@ module Email html = @message.body.to_s end end + if html.present? @body = scrub_html(html) return @body diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb index ad0697111..2d8259bde 100644 --- a/spec/components/email/receiver_spec.rb +++ b/spec/components/email/receiver_spec.rb @@ -43,16 +43,6 @@ stripped from my reply?") end end - describe "it ignores messages it can't parse due to containing weird terms" do - let(:attachment) { File.read("#{Rails.root}/spec/fixtures/emails/attachment.eml") } - let(:receiver) { Email::Receiver.new(attachment) } - - it "processes correctly" do - expect { receiver.process}.to raise_error(Email::Receiver::EmptyEmailError) - expect(receiver.body).to be_blank - end - end - describe "it supports a dutch reply" do let(:dutch) { File.read("#{Rails.root}/spec/fixtures/emails/dutch.eml") } let(:receiver) { Email::Receiver.new(dutch) } @@ -187,6 +177,18 @@ greatest show ever created. Everyone should watch it. end + describe "email with attachments" do + it "can find the message and create a post" do + User.stubs(:find_by_email).returns(user) + EmailLog.stubs(:for).returns(email_log) + attachment_email = File.read("#{Rails.root}/spec/fixtures/emails/attachment.eml") + r = Email::Receiver.new(attachment_email) + r.expects(:create_reply) + expect { r.process }.to_not raise_error + expect(r.body).to eq("here is an image attachment") + end + end + end describe "processes a valid incoming email" do