Forum: Ruby on Rails Email attachment

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2d1351be76ef25ba85b403f29e42453f?d=identicon&s=25 Markus (Guest)
on 2007-01-07 21:11
I'm quite new to Ruby and Rails. Now I'm stuck on a problem with
receiving emails. Everything but attachments work just fine. I try to
save the attached file (jpeg) in a file.

I use a Windows XP system and use Outlook to send the mails.

My receive function looks like this:
class Receiver < ActionMailer::Base
def receive(email)
  Post.create(:title => email.subject, :body => email.body, :created_at
=> Time.now)
  email.parts.each do |part|
    filename = "#{RAILS_ROOT}/public/post/image/test.jpg"
    File.open(filename,File::CREAT|File::TRUNC|File::WRONLY,0644){ |f|
      f.write(part.body)
    }
  end
end

The problem is that the saved file is not an exact copy of the original
file. I see the pattern below:
Original:  .. 07 08 09 0A 0B FF ..
Saved:     .. 07 08 09 0D 0A 0B FF ..
(All 0A are replaced with 0D 0A)

So, there is a problem with "line feeds" in the image data. But what and
why? What can I do about it?
2d1351be76ef25ba85b403f29e42453f?d=identicon&s=25 Markus (Guest)
on 2007-01-08 17:49
Problem solved. Need a "file.binmode"
This topic is locked and can not be replied to.