Rails 4.0 Sneak Peek: Asynchronous ActionMailer

Brian Cardarella

CEO & Founder

Brian Cardarella

My previous deep dive into the Rails 4.0 Queueing system was motivated by a patch to Rails I was working on while at RailsCamp New England this past weekend. I’m happy to say that Rails 4.0 now has an optional asynchronous ActionMailer.

The API for pushing your emails to the background is very simple. If you want to make this change application wide simply set it in your application.rb (or in any of the environment files)

config.action_mailer.async = true

Or if you want to only make specific mailers asynchrounous

class WelcomeMailer < ActionMailer::Base
  self.async = true

That’s it! Any messages that are being delivered will be sent as a background job. In fact, the rendering is happening on the background as well.

You will need to take care that the arguments you are passing your mailers can be properly marshalled. Instead of:


You should do:


Then in your mailer:

class WelcomeMailer < ActionMailer::Base
  def welcome(id)
    @user = User.find(id)

Switching it up

The default queueing system is Rails.queue, but you can override this to use any queueing system you want by overriding ActionMailer::Base#queue.

class WelcomeMailer < ActionMailer::Base
  def queue

Your custom queue should expect the jobs to respond to #run, same as Rails.queue.


Much of the original code was cribbed (with permission) from Nick Plante’s resque_mailer gem.


Stay in the Know

Get the latest news and insights on Elixir, Phoenix, machine learning, product strategy, and more—delivered straight to your inbox.

Narwin holding a press release sheet while opening the DockYard brand kit box