Rails 4.2 ActionMailer

From Rails 4.2, you are able to deliver an email via a background job.

ActionMailer classes come with two additional methods:

1 deliver_later
2 deliver_now

deliver_now is similar to the original deliver method; it sends the email synchronously

deliver_later makes use of your background job system to send the email.

To get this to work properly, some additional changes need to be made.

Firstly, make sure that your background job system is working properly.

This assumes that you have config.active_job.queue_adapter set to something like sidekiq:

1 config.active_job.queue_adapter = :sidekiq

Next you need to add the ‘mailers’ queue into the config file.

ActionMailer by default places background deliveries into this queue.

In sidekiq:

1 # config/sidekiq.yml
3 :queues:
4   - [mailers, 1]

Tail the development logs to make sure the deliveries are sent:

1 [ActiveJob] [ActionMailer::DeliveryJob] [fd25b472-9d80-47f2-9ea6-5da0e3cf7552] Performed ActionMailer::DeliveryJob from Sidekiq(mailers) in 3160.77ms

In a follow up post I will describe the steps needed to test mailers using deliver_later and the pitfalls to avoid.

Happy Hacking!