Delay and or resend Devise's confirmation emai

2020-05-14 08:16发布

问题:

I'm using Devise to allow user signup as-well-as using my own user admin to create users manually. When I create a user in the admin, Devise sends a confirmation immediately to the new user. I believe this is due to the fact that both devise and my admin use the same model. How do I delay this email until the administrator is ready to send it?

Additionally, Devise's validation is requiring the admin set a password for the new user. I would much prefer the manually created users specify their own password when they respond the confirmation. Right now manually created users will not know their password unless I send it too them in a supplemental email.

回答1:

We do this in one of our apps. You can tell Devise NOT to automatically deliver the confirmation like this:

@user.skip_confirmation!

And then later, you can do

Devise::Mailer.confirmation_instructions(@user).deliver

For Rails 2.x you'd do something like:

DeviseMailer.deliver_confirmation_instructions(@user)


回答2:

@user.skip_confirmation! confirms the user, so the user can log in without using the confirmation.

This works for me in devise 3.5

Stop devise to send confirmation email while creating user.

@user.skip_confirmation_notification! 

Send confirmation instructions later

@user.send_confirmation_instructions


回答3:

The solution is not so simple as @Ryan Heneise's answer. If you do @user.skip_confirmation! it confirms the user, so the user can log in without using the confirmation, so the confirmation letter in this case is useless. This solution does not allows the user to log in without the confirmation: Rails 3 with Devise for Authentication - How do I manually create a user?



回答4:

There's now an easier way (was added back in v2.2.4)

Devise's confirmable module now includes the perfect skip_confirmation_notification! method which allows for a cleaner solution:

@user = User.new params[:user]
@user.skip_confirmation_notification! 
@user.save

# ... do stuff, then later...

Devise::Mailer.confirmation_instructions(@user).deliver 


回答5:

I just spent some time looking into this, basically you just need to add this and setup delayed_job.

def send_confirmation_instructions
  generate_confirmation_token! if self.confirmation_token.nil?
  ::Devise.mailer.delay.confirmation_instructions(self)
end

Add it in your user model as protected method to override the devise one in confirmable. Also, note the gem was just updated to add a new method to be overridden on create that sends the confirmation instructions.



回答6:

There is now the devise-async project:

  • https://github.com/mhfs/devise-async
  • https://github.com/plataformatec/devise/wiki/How-To:-Send-devise-emails-in-background-(Resque,-Sidekiq-and-Delayed::Job)


回答7:

Do you use delayed job ? It allows you to define single methods for delayed run.