Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Given 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.

like image 571
d11wtq Avatar asked Aug 27 '11 10:08

d11wtq


People also ask

How does ActionMailer work?

Action Mailer allows you to send emails from your application using mailer classes and views. Mailers work very similarly to controllers. They inherit from ActionMailer::Base and live in app/mailers , and they have associated views that appear in app/views .

What generates information on the mailing run if available?

rb, production. rb, etc...) Generates information on the mailing run if available.

What is Action_ mailer in Rails?

Action Mailer allows you to send emails from your application using a mailer model and views. So, in Rails, emails are used by creating mailers that inherit from ActionMailer::Base and live in app/mailers. Those mailers have associated views that appear alongside controller views in app/views.


1 Answers

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.

like image 143
d11wtq Avatar answered Oct 05 '22 23:10

d11wtq