问题描述
是否有教程解释了如何从头开始设置 Devise 的注册确认电子邮件(在开发和生产中),即如果您没有设置 Action Mailer?
Google 搜索刚刚找到了一堆与此相关的单独部分.没有一篇文章解释得足够清楚,我不确定它们是如何组合在一起的.是否有一步一步的解释,或者甚至可以解释初始步骤?
终于搞定了.按照下面接受的答案中的所有步骤进行操作,然后将以下内容添加到我的 environment.rb 文件中:
ActionMailer::Base.delivery_method = :smtpActionMailer::Base.smtp_settings = {:tls =>真的,:地址 =>"smtp.gmail.com",:端口 =>587,:域 =>"gmail.com",:认证=>:登录,:user_name =>[用户名]",:密码 =>[密码]"}
1. 确保在 Model.devise 调用中包含可确认
class User
2. 确保您在用户迁移中添加了可确认的
create_table :users do |t|t.database_authenticablet. 可确认的...结尾
如果您使用的是 devise 2.0+,这将失败,因为 devise 不再提供迁移帮助程序,因此 t.confirmable
会引发错误.相反,从 他们的迁移指南.
3. 使用以下任一命令生成设计视图,以便您可以覆盖设计邮件程序视图:
rails 生成 devise:views # globalrails generate devise:views users # scoped
您现在可以根据您的设置覆盖 devise/mailer/confirmation_instructions.html.erb
或 users/mailer/confirmation_instructions.html.erb
中的邮件程序视图>
4. 对于 development 环境,在 /config/environments/development.rb
config.action_mailer.default_url_options = { :host =>'本地主机:3000' }config.action_mailer.delivery_method = :smtpconfig.action_mailer.smtp_settings = {:address =>"本地主机", :port =>1025}
5. 对于 /config/environments/production.rb
中的 production 环境,您可以使用类似于以下内容的内容(假设您有本地主机上的 SMTP 服务器:25):
config.action_mailer.default_url_options = {:host =>'yourdomain.com'}config.action_mailer.delivery_method = :smtpconfig.action_mailer.smtp_settings = {:地址 =>"127.0.0.1",:端口 =>25,:域 =>'yourdomain.com'}
6 要在开发中测试设置,请安装 mailcatcher gem,您将在开发中将其用作 SMTP 服务器,捕获所有传入邮件并将它们显示在 http://localhost 上:1080/
:
gem 安装 mailcatcher
安装后使用以下命令启动mailcatcher服务器:
mailcatcher
一个玩具 SMTP 服务器将在端口 1025 上运行,以捕获电子邮件并在 HTTP 端口 1080 上显示它们.
您现在可以创建一个帐户并查看确认信息.
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]"
}
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.
这篇关于如何使用 Devise 设置电子邮件确认?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!