我基本上是通过在 ActionMailer 的帮助下自动生成的附件(报告)文件向用户发送电子邮件。
这是我的 app/mailer/report_mailer.rb :
class ReportMailer < ActionMailer::Base
default from: "server.email@my_domaine.com"
def generated_report(user, file_name, report_name, file_path)
@user = user
@report_name = report_name
attachments[file_name] = File.read(file_path)
mail(:to => user.email, :subject => "Generated report #{report_name} (automatic message)")
end
end
从我的控制器调用:
...
t = Time.now
file_name = "#{generated_name}_#{t.strftime("%Y-%m-%d_%HH-%M-%Ssec")}_#{t.usec}.xls"
file_path = '/tmp/' + file_name
ReportMailer.generated_report(current_user, file_name, generated_name, file_path).deliver
...
我的 app/models/generated_report_observer.rb :
class GeneratedReportObserver < ActiveRecord::Observer
def self.delivered_email(message)
file_path = ????
File.delete file_path if File.exist? file_path
end
end
在 config/initilizer/my_mailer_observer_initializer.rb 中为我的邮件程序初始化我的观察者:
ReportMailer.register_observer(GeneratedReportObserver)
感谢帮助