【问题标题】:Rails 3.0.7 ActionMailer attachment issueRails 3.0.7 ActionMailer 附件问题
【发布时间】:2011-05-24 04:04:00
【问题描述】:

我正在尝试将文件附加到外发电子邮件,但附件大小最终为 1 个字节。不管我转发什么附件,它总是以 1 字节大小的电子邮件结尾(损坏)。在我看来,其他一切都很好。

电子邮件信息从 IMAP 帐户中提取并存储在数据库中以供浏览。附件存储在文件系统中,其文件名存储为电子邮件的关联记录。

在视图中有一个选项可以将电子邮件转发给其他收件人。它在 Rails 2.3.8 中工作,但对于 Rails 3,我不得不更改方法的附件部分,所以现在看起来像......

def forward_email(email_id, from_address, to_address)
    @email = Email.find(email_id)
    @recipients = to_address
    @from = from_address
    @subject = @email.subject
    @sent_on = Time.now
    @body = @email.body + "\n\n"

    @email.attachments.each do |file|
      if File.exist?(file.full_path)
        attachment :filename => file.file_name, :body => File.read(file.full_path)
      else
        @body += "ATTACHMENT NOT FOUND: #{file.file_name}\n\n"
      end
    end
end

我也试过...

attachments[file.file_name] = File.read(file.full_path)

并添加 :mime_type 和 :content_type 无济于事。

任何帮助将不胜感激。

谢谢!

【问题讨论】:

  • 我与 Nokogiri 合作过很多次。你可以把我当作付费开发者(我收费很少)。我看到this question

标签: ruby-on-rails attachment actionmailer


【解决方案1】:

这是我尝试并为我工作的方法

 attachments.each do |file|
      attachment :content_type => MIME::Types.type_for(file.path).first.content_type, :body => File.read(file.path) 
 end

【讨论】:

    【解决方案2】:

    文件可读吗?你能通过放置这样的东西来调试问题吗?

    logger.debug "File: #{file.full_path.inspect} : #{File.read(file.full_path).inspect[0..100]}"
    

    你的 development.log 中有什么内容吗?

    【讨论】:

    • 谢谢。这是我在日志中得到的... 文件:“/rails/jts/attachments_development/2011/2815/14035482661(1 分 3 秒)语音邮件.wav”:“RIFFl\224\001\000WAVEfmt \024\ 000\000\0001\000\001\000@\037\000\000Y\006\000\000A\000\000\000\002\00 除此之外,开发日志中没有任何注意事项
    • 如果我这样做... attachments[file.file_name] = {:content => File.read(file.full_path)} 然后我会在底部获得附件的文本版本电子邮件,但电子邮件不知道它是附件。电子邮件大小看起来不错
    【解决方案3】:

    好吧,Rails 团队的某个人回答了我的问题。问题在于添加正文内容(@body)而不是方法内的附件。如果要附加文件,则必须使用视图模板。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-11
      • 2011-08-09
      • 2013-04-09
      • 2015-02-16
      • 2010-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多