我需要使用邮件程序向用户发送电子邮件,以将他们的密码设置为Devise和活动管理员的"可恢复"功能。在开发环境中,我已通过将以下内容添加到这些文件来做到这一点:
配置/环境/开发
#Added per active admin install instructions
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
#These settings are for the sending out email for active admin and consequently the devise mailer
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings =
{
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'gmail.com', #you can also use google.com
:authentication => :plain,
:user_name => 'XXXXX@gmail.com',
:password => 'XXXXXXX'
}
如何为生产环境获得相同的功能?我想将我的应用程序部署到 Heroku。我需要添加哪些文件和代码?
您在开发模式下设置的所有配置都将起作用,但您需要重新配置默认邮件程序 URL。
所以。
-
从 development.rb 复制粘贴您的设置。
-
将您的默认邮件程序指向您的 heroku 应用程序:
config.action_mailer.default_url_options = { :host => 'YOURAPPNAME.herokuapp.com' }
此外,请注意 SMTP 在迁移到生产环境时可能存在的任何电子邮件限制。例如,在开发时很难触发Gmail的smtp限制,但它们可以在生产中更容易触发。
如果它在开发模式下工作,那么它将在生产模式下工作。
假设一切都设置正确,在开发中重置密码已经使用您的Gmail帐户发送实际电子邮件。
Devise 仅依赖于正确的邮件程序配置设置(您已经完成),并将 Design 配置为允许密码重置,以及电子邮件的"发件人"字段的可能另一个设置。
这应该可以正常工作!
只要 config/environment/production.rb 有同样的事情,但有一个例外。该default_url_options仅在开发中应具有"localhost"的 :host 值,在 heroku 生产中应具有"YOURAPPNAME.herokuapp.com"。
即
config.action_mailer.default_url_options = { :host => 'YOURAPPNAME.herokuapp.com' }
请记住在Gmail上解锁验证码,否则它不会从heroku(未知来源)发送电子邮件。您可以通过转到此链接来执行此操作:http://www.google.com/accounts/DisplayUnlockCaptcha
作为一个建议,我会说从 environment.rb 中移动它
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
和地方在环境/开发.rb 作为
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
生产中不需要它。
请参阅 Net::SMTPAuthentication从 Rails 应用程序发送电子邮件时出错(在暂存环境中),了解有关 Gmail 将 heroku 视为未知主机的更多信息。