【问题标题】:How do I set up email confirmation with Devise?如何使用 Devise 设置电子邮件确认?
【发布时间】:2011-11-18 17:41:23
【问题描述】:

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

Google 搜索刚刚发现了一堆与此相关的单独部分。没有一篇解释得足够多,我不确定它们是如何组合在一起的。有没有一步一步的解释,或者甚至是解释初始步骤的东西?


终于搞定了。按照下面接受的答案中的所有步骤操作,然后将以下内容添加到我的 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]"
 }

【问题讨论】:

    标签: ruby-on-rails devise actionmailer confirmation


    【解决方案1】:

    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 会引发错误。而是从their migration guide 复制标有“可确认”的块。

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

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

    【讨论】:

    • 哇,感谢您提供如此全面的答案。我已经让它工作了,邮件捕捉器正在捕捉电子邮件,但实际上没有电子邮件出现在我的收件箱中。我尝试了两个不同的电子邮件地址,并检查了两者中的垃圾邮件文件夹。有什么我可能在这里遗漏的吗? (我处于开发模式)。
    • 在开发中,您不需要将电子邮件实际发送到该地址。 Mailcatcher 在localhost:1080 上有一个网络界面,您可以打开它并查看捕获的电子邮件——这就是它的重点,让您在开发过程中变得简单。但是,在生产中,您想使用真正的 SMTP 服务器(Google Apps、qmail、postfix 等与您的系统管理员交谈)
    • 别忘了重启你的服务器!
    • Devise 2.0 不再提供迁移助手,因此t.confirmable 会引发错误。相反,从他们的迁移指南中复制标记为“可确认”的块:github.com/plataformatec/devise/wiki/…
    • 很棒的教程...感谢分享..+1..也值得一看How To: Add :confirmable to Users页面。
    【解决方案2】:

    我认为您应该再次编辑它... 端口号应该用引号引起来..像这样:-

    :port => "587",
    

    我在 rails 3.2.0/ruby 1.9.2 遇到问题

    【讨论】:

    • 正确。或者 "1025" 如果使用 mailcatcher。
    【解决方案3】:

    你看过ActionMailer Rails Guide吗?

    【讨论】:

      猜你喜欢
      • 2016-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      • 2019-08-04
      • 2012-08-10
      • 2011-03-03
      • 1970-01-01
      相关资源
      最近更新 更多