views:

71

answers:

1

I am trying to save an image attachment sent to my rails app to s3 using paperclip however I am hitting these problems:

Its getting to the attachment loop and failing with a

NoMethodError (undefined method `size' for #<Mail::Part: 
0x2b62856e8030>): 
  app/mailers/user_mailer.rb:23:in `receive' 
  app/mailers/user_mailer.rb:14:in `each' 
  app/mailers/user_mailer.rb:14:in `receive' 
  app/controllers/emails_controller.rb:10:in `create' 

which is line

:photo_file_size => attachment.size, 

commenting out that line, i hit this error when it tries to create:

NoMethodError (undefined method `to_tempfile' for #<Mail::Part: 
    0x2ac5eb944220>): 

Here is my code below. Appreciate the help..

class UserMailer < ActionMailer::Base 
  def receive(email) 
     @user = User.find_or_create_by_email( 
                                    #:name => FIXME, 
                                    :email => email.from, 
                                    :password => 'password', 
                                    :password_confirmation => 'password' 
                                    ) 
     @item = Item.create(:title => email.subject, :user => @user, :price => 50) 
     if email.has_attachments? 
        for attachment in email.attachments 
        @item.photos.create( 
                            :photo => attachment, 
                            :photo_file_name => attachment.original_filename, 
                            :photo_content_type => attachment.content_type, 
                            :photo_file_size => attachment.size, 
                            :photo_updated_at => Time.now.to_datetime) 
         @item.photos << attachment 
        end 
      end 
   end 
end 

Doing an inspect on my attachment object gives me this:

#<Mail::Part:23597877753640, Multipart: false, Headers: <Date: Wed, 25 Aug 2010 16:55:07 -0700>, <Mime-Version: 1.0>, <Content-Type: image/JPG; name="photo.jpeg">, <Content-Transfer-Encoding: base64>, <Content-Disposition: inline; filename=photo.jpeg>, <Content-ID: <[email protected]>>>
A: 

I experienced the same issue with rails 3.0.0 + paperclip + ActionMailer. I was able to work around the issue (ugly but working) by:

if mail_message.has_attachments? 
  for attachment in mail_message.attachments 
    tempfile=File.open(attachment.original_filename,'w')
    tempfile.write_nonblock(attachment.body)
    asset = Asset.new(:photo => File.open(tempfile))
    asset.save!
    tempfile.close
  end  
end
rrichards