3

我正在使用mailmain gem 来获取 pop3 邮件。该库使用邮件gem 来分解邮件正文和附件。我已经到了可以在 pry 命令行中获取附件的地步,如下所示:

14: Mailman.config.rails_root = '../'
15: 
16: Mailman::Application.run do
17:   to 'expenses@surveymonkey.com' do
18:     require 'debugger'; debugger
=> 19:     print message
20:   end
21: end

我可以像这样得到一个单独的附件

[1] pry(#<Mailman::Router>)> a = message.attachments[0]
=> #<Mail::Part:70339703566060, Multipart: false, Headers: <Content-Type: image/jpeg;   name="70s-Jump-Suit.jpeg">, <Content-Transfer-Encoding: base64>, <Content-Disposition: attachment; filename="70s-Jump-Suit.jpeg"; size=38412; creation-date="Tue, 26 Jun 2012 22:11:10 GMT"; modification-date="Tue, 26 Jun 2012 22:11:10 GMT">, <Content-Description: 70s-Jump-Suit.jpeg>>

所以,问题是,我如何保存这些数据?

我很接近这种方法,但我无法正确保存。

我试过这样的东西

[2] pry(#<Mailman::Router>)> File.open( '/tmp/output.jpg', "w+b", 0644 ) { |f| f.write a.raw_source }

但输出被搞砸了。

我只是对电子邮件编码知之甚少,无法使其正常工作。

提前致谢!

4

1 回答 1

3

啊,我们开始:

http://cbpowell.wordpress.com/2011/01/17/saving-attachments-with-ruby-1-9-2-rails-3-and-the-mail-gem/

# tmail is now a Mail object
tmail.attachments.each do |tattch|
  fn = tattch.filename
  begin
    File.open( fn, "w+b", 0644 ) { |f| f.write tattch.body.decoded }
  rescue Exception => e
    logger.error "Unable to save data for #{fn} because #{e.message}"
  end
end
于 2012-06-27T00:52:11.173 回答