开发者

Use a different delivery_method with ActionMailer just for a single email?

开发者 https://www.devze.com 2023-03-30 23:23 出处:网络
开发者_如何学PythonGiven a Mailer instance in Rails 3, is there a way to override the delivery_method on it?

开发者_如何学PythonGiven a Mailer instance in Rails 3, is there a way to override the delivery_method on it?

I want to push certain emails out through a high-priority transport, and others use a busier, low-priority method.

I can adjust the config at runtime and change it back again, but this is fraught with potential side-effects.


It turns out that doing this affects just this specific Mailer, without changing ActionMailer::Base globally.

class SomeMailer < ActionMailer::Base
  self.delivery_method = :high_priority  

  def some_email(params)
  end
end

You can also do this (warning: will affect all instances of AnotherMailer), if you have the instance up-front:

mail = AnotherMailer.whatever_email
mail.delivery_handler.delivery_method = :something_else

These don't seem to be documented, but work.


I used to do that in a Rails 2 application where some emails were sent via ActionMailer and others were sent via ArMailer. It's not a bad solution as long as you don't change the delivery method back in the same delivery because it could cause the delivery method not to be changed in case of error with the delivery, this is what a I made:

class Mailer1

  def my_mail1
    config
  end

private

  def config 
    ActionMailer::Base.delivery_method = :smtp
  end

end


class Mailer2

  def my_mail2
    config
  end

private

  def config 
    ActionMailer::Base.delivery_method = :sendmail
  end

end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号