ruby on rails 3.2 - How to (with a mailer-observer) delete a temporary (generated attachement) file after the e-mail has been sent? -
i'm sending e-mails user attachment (report) file automatically generated of actionmailer.
this 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
called controller :
... 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 ...
my 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
the initialization of observer mailer in config/initilizer/my_mailer_observer_initializer.rb :
reportmailer.register_observer(generatedreportobserver)
thanks help
i' managed find correct instruction afrer digging deep rails library :
so end following app/models/generated_report_observer.rb :
class generatedreportobserver < activerecord::observer def self.delivered_email(message) file_path = '/tmp/' + message.attachments[0].filename file.delete file_path if file.exist? file_path end end
Comments
Post a Comment