How can I send a welcome email to newly registered users in Rails using Devise?

picardo picture picardo · Nov 10, 2010 · Viewed 15.3k times · Source

I am using Devise on Rails and I'm wondering if there is a hook or a filter that I can use to add a bit of code to Devise's user registration process and send a welcome email to the user after an account has been created. Without Devise it would be something like this...

  respond_to do |format|
      if @user.save
        Notifier.welcome_email(@user).deliver    # <======= 
  ...   

Answer

Arcolye picture Arcolye · Feb 22, 2013

The next most popular answer assumes you're using using Devise's :confirmable module, which I'm not.

I didn't like the other solutions because you have to use model callbacks, which will always send welcome emails even when you create his account in the console or an admin interface. My app involves the ability to mass-import users from a CSV file. I don't want my app sending a surprise email to all 3000 of them one by one, but I do want users who create their own account to get a welcome email. The solution:

1) Override Devise's Registrations controller:

#registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController

  def create
    super
    UserMailer.welcome(resource).deliver unless resource.invalid?
  end

end

2) Tell Devise you overrode its Registrations controller:

# routes.rb
devise_for :users, controllers: { registrations: "registrations" }