如何使用Devise设置电子邮件确认?

时间:2011-11-18 17:41:24

标签: ruby-on-rails devise actionmailer confirmation

是否有一个教程解释如何从头开始设置Devise的注册确认电子邮件(在开发和生产中),即如果您没有设置Action Mailer?

谷歌搜索刚刚发现了一堆与此相关的单独部分。没有一个人能够解释得足够多,而且我不确定它们是如何组合在一起的。是否有逐步解释,甚至是解释初始步骤的东西?


终于搞定了。按照下面接受的答案中的所有步骤,然后将以下内容添加到我的environment.rb文件中:

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]"
 }

3 个答案:

答案 0 :(得分:201)

1. 确保在Model.devise调用中包含确认

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

2. 确保向用户迁移添加确认

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

如果您使用的是devise 2.0+,则会失败,因为设计不再提供迁移助手,因此t.confirmable会引发错误。而是从their migration guide复制标有“可确认”的块。

3. 使用以下任一命令生成设计视图,以便您可以覆盖设计邮件程序视图:

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

您现在可以覆盖devise/mailer/confirmation_instructions.html.erbusers/mailer/confirmation_instructions.html.erb中的邮件程序视图,具体取决于您的设置

4. 对于开发环境,在/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. 对于/config/environments/production.rb中的生产环境,您可以使用类似于以下的内容(假设您在localhost上有SMTP服务器:25):< / p>

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 要在开发中测试设置,请安装mailcatcher gem,它将用作开发中的SMTP服务器,捕获所有传入的邮件并在http://localhost:1080/上显示:

gem install mailcatcher

安装完成后,使用以下命令启动mailcatcher服务器:

mailcatcher

玩具SMTP服务器将在端口1025上运行,捕获电子邮件并在HTTP端口1080上替换它们。

您现在可以创建一个帐户并查看确认信息。

答案 1 :(得分:7)

我相信你应该再次编辑它...... 港口号应该在引号中..像这样: -

:port => "587",

我在rails 3.2.0 / ruby​​ 1.9.2

中遇到了问题

答案 2 :(得分:3)

你看过ActionMailer Rails Guide吗?