Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save each email before sending rails 4

I want to keep track of all triggered emails by the application into a db table, so that i can have a log which emails are sent and to whom.

Kindly suggest me the best possible solution.

like image 503
Baran Avatar asked Nov 05 '13 05:11

Baran


1 Answers

I have solved this using the following way:

created a class in lib directory

class MyProjectMailLogger

  def self.delivering_email(message)
   @to = message.to.to_s
   @subject = message.subject.to_s
   @message = message.body.to_s
   EmailQueue.create!(:receipient_email => @to, :subject => @subject, :message => @message, :email_status_id => 3)
 end

end

In config/initalizers/setup_mail.rb

ActionMailer::Base.register_interceptor(MyProjectMailLogger)

You might need to add the following line in the application.rb file as its not include files from lib directory:

config.autoload_paths += %W(#{config.root}/lib)

Yay!! and i logged my emails.

like image 155
Baran Avatar answered Nov 16 '22 10:11

Baran