diff --git a/CHANGELOG.md b/CHANGELOG.md index 5878f75eb9..3d1d78ec7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ * enhancements * Removed deprecations warning output for `Devise::Models::Authenticatable::BLACKLIST_FOR_SERIALIZATION` (@soartec-lab) + * Added `Devise.mailer_delivery_method` configuration to enable deferring email delivery with `:deliver_later` [#5610](https://github.com/heartcombo/devise/pull/5610) [@seanpdoyle](https://github.com/seanpdoyle) ### 4.9.2 - 2023-04-03 diff --git a/lib/devise.rb b/lib/devise.rb index 3847e190c6..cd2cd12616 100644 --- a/lib/devise.rb +++ b/lib/devise.rb @@ -166,6 +166,10 @@ module Test mattr_accessor :send_password_change_notification @@send_password_change_notification = false + # Used to control when to deliver Action Mailer emails + mattr_accessor :mailer_delivery_method + @@mailer_delivery_method = :deliver_now + # Scoped views. Since it relies on fallbacks to render default views, it's # turned off by default. mattr_accessor :scoped_views diff --git a/lib/devise/models/authenticatable.rb b/lib/devise/models/authenticatable.rb index e3466ebaf8..c8efdc7ee9 100644 --- a/lib/devise/models/authenticatable.rb +++ b/lib/devise/models/authenticatable.rb @@ -196,9 +196,11 @@ def devise_mailer # def send_devise_notification(notification, *args) message = devise_mailer.send(notification, self, *args) + mailer_delivery_method = Devise.mailer_delivery_method + # Remove once we move to Rails 4.2+ only. - if message.respond_to?(:deliver_now) - message.deliver_now + if message.respond_to?(mailer_delivery_method) + message.send(mailer_delivery_method) else message.deliver end diff --git a/lib/generators/templates/devise.rb b/lib/generators/templates/devise.rb index 4503f33f81..5e44f30229 100644 --- a/lib/generators/templates/devise.rb +++ b/lib/generators/templates/devise.rb @@ -29,6 +29,9 @@ # Configure the class responsible to send e-mails. # config.mailer = 'Devise::Mailer' + # Configure when to send e-mails. + # config.mailer_delivery_method = :deliver_now + # Configure the parent class responsible to send e-mails. # config.parent_mailer = 'ActionMailer::Base' diff --git a/test/mailers/mailer_test.rb b/test/mailers/mailer_test.rb index 6f9f568e8a..c27023453a 100644 --- a/test/mailers/mailer_test.rb +++ b/test/mailers/mailer_test.rb @@ -43,4 +43,20 @@ def computed_reply_to assert mail.from, "from@example.com" assert mail.reply_to, "reply_to@example.com" end + + test "defaults to immediate delivery" do + create_user + + assert_not_empty ActionMailer::Base.deliveries + assert_empty ActiveJob::Base.queue_adapter.enqueued_jobs + end + + test "supports deferred delivery with Devise.mailer_delivery_method = :deliver_later" do + swap Devise, mailer_delivery_method: :deliver_later do + create_user + + assert_empty ActionMailer::Base.deliveries + assert_not_empty ActiveJob::Base.queue_adapter.enqueued_jobs + end + end end