Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support deferred Mailer delivery with deliver_later #5610

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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)

Please check [4-stable](https://github.com/heartcombo/devise/blob/4-stable/CHANGELOG.md)
for previous changes.
4 changes: 4 additions & 0 deletions lib/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/devise/models/authenticatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def devise_mailer
#
def send_devise_notification(notification, *args)
message = devise_mailer.send(notification, self, *args)
message.deliver_now
message.public_send(Devise.mailer_delivery_method)
end

def downcase_keys
Expand Down
3 changes: 3 additions & 0 deletions lib/generators/templates/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
16 changes: 16 additions & 0 deletions test/mailers/mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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