首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MailMan和保存附件

MailMan和保存附件
EN

Stack Overflow用户
提问于 2012-06-27 07:07:36
回答 1查看 1.9K关注 0票数 3

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

代码语言:javascript
复制
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

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

代码语言:javascript
复制
[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>>

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

我已接近this method,但无法正确保存。

我试过像这样的东西

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

但输出却搞砸了。

我只是对电子邮件编码的了解不够,无法让它正常工作。

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-27 08:52:11

啊,我们开始吧:

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

代码语言:javascript
复制
# 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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11217190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档