【问题标题】:Rails 3 ActionMailer sending 0 bytes pdf attachmentsRails 3 ActionMailer 发送 0 字节 pdf 附件
【发布时间】:2012-07-13 19:57:44
【问题描述】:

我已成功发送一封电子邮件,其中包含存储在 s3 上的 pdf 附件

def welcome_pack1(website_registration)
      require 'open-uri'
      @website_registration = website_registration
      email_attachments = EmailAttachment.find(:all,:conditions=>{:goes_to_us=>true})

      email_attachments.each do |a|
        tempfile = File.new("#{Rails.root.to_s}/tmp/#{a.pdf_file_name}", "w")
        tempfile << open(a.pdf.url)
        tempfile.puts
        attachments[a.pdf_file_name] = File.read("#{Rails.root.to_s}/tmp/#{a.pdf_file_name}")
      end

      mail(:to => website_registration.email, :subject => "Welcome")
  end

附件附在电子邮件中。但它们以 0 字节的形式出现。我正在使用此处发布的示例paperclip + ActionMailer - Adding an attachment?。我错过了什么吗?

【问题讨论】:

    标签: ruby-on-rails actionmailer email-attachments


    【解决方案1】:

    文件对象被缓冲 - 直到你关闭文件(你不是)然后你写的字节可能不在磁盘上。不要忘记调用 close 的一个好方法是使用块形式:

    File.open(path, 'w') do |f|
      #do stuff to f
    end #file closed for you when the block is exited.
    

    我不确定你为什么要使用文件 - 为什么不这样做

    attachments[a.pdf_file_name] = open(a.pdf.url)
    

    【讨论】:

    • 电子邮件中的文件附件是否不必存储在本地才能附加?你的第二个建议给出了一个错误:未定义的方法`[]'
    • 否 - 您可以提供文件,也可以只提供数据(这就是您正在做的事情,因为您正在调用 File.read - 操作邮件程序不知道字节来自哪里从)。不知道为什么没有更多信息会出现该错误
    猜你喜欢
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    • 2011-04-13
    相关资源
    最近更新 更多