139

Is there a tutorial out there that explains how to set up Devise's signup confirmation email from scratch (in both development and production), i.e. if you don't have Action Mailer set up?

Google searching has just turned up a bunch of separate pieces related to this. No one piece explains enough, and I'm not sure how they fit together. Is there a step-by-step explanation out there, or even something that explains the initial steps?


Finally got it working. Followed all the steps in the accepted answer below, then added the following to my environment.rb file:

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
   :tls => true,
   :address => "smtp.gmail.com",
   :port => 587,
   :domain => "gmail.com",
   :authentication => :login,
   :user_name => "[username]",
   :password => "[password]"
 }
jyli7
  • 2,731
  • 6
  • 23
  • 31

3 Answers3

221

1. Make sure you include confirmable in Model.devise call

class User < ActiveRecord::Base
  devise :database_authenticatable, :confirmable ...
end

2. Make sure you add confirmable to the user migration

create_table :users do |t|
  t.database_authenticatable
  t.confirmable
  ...
end

If you're using devise 2.0+ this fails because devise no longer provides migration helpers, and so t.confirmable raises an error. Instead, copy the block labeled "Confirmable" from their migration guide.

3. Generate the devise views, with either of the following commands,so you can override the devise mailer views:

rails generate devise:views # global
rails generate devise:views users # scoped

You can now override the mailer views in devise/mailer/confirmation_instructions.html.erb or users/mailer/confirmation_instructions.html.erb depending on your setup

4. For development environment add the following config lines in /config/environments/development.rb

config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {:address => "localhost", :port => 1025}

5. For production environment in /config/environments/production.rb you may use something similar to the following (supposing you have a SMTP server on localhost:25):

config.action_mailer.default_url_options = {:host => 'yourdomain.com'}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address => "127.0.0.1",
  :port    => 25,
  :domain  => 'yourdomain.com'
}

6 To test the setup in development install the mailcatcher gem, that you will use as a SMTP server in development, catching all incoming mails and displaying them on http://localhost:1080/:

gem install mailcatcher

Once installed start the mailcatcher server with the command:

mailcatcher

A toy SMTP server will be running on port 1025 catching emails and displaing them on HTTP port 1080.

You can now create an account and see the confirmations.

ANeves
  • 6,219
  • 3
  • 39
  • 63
clyfe
  • 23,695
  • 8
  • 85
  • 109
  • 1
    Wow, thanks for that amazingly comprehensive answer. I've got it working such that the mail catcher is catching the emails, but no email actually shows up in my inbox. I've tried two different email addresses and checked the spam folders in both. Is there something I might be missing here? (I'm in development mode). – jyli7 Nov 18 '11 at 22:32
  • Realized that the mail catcher was preventing them from going through. When I turned off mail catcher, my server threw up this error: Errno::ECONNREFUSED in Devise::RegistrationsController#create. Any ideas? – jyli7 Nov 18 '11 at 22:49
  • 2
    In development you don't need to actually deliver the emails to the address. Mailcatcher has a web interface on http://localhost:1080/ that you can open and see the catched emails - that's the point of it, to make it simple for you in development. In production however, you want to use a real SMTP server (Google Apps, qmail, postfix etc talk to your sysadmin) – clyfe Nov 19 '11 at 00:10
  • 3
    And don't forget to restart your server! – Matt Bond May 23 '13 at 13:37
  • 10
    Devise 2.0 no longer provides migration helpers, and so `t.confirmable` raises an error. Instead, copy the block labeled "Confirmable" from their migration guide: https://github.com/plataformatec/devise/wiki/How-To:-Upgrade-to-Devise-2.0-migration-schema-style#after – Ross Allen Aug 14 '13 at 19:22
  • 1
    Awesome tutorial... thanks for sharing.. +1.. Also worth to see [How To: Add :confirmable to Users](https://github.com/plataformatec/devise/wiki/How-To:-Add-:confirmable-to-Users) page. – Arup Rakshit May 12 '15 at 06:28
  • The letter_opener gem is a better choice than mailcatcher – user3763682 Jun 13 '15 at 21:57
  • I'm getting an error: SSL_connect returned=1 errno=0 state=unknown state: unknown protocol. what could I do to make this work? – Aleksandrus Aug 28 '15 at 00:42
  • @clyfe thank you for your answer. do you know if your instructions would be the same given rails 5? – BenKoshy Feb 02 '18 at 01:37
7

I believe you should edit it once again... port no. should be in quotes .. Like this :-

:port => "587",

I faced a problem in rails 3.2.0/ruby 1.9.2

3

Have you looked at the ActionMailer Rails Guide?

clem
  • 3,524
  • 3
  • 25
  • 41