Rails 3 ActionMailer发送0字节pdf附件

时间:2012-07-13 11:16:48

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

我已设法发送一封包含存储在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?。我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

文件对象被缓冲 - 直到你关闭文件(你不是),然后你写的字节可能不在磁盘上。不要忘记调用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)