134

是否有教程解释了如何从头开始设置 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]"
 }
4

3 回答 3

215

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+,则会失败,因为 devise 不再提供迁移帮助程序,因此t.confirmable会引发错误。相反,从他们的迁移指南中复制标有“可确认”的块。

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:25 上有一个 SMTP 服务器):

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 上。

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

于 2011-11-18T22:11:19.127 回答
7

我相信你应该再次编辑它......端口号。应该用引号引起来..像这样:-

:port => "587",

我在 rails 3.2.0/ruby 1.9.2 遇到问题

于 2012-08-22T11:22:18.043 回答
3

你看过ActionMailer Rails 指南吗?

于 2011-11-18T19:26:42.317 回答