A gem plugin which allows messages prepared by ActionMailer to be delivered asynchronously. Assumes you're using Qu for your background jobs.
Note that Qu::Mailer only work with Rails 3.x.
Include Qu::Mailer in your ActionMailer subclass(es) like this:
class MyMailer < ActionMailer::Base include Qu::Mailer end
Now, when @MyMailer.subject_email(params).deliver@ is called, an entry will be created in the job queue. Your Qu workers will be able to deliver this message for you. The queue we're using is imaginatively named mailer, so just make sure your workers know about it and are loading your environment:
bundle exec rake environment qu:work QUEUES=mailer
Note that you can still have mail delivered synchronously by using the bang method variant:
Oh, by the way. Don't forget that your async mailer jobs will be processed by a separate worker. This means that you should resist the temptation to pass database-backed objects as parameters in your mailer and instead pass record identifiers. Then, in your delivery method, you can look up the record from the id and use it as needed.
If you want to set a different default queue name for your mailer, you can change the @default_queue_name@ property like so:
# config/initializers/qu_mailer.rb Qu::Mailer.default_queue_name = 'application_specific_mailer'
This is useful when you are running more than one application using Qu::Maile in a shared environment. You will need to use the new queue name when starting your workers.
bundle exec rake environment qu:work QUEUES=application_specific_mailer
Qu::Mailer as a Project Default
If you have a variety of mailers in your application and want all of them to use Qu::Mailer by default, you can subclass ActionMailer::Base and have your other mailers inherit from an AsyncMailer:
# config/initializers/qu_mailer.rb class BackgroundMailer < ActionMailer::Base include Qu::Mailer end # app/mailers/example_mailer.rb class ExampleMailer < AsyncMailer def say_hello(user) # ... end end
# Rails 3.x: add it to your Gemfile gem 'qu-mailer'
You don't want to be sending actual emails in the test environment, so you can configure the environments that should be excluded like so:
# config/initializers/qu_mailer.rb Qu::Mailer.excluded_environments = [ :test, :cucumber ]
Note on Patches / Pull Requests
- Fork the project.
- Make your feature addition or bug fix.
- Add tests for it. This is important so I don't break it in a future version unintentionally.
- Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
- Send me a pull request. Bonus points for topic branches.
This work is a port of ResqueMailer by Nick Plante. Modified to work with the Qu queueing library by Morton Jonuschat.