22
votes

In Rails 4.2.0, I am using the Devise gem for authentication - devise 3.4.1.

The confirmation email is sent as soon as a user is created. We need to skip this confirmation email and send a custom email with the confirmation link. Please help me to solve this issue.

:confirmable is used in the User model.

3
You want to "skip" the email confirmation or you want to "change" the confirmation mail? - Alper Karapınar

3 Answers

52
votes

You can try this

@user.skip_confirmation!
@user.save

Call skip_confirmation! before saving the user record.

To skip the confirmation notification only you can use skip_confirmation_notification!

@user.skip_confirmation_notification!
@user.save

Hope this helps!


In Addition

if you wish to skip confirmation on the same line without creating a variable and you do not need to confirm that particular User, for example in seeds.rb:

User.create(email: "#{role_name}@example.com",
            password: 'password',
            password_confirmation: 'password',
            confirmed_at: Time.now.utc, # skip confirmation)

or

If you just want to override the default email template, then you can run

rails generate devise:views -v registrations confirmations

It will generate the default views for confirmations then you can override it according to your need.

2
votes

Instead of disabling devise's in-build confirmation email and sending your custom email, you can customize devise's confirmation email. You can generate devise view files using

rails generate devise:views

and then customize confirmation email.

1
votes

You can do this:

this can are the UserController

@user = User.new(email: "#{role_name}@example.com",
        password: 'password',
        password_confirmation: 'password',
        confirmed_at: Time.now.utc) # skip confirmation
@user.skip_confirmation!
@user.confirm
if @user.save
  MyMailer.confirmation(@user)
else
  # your implementation
end

See the doc https://github.com/plataformatec/devise/wiki/How-To:-Use-custom-mailer