【问题标题】:paperclip + ActionMailer - Adding an attachment?回形针 + ActionMailer - 添加附件?
【发布时间】:2010-12-05 01:46:22
【问题描述】:

我有一个回形针文件,我想将它作为附件添加到我的电子邮件中......

类 UserMailer

  attachments ??? How to add a paperclip file?

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

google 上似乎没有关于该主题的内容,如果您有任何想法,我很想听听 :)

谢谢

更新

  @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

【问题讨论】:

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


    【解决方案1】:

    已经回答了,但我只想分享一种稍微不同的方法:

    这是我的模型报告。我正在使用回形针。

    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
    

    【讨论】:

      【解决方案2】:

      来自 Ruby on Rails 指南(只能通过 Bing 访问):

      http://guides.rubyonrails.org/action_mailer_basics.html#sending-emails-with-attachments

      剩下的就是将附件(如果在 S3 中)下载到 file 对象或访问它,它存储在本地。尝试使用open-uri。

      【讨论】:

      • 谢谢,但这是我无法弄清楚的回形针文件的打开方式
      • 所以我需要下载S3文件然后阅读??我不能只访问它并创建附件吗?
      • 是的。您必须下载文件才能附加它。
      • 谢谢。我正在尝试,但还没有运气。我更新了代码有什么想法吗?
      猜你喜欢
      • 2014-06-04
      • 2014-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多