Ruby-on-rails 如何使用 Devise 设置电子邮件确认?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8186584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I set up email confirmation with Devise?
提问by jyli7
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?
是否有教程解释如何从头开始设置 Devise 的注册确认电子邮件(在开发和生产中),即如果您没有设置 Action Mailer?
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:
终于让它工作了。遵循下面接受的答案中的所有步骤,然后将以下内容添加到我的 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]"
}
回答by clyfe
1.Make sure you include confirmable in Model.devise call
1.确保在 Model.devise 调用中包含可确认
class User < ActiveRecord::Base
devise :database_authenticatable, :confirmable ...
end
2.Make sure you add confirmable to the user migration
2.确保在用户迁移中添加了confirmable
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.confirmableraises an error. Instead, copy the block labeled "Confirmable" from their migration guide.
如果您使用的是 devise 2.0+,这将失败,因为 devise 不再提供迁移助手,因此t.confirmable会引发错误。相反,从他们的迁移指南中复制标记为“可确认”的块。
3.Generate the devise views, with either of the following commands,so you can override the devise mailer views:
3.使用以下任一命令生成设计视图,以便您可以覆盖设计邮件程序视图:
rails generate devise:views # global
rails generate devise:views users # scoped
You can now override the mailer views in devise/mailer/confirmation_instructions.html.erbor users/mailer/confirmation_instructions.html.erbdepending on your setup
现在,您可以在邮件视图devise/mailer/confirmation_instructions.html.erb或users/mailer/confirmation_instructions.html.erb根据您的设置
4.For developmentenvironment add the following config lines in /config/environments/development.rb
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.For productionenvironment in /config/environments/production.rbyou may use something similar to the following (supposing you have a SMTP server on localhost:25):
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'
}
6To 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/:
6要测试开发中的设置,请安装 mailcatcher gem,您将在开发中将其用作 SMTP 服务器,捕获所有传入邮件并将其显示在http://localhost:1080/:
gem install mailcatcher
Once installed start the mailcatcher server with the command:
安装后,使用以下命令启动 mailcatcher 服务器:
mailcatcher
A toy SMTP server will be running on port 1025 catching emails and displaing them on HTTP port 1080.
一个玩具 SMTP 服务器将在端口 1025 上运行,以捕获电子邮件并在 HTTP 端口 1080 上显示它们。
You can now create an account and see the confirmations.
您现在可以创建一个帐户并查看确认信息。
回答by jyli7
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
我在 rails 3.2.0/ruby 1.9.2 中遇到了问题
回答by clem
Have you looked at the ActionMailer Rails Guide?

