如何使用Devise设置电子邮件确认? [英] How do I set up email confirmation with Devise?

查看:153
本文介绍了如何使用Devise设置电子邮件确认?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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搜索刚刚出现了与此相关的一系列单独的部分。没有一件足够的解释,我不知道他们如何配合在一起。那里有一个一步一步的解释,甚至是解释初始步骤的东西吗?

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?

最后让它工作。按照以下接受的答案中的所有步骤,然后将以下内容添加到我的environment.rb文件中:

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


推荐答案

strong> 1。确保您在Model.devise调用中包含可确认

1. Make sure you include confirmable in Model.devise call

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

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

2. Make sure you add confirmable to the user migration

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

如果您使用devise 2.0+,则失败是因为devise不再提供迁移助手,因此 t.confirmable 引发错误。而是从他们的迁移指南

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。使用以下任一命令生成设计视图,所以你可以覆盖设计邮件视图:

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

现在,您可以覆盖 devise / mailer / confirmation_instructions中的邮件视图.html.erb users / mailer / confirmation_instructions.html.erb 根据您的设置

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。对于开发环境,请在 /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 中,您可能会使用类似于以下内容(假设您在本地主机上拥有SMTP服务器:25):

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

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

玩具SMTP服务器将在端口1025上运行,捕获电子邮件并将其置于HTTP端口1080上。

A toy SMTP server will be running on port 1025 catching emails and displaing them on HTTP port 1080.

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

这篇关于如何使用Devise设置电子邮件确认?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆