Save each email before sending rails 4

痞子三分冷 提交于 2019-12-21 02:42:15

问题


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.


回答1:


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.




回答2:


I am doing some research on this and it seems like https://github.com/ankane/ahoy_email is a nice gem to accomplish this. It uses an interceptor internally like the accepted answer. It also integrates with a free event tracking library that has a number of different backends. Might be something to consider if you're starting something like this today.



来源:https://stackoverflow.com/questions/19782574/save-each-email-before-sending-rails-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!