paperclip + ActionMailer – 添加附件?
我有一个回形针文件,我想添加为我的电子邮件的附件….
class UserMailer <ActionMailer :: Base def XXXXXX_notification(record)@record = record
attachments ??? How to add a paperclip file? mail( :to => "#{record.email}", :subject => "XXXXXXXX" ) end
通过谷歌似乎没有任何关于这个话题,如果你有任何想法,我很想听听:)
谢谢
UPDATE
@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指南(只能通过Bing访问):
http://guides.rubyonrails.org/action_mailer_basics.html#sending-emails-with-attachments
剩下的就是将附件(如果在S3中)下载到文件对象或访问它,它将存储在本地。 尝试使用open-uri 。
它已经得到了回答,但我只想分享一种略有不同的方式:
这是我的模特报告。 我正在使用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