Monthly Archives: April 2011

Adding welcome emails to dm-devise

I was looking for a way to have users greeted with a welcome email when they created accounts, and Devise doesn’t have a ‘welcomeable’ module. Initially I forked devise and started building one, but it’s actually so simple to add (at least with DataMapper) that I can see why there isn’t one included. So, here we go.

Subclass Devise’s mailer

We do this to keep the rest of this as parallel to Devise as possible.

class AppMailer < ::Devise::Mailer
  def welcome_message(resource)
    setup_mail(resource, :welcome_message)
And in config/initializers/devise.rb:
config.mailer = "AppMailer"

Add the template for your message

In app/views/devise/mailer/welcome_message.html.erb:

<p>Welcome <%= %>!</p>
Thanks for signing up for App.  
App is one of the greatest things you could sign up for. 
You won't regret it!

Add the title to the devise localization file

In config/devise/en.yml:

        subject: "Welcome!"

Connect the creation of a user to sending the mail

We’re going to use DataMapper’s CRUD hooks to do this. These are declared either before or after the CRUD operations. It used to be the case that dm hooks would work on any function at all, but that was deprecated a while back.

class User
  include DataMapper::Resource
  devise :database_authenticatable, :registerable,
            :recoverable, :rememberable, :trackable, :validatable
  after :create, :send_welcome_message
  def send_welcome_message
Now, because devise looks for the templates to render in the views directory, under the name of the mailer class, we need to move all our views from app/views/devise/mailer to app/views/app_mailer. Once that is done, everything should be up and running.