【问题标题】:Rails mailer PDF attachment brokenRails 邮件程序 PDF 附件损坏
【发布时间】:2014-08-04 13:20:23
【问题描述】:

在我的一个邮件程序中,我有这种方法可以发送附有 pdf 的电子邮件:

def send_offer(customer, ip)
    @customer = customer
    attachments['offer.pdf'] = {
      :encoding => 'base64',
      :content  => Base64.encode64(File.read(Rails.root.join('app', 'assets', 'images', 'offer.pdf')))
    }
    mail(:to => "mail@example.com",
         :from => "mail@example.com",
         :body =>"this tag is important when do any attachment",
         :subject => "#{@customer[:name]} Offer")
end

电子邮件已发送,但当我打开它时,附件的 PDF 已损坏。文件大小为670 bytes

原始PDF是263 KB (269.485 byte)

怎么可能?

【问题讨论】:

  • 仅在 Windows 上发生..
  • 你试过没有编码吗? ActionMailer 默认编码 Base64:attachments['offer.pdf'] = File.read(...)
  • 已经尝试了各种解决方案,但它在 Windows 上不起作用。它真的没那么重要,因为它在 UNIX 系统上运行良好。
  • 明白了。我的最后一个建议是在文本编辑器中打开那个 670 字节的文件,看看它是否类似于错误消息,或者更有可能是像 #<File:0x007fc9840b37e0> 这样的文件描述符。

标签: ruby-on-rails ruby ruby-on-rails-3


【解决方案1】:

我遇到了同样的问题,here 的解决方案帮我解决了:

attachments[file_name] = File.open(file_location, 'rb'){|f| f.read}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多