0

我设法发送了一封电子邮件,其中包含存储在 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 字节的形式出现。我正在使用此处发布的示例回形针 + ActionMailer - 添加附件?. 我错过了什么吗?

4

1 回答 1

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)
于 2012-07-13T11:57:44.757 回答