开发者

Uploading a file through Paperclip or Carrierwave from a Mail attachment

开发者 https://www.devze.com 2023-01-27 08:10 出处:网络
If I have a mail object, eg: mail = Mail.new do from\"jim@gmail.com\" to\"jane@yahoo.com\" subject\"Example\"

If I have a mail object, eg:

mail = Mail.new do
  from      "jim@gmail.com"
  to        "jane@yahoo.com"
  subject   "Example"
  text_part do
    body    "开发者_JAVA百科Blarg"
  end
  add_file  "/some/file/or/some_such.jpg"
end

If I were to receive the above mail in my application

received_mail = mail.encoded
Message.parse(received_mail)

How would I pass the attachment on to CarrierWave/Paperclip (not fussed about which, I'll use whichever one handles this best)? I've tried a few different methods, but I keep running in to various stumbling blocks - has anyone got a working solution for it?

My current attempt is:

mail.attachments.each do |attachment|
  self.attachments << Attachment.new(:file => Tempfile.new(attachment.filename) {|f| f.write(attachment.decoded)})
end

This doesn't appear to work - any tips? end


I know that when I tried to take mail attachments and use them with paperclip, I also ran into some problems. The problem as I remember it was that paperclip expected certain attributes on the File object passed to it.

I solved it like this:

mail.attachments.each do |attachment|
  file = StringIO.new(attachment.decoded)
  file.class.class_eval { attr_accessor :original_filename, :content_type }
  file.original_filename = attachment.filename
  file.content_type = attachment.mime_type

  #Then you attach it where you want it
  self.attachments << Attachment.new(:file => file)
0

精彩评论

暂无评论...
验证码 换一张
取 消