Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
721 views
in Technique[技术] by (71.8m points)

ruby on rails - How do I set up email confirmation with Devise?

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]"
 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...