I have a need to use two different smtp servers in a Rails application. It appears that the way ActionMailer is constructed, it is not possible to have different smtp_settings for a subclass. I could reload the smtp settings for each mailer class whenever a message is being sent, but that messes up the ExceptionNotifier plugin which is outside my control (unless I mess with it too). Does anyone have a solution/plugin for something like this?
Ideally I would like to have
class UserMailer < ActionMailer::Base; end
and then set in environment.rb
ActionMailer::Base.smtp_settings = standard_smtp_settings
UserMailer.smtp_settings = user_smtp_settings
Thus, most of my mailers including ExceptionNotifier would pickup the default settings, but the UserMailer would use a paid relay service.
Solution for Rails 4.2+:
config/secrets.yml:
config/environments/production.rb:
app/mailers/application_mailer.rb:
app/mailers/user_mailer.rb:
When I wanted a quick test in the console (Rails 5) I did the following:
https://github.com/AnthonyCaliendo/action_mailer_callbacks
I found this plugin helped solve the problem for me pretty easily (as in < 5 mins). I simply change the @@smtp_settings for a particular mailer in the before_deliver and then change it back to the defaults in the after_deliver. Using this approach, I only have to add the callbacks to mailers that need @@smtp_settings different than the default.
For anybody approaching this issue with later versions (3+) of Rails, try this
http://guides.rubyonrails.org/action_mailer_basics.html#sending-emails-with-dynamic-delivery-options
I'm afraid it's not doable natively.
But you can trick it a bit by modifying the @@smtp_settings variable in the model.
There's an article on Oreilly which explains it pretty well even though they code is not clean at all. http://broadcast.oreilly.com/2009/03/using-multiple-smtp-accounts-w.html