Use a different delivery method with ActionMailer for just one email?

Given a Mailer instance in Rails 3, is there a way to override delivery_method on it?

I want to push some emails through a high priority transport network, while others use a more busy, low priority method.

I can tune the configuration at runtime and change it again, but this is fraught with potential side effects.

+6
source share
2 answers

It turns out that this only affects this particular 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 an instance up:

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

They do not seem to be documented, but work.

+6
source

I did this in a Rails 2 application where some emails were sent through ActionMailer and others were sent through ArMailer. This is not a bad decision if you do not change the delivery method in the same delivery, because it can lead to the delivery method not being changed in case of a delivery error, here is what I did:

 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
source

Source: https://habr.com/ru/post/896032/


All Articles