valve's

corner

Different SMTP Settings for ActionMailer Action

Sometimes you may want to set per-action SMTP settings that are different from site-wide settings. You might try to set them in mailer:

1
2
3
4
5
6
7
class UserMailer < ActionMailer::Base
  ActionMailer::Base.smtp_settings = {address: 'some.domain'}

  def user_registered(user)
    mail(to: user.email, subject: 'You registered here!')
  end
end

But this will override the smtp settings for the rest of mailers.

Instead, set them for the email instance:

1
2
3
4
5
6
# assuming in controller action
mail = UserMailer.user_registered(current_user)
custom_smtp_settings = {address: 'some.domain', port: 25}
mail.delivery_method.settings.merge! custom_smtp_settings
# now this will send using custom SMTP settings
mail.deliver

rails-3.1

Comments