#lib/exception_mail_notify.rb
class ExceptionMailNotify < ActionMailer::Base
@@receivers = []
@@subject_prefix = ''
def self.receivers=(r)
@@receivers = r
end
def self.subject_prefix=(s)
@@subject_prefix = s
end
def self.receivers
@@receivers
end
def app_exception_notify(recipient, body)
from 'server@abc.com'
recipients recipient
subject "[#{@@subject_prefix}Exception] #{Time.now}"
content_type "text/html"
body body
end
end
class ApplicationController < ActionController::Base
protected
def rescue_action(exception)
if RAILS_ENV == 'production'
error = "
" + exception.class.to_s + "
" +
"
" + exception.message.inspect + "
" +
exception.backtrace.join(' ')
ExceptionMailNotify.deliver_app_exception_notify ExceptionMailNotify.receivers.join(', '), error
end
super exception
end
end
#app/controllers/application_controller.rb
#收件人
ExceptionMailNotify.receivers = %w[a@abc.com b@abc.com]
#邮件主题前缀
ExceptionMailNotify.subject_prefix = 'WWW '