纸夹+ActionMailer-如何添加附件?

10

我有一个被别针卡住的文件,我想把它作为附件添加到我的电子邮件中....

类UserMailer < ActionMailer::Base 定义了一个名为XXXXXX_notification的方法,该方法接收参数record。

  attachments ??? How to add a paperclip file?

  mail( :to => "#{record.email}", 
        :subject => "XXXXXXXX"
        )
end

在谷歌上似乎没有关于这个主题的任何内容,如果你有任何想法,我很乐意听取 :)

谢谢

更新

  @comment.attachments.each do |a|
    tempfile = File.new("#{Rails.root.to_s}/tmp/#{a.attachment_file_name}", "w")
    tempfile << open(a.authenticated_url())
    tempfile.puts
    attachments[a.attachment_file_name] = File.read("#{Rails.root.to_s}/tmp/#{a.attachment_file_name}")
    # Delete it tempfile
    #File.delete("#{Rails.root.to_s}/tmp/#{a.filename}")
  end
2个回答

9
已经有答案了,但我想分享一种略微不同的方法来做到这一点:
这是我的模型报告。我正在使用Paperclip。
class Report < ActiveRecord::Base
  has_attached_file :pdf_file
  ...
end

这里是我的邮件发送器ReportMailer。

class ReportMailer < ActionMailer::Base
  def monthly_report_email(emails, report)
    attachments[report.pdf_file_file_name] = File.read(report.pdf_file.path)
    mail(:to => emails, :subject => 'monthly report')
  end
end

4

1
谢谢,但我无法弄清楚如何打开一个纸片文件。 - AnApprentice
所以我需要下载S3文件然后读取它吗?我不能直接访问并创建附件吗? - AnApprentice
是的。您必须下载文件才能附加它。 - Kevin Sylvestre
谢谢。我正在尝试,但还没有成功。我已经更新了代码,有什么想法吗? - AnApprentice

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接