A
A
Anton Misyagin2018-08-30 14:40:17
Ruby on Rails
Anton Misyagin, 2018-08-30 14:40:17

How to read smtp ActionMailer settings in error_handler?

Here are the default smtp settings for the ActionMailer::Base class mail method
config/application.rb

module MyApp
  class Application < Rails::Application
...
    config.action_mailer.delivery_method = :smtp
    config.action_mailer.smtp_settings = {
        enable_starttls_auto: true,
        address: 'smtp.yandex.ru',
        port: 587,
....
        user_name: xxxx
        password: yyy
    }
  end

The code of some mailera. In some method, the smtp settings are tricky, taken randomly from somewhere in the database. We send in an asynchronous way:
mailer/some_mailer.rb
class SomeMailer < ActionMailer::Base
include Resque::Mailer
...
  def send_feedback opts
...
  	mail(to: to, subject: 'subj',
  		delivery_method_options: get_random_smtp_settings) 
  end
end

Let's say the account was blocked, I'm trying to find out about it in a timely manner. I want to know which account is dead. I'm trying to catch the error and send a message to the administrator about the error that has occurred. I want to know the username and password of the account from which I am trying to send a message.
config/initializers/resque_mailer.rb
Resque::Mailer.error_handler = lambda { |mailer, message, error, action, args|
if error.is_a?(Resque::TermException)
Resque.enqueue(mailer, action, *args)
else
SMS.send_error :admin, {error: error.message, error_class: error.class, smtp_login: ????, smtp_pass: ????}
raise error
end

It turns out to consider only the default settings:
mailer.smtp_settings[:user_name]
mailer.smtp_settings[:password]

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question