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.

This commit is contained in:
Neil Lalonde 2014-03-28 09:57:12 -04:00
parent d23585e444
commit fd504e741f
2 changed files with 19 additions and 18 deletions

View file

@ -37,7 +37,7 @@ module Email
def process def process
raise EmptyEmailError if @raw.blank? raise EmptyEmailError if @raw.blank?
@message = Mail::Message.new(@raw) @message = Mail.new(@raw)
# First remove the known discourse stuff. # First remove the known discourse stuff.
@ -92,15 +92,13 @@ module Email
# If the message is multipart, find the best type for our purposes # If the message is multipart, find the best type for our purposes
if @message.multipart? if @message.multipart?
@message.parts.each do |p| if p = @message.text_part
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 @body = p.charset ? p.body.decoded.force_encoding(p.charset).encode("UTF-8").to_s : p.body.to_s
return @body return @body
elsif p.content_type =~ /text\/html/ elsif p = @message.html_part
html = p.charset ? p.body.decoded.force_encoding(p.charset).encode("UTF-8").to_s : p.body.to_s html = p.charset ? p.body.decoded.force_encoding(p.charset).encode("UTF-8").to_s : p.body.to_s
end end
end end
end
if @message.content_type =~ /text\/html/ if @message.content_type =~ /text\/html/
if defined? @message.charset if defined? @message.charset
@ -109,6 +107,7 @@ module Email
html = @message.body.to_s html = @message.body.to_s
end end
end end
if html.present? if html.present?
@body = scrub_html(html) @body = scrub_html(html)
return @body return @body

View file

@ -43,16 +43,6 @@ stripped from my reply?")
end end
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 describe "it supports a dutch reply" do
let(:dutch) { File.read("#{Rails.root}/spec/fixtures/emails/dutch.eml") } let(:dutch) { File.read("#{Rails.root}/spec/fixtures/emails/dutch.eml") }
let(:receiver) { Email::Receiver.new(dutch) } let(:receiver) { Email::Receiver.new(dutch) }
@ -187,6 +177,18 @@ greatest show ever created. Everyone should watch it.
end 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 end
describe "processes a valid incoming email" do describe "processes a valid incoming email" do