如何在Rails中使用Heroku和Sendgrid配置Devise电子邮件?

11

我有一个简单的Rails 3.2.7应用程序,添加了Devise,并部署到了添加了Sendgrid的Heroku上。它在Heroku上运行得很好,除了需要发送电子邮件来恢复密码时遇到的问题。根据我阅读的所有帖子,我怀疑我可能设置了不正确的邮件参数。如果有任何建议,将不胜感激。

对于config/environments/production.rb,我添加了以下内容:

config.action_mailer.default_url_options = { :host => 'smtp.sendgrid.net'} 

我在config/initializers/devise.rb中添加了以下内容:

config.mailer_sender = "mail-to-send@from.com"

并且对于config/environments.rb,我添加了

ActionMailer::Base.smtp_settings = {
:address        => 'smtp.sendgrid.net',
:port           => '587',
:authentication => :plain,
:user_name      => ENV['SENDGRID_USERNAME'],
:password       => ENV['SENDGRID_PASSWORD'],
:domain         => 'heroku.com',
:enable_starttls_auto => true
}
1个回答

8
所以,你的问题在于引用了错误的环境变量。Heroku将你的SendGrid凭据存储在ENV ['SENDGRID_USERNAME']ENV ['SENDGRID_PASSWORD']中。你使用了实际的用户名和密码作为键名。
以下代码可以运行:
ActionMailer::Base.smtp_settings = {
  :address        => 'smtp.sendgrid.net',
  :port           => '587',
  :authentication => :plain,
  :user_name      => ENV['SENDGRID_USERNAME'],
  :password       => ENV['SENDGRID_PASSWORD'],
  :domain         => 'heroku.com',
  :enable_starttls_auto => true
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接