Skip to content

Welcome mailers

nrhammond edited this page · 1 revision

Welcome emails are really simple to implement in Clearance, although not completely obvious at first.

The proper place to trigger a welcome email would be somewhere when the user’s account is confirmed. Within Clearance this is done within the ConfirmationsController in the create action. If you take a look at app/controllers/clearance/confirmations_controller.rb#create you can see a great place to hook into, the @user.confirm_email! line.

def create
    @user = ::User.find_by_id_and_confirmation_token(
                   params[:user_id], params[:token])
    @user.confirm_email!

    sign_in(@user)
    flash_success_after_create
    redirect_to(url_after_create)
end

First, create the mailer that will send out the welcome email.

script/generate mailer notifier welcome

You’ll probably want to add in a param to the email for the user object so that you can greet your user by name and use other pieces of information from their account. Edit your notifier model in app/models/notifier.rb.

def welcome(user, sent_at = Time.now)
  subject    'Welcome!'
  recipients user.email
  from       'info@example.com'
  sent_on    sent_at

  body       :user => user
end

Now, go over to your application’s user model, most likely app/models/user.rb and add in the confirm_email! method.

def confirm_email!
    Notifier.deliver_welcome(self)
    super
end

We call super here just in case that method is ever implemented within Clearance, it will go up the chain and call the parent method.

All done.

Something went wrong with that request. Please try again.