在Heroku上托管的Ruby on Rails中,Sendgrid /电子邮件发送问题

16

我在部署在Heroku上使用Authlogic进行身份验证的Rails 3.1应用程序中,无法成功地通过SendGrid发送电子邮件。 我在config / environments / [development.rb和production.rb]中有以下的Action Mailer配置:

config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.default_charset = "utf-8"
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
  :address => 'smtp.sendgrid.net',
  :port => 587,
  :domain => ENV['SENDGRID_DOMAIN'],
  :user_name =>  ENV['SENDGRID_USERNAME'],
  :password => ENV['SENDGRID_PASSWORD'],
  :authentication => 'plain',
  :enable_starttls_auto => true
}

对于production.rb,以上代码相同,只是


    config.action_mailer.default_url_options = { :host => [Heroku应用名称] }

当我在开发模式下运行它时,报告了以下错误:


    Completed 500 Internal Server Error in 21740ms
    Net::SMTPFatalError (550 Cannot receive from specified address notification@[app-domain]: Unauthenticated senders not allowed
):

我现在不知道如何设置才能使其正常工作。 有没有经验丰富的人帮忙解决这个问题?

非常感谢。 你们是最棒的 !!!

3个回答

44

我为此花了半天的时间,终于让我的项目运行起来了。由于糟糕的文档错误,我感到非常沮丧。顺便一提,我正在运行 Rails 3.1 和 Cedar stack on Heroku。

http://devcenter.heroku.com/articles/sendgrid 告诉你将 SMTP 设置放在 config/initializers/mail.rb 文件中。但是,在 http://docs.sendgrid.com/documentation/get-started/integrate/examples/rails-example-using-smtp/ 中指出,应该将所有 SMTP 设置放在 config/environment.rb 文件中,而不是 config/initializers/mail.rb

因此,解决方案是将SMTP设置放入 environment.rb 文件中。以下是我的 environment.rb 文件的内容:

# Load the rails application
require File.expand_path('../application', __FILE__)

# Initialize the rails application
Freelanceful::Application.initialize!

# Configuration for using SendGrid on Heroku
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
  :user_name => "yourSendGridusernameyougetfromheroku",
  :password => "yourSendGridpasswordyougetfromheroku",
  :domain => "staging.freelanceful.com",
  :address => "smtp.sendgrid.net",
  :port => 587,
  :authentication => :plain,
  :enable_starttls_auto => true
}

要获取您的SendGrid用户名和密码,请输入

$ heroku config -long

希望这可以帮到你...并帮助更多的人解决这个头疼问题。


1
抱歉,那是我的错误。那是一个配置问题。在Heroku中,RACK_ENV和RAILS_ENV都是“production”,但我编辑了config/environments/staging.rb。 - Junichi Ito
1
我也遇到过这个问题,感谢 Heroku 的文档。但是,只要在 config/initializers/mail.rb 中不写上 ActionMailer::Base.delivery_method = :smtp,你仍然可以将 SMTP 设置放在那里。 - jmdeldin
1
不要忘记像我一样在Heroku上设置环境变量。heroku config:add SENDGRID_USERNAME=foo和heroku config:add SENDGRID_PASSWORD=bar - seanmrafferty
https://devcenter.heroku.com/articles/sendgrid 上说在“domain”字段中填写heroku.com,这应该是应用程序的域名还是Heroku的域名? - Harry Moreno
发件人在我所在的暂存区中运作良好,其中SMTP设置位于config/initializers/mail.rb,但在生产环境中却无法正常工作!不确定原因。 - nish
显示剩余3条评论

0
请注意,如果您在Bamboo堆栈上运行Heroku应用程序,则无需在environment.rb文件中配置设置,因为Heroku会为您完成。 但是,在将应用程序激活到Heroku后,至少需要进行一次git push以设置这些设置。今天早上我犯了这个错误并发现了您的帖子。

0

我猜你的意思是开发模式指本地吧?如果是的话,我认为SendGrid插件不允许您从Heroku网络外发送电子邮件(因为他们有独立的帐户,他们更喜欢您使用)。

话虽如此,在使用SendGrid插件时,您无需在生产中配置邮件,因为当您部署应用程序时,它会自动为您配置。

因此,您可以删除config.action_mailer.smtp_settings代码,并在开发中使用默认设置。


我在 production.rb 中注释掉了 smtp_settings,然后根据 heroku 日志出现了连接被拒绝的错误。Errno::ECONNREFUSED(Connection refused - connect(2)):我应该在 Gemfile 中包含 sendgrid 作为 gem 吗? - Ringo Blancke
不,你只需要将插件添加到应用程序中,然后进行部署即可。 - Neil Middleton
2
@NeilMiddleton Heroku文档确实指出,您应该在Cedar堆栈上添加smtp_settings。但如果是Aspen或Bamboo,则可以将其省略。Heroku上的Sendgrid使用 - Pete

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