首页 > 解决方案 > 如何设置来自 gmail 的密码重置电子邮件并获得许可?

问题描述

我正在尝试创建一个清除密码重置电子邮件以从我创建的 gmail 帐户发送。当有人点击“重置密码”时,我需要做什么才能完成此操作并发送密码重置电子邮件?我已经设置

config.mailer_sender = 'newemail@email.com'

对不起,如果这是一个愚蠢的问题,我对轨道和间隙还是新手。

标签: ruby-on-railsrubyclearance

解决方案


这是您在 Clearance 中配置要从其发送的特定电子邮件地址的方式。把它放在:config/initializers/clearance.rb

Clearance.configure do |config|
    config.mailer_sender = "reply@example.com"
end

从文档

但在 Rails 中,您还需要配置服务器以发送电子邮件。/config/environments/development.rb

# Gmail configuration
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address:              'smtp.gmail.com',
  port:                 587,
  domain:               'example.com',
  user_name:            ENV['EMAIL_USER'],
  password:             ENV['EMAIL_PASS'],
  authentication:       'plain',
  enable_starttls_auto: true
}

但是,请密切注意两件事:

  1. 此示例使用 ENV 变量EMAIL_USEREMAIL_PASS它们是您的 bash 会话中需要存在的自定义环境变量(例如。$ export EMAIL_USER=my_email@gmail.com
  2. 此示例假设您仅在开发中执行此操作(对于从个人 Gmail 帐户发送应该是正确的)。如果你想在生产中使用它,你应该在/config/environments/production.rb

推荐阅读