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

jyl*_*li7 125 ruby-on-rails actionmailer confirmation devise

是否有一个教程解释如何从头开始设置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]"
 }
Run Code Online (Sandbox Code Playgroud)

cly*_*yfe 205

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

class User < ActiveRecord::Base
  devise :database_authenticatable, :confirmable ...
end
Run Code Online (Sandbox Code Playgroud)

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

create_table :users do |t|
  t.database_authenticatable
  t.confirmable
  ...
end
Run Code Online (Sandbox Code Playgroud)

如果您正在使用devise 2.0+,则会失败,因为设计不再提供迁移帮助程序,因此t.confirmable会引发错误.而是从其迁移指南中复制标记为"可确认"的块.

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

rails generate devise:views # global
rails generate devise:views users # scoped
Run Code Online (Sandbox Code Playgroud)

现在,您可以在邮件视图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}
Run Code Online (Sandbox Code Playgroud)

5.对于生产环境,/config/environments/production.rb您可以使用类似于以下内容的东西(假设您在localhost上有一个SMTP服务器: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'
}
Run Code Online (Sandbox Code Playgroud)

6要在开发中测试设置,请安装mailcatcher gem,您将在开发中将其用作SMTP服务器,捕获所有传入的邮件并将其显示在http://localhost:1080/:

gem install mailcatcher
Run Code Online (Sandbox Code Playgroud)

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

mailcatcher
Run Code Online (Sandbox Code Playgroud)

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

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

  • Devise 2.0不再提供迁移帮助程序,因此`t.confirmable`会引发错误.而是从他们的迁移指南中复制标记为"可确认"的块:https://github.com/plataformatec/devise/wiki/How-To:-Upgrade-to-Devise-2.0-migration-schema-style#after (10认同)
  • 别忘了重新启动服务器! (3认同)
  • 在开发过程中,您不需要实际将电子邮件发送到该地址。Mailcatcher 在 http://localhost:1080/ 上有一个 Web 界面,您可以打开并查看捕获的电子邮件 - 这就是它的重点,让您的开发变得简单。然而,在生产中,您想要使用真正的 SMTP 服务器(Google Apps、qmail、postfix 等,请与您的系统管理员联系) (2认同)

小智 7

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

:port => "587",
Run Code Online (Sandbox Code Playgroud)

我在rails 3.2.0/ruby​​ 1.9.2中遇到了问题