Ruby on rails 在Rails 4上设置Sendgrid

Ruby on rails 在Rails 4上设置Sendgrid,ruby-on-rails,heroku,sendgrid,Ruby On Rails,Heroku,Sendgrid,我有一个rails 4应用程序。我设置了ActionMailer,可以通过localhost和gmail发送订单确认电子邮件 我在Heroku上安装了Sendgrid,并按照设置说明进行了操作。我得到一个Net::SMTPSyntaxError(501语法错误 my environment.rb(我在application.yml中有sendgrid user/pwd) 在production.rb中-我唯一的actionamailer设置是这个。我把它作为占位符,以便以后放入真正的域。我目前正

我有一个rails 4应用程序。我设置了ActionMailer,可以通过localhost和gmail发送订单确认电子邮件

我在Heroku上安装了Sendgrid,并按照设置说明进行了操作。我得到一个
Net::SMTPSyntaxError(501语法错误

my environment.rb(我在application.yml中有sendgrid user/pwd)

在production.rb中-我唯一的actionamailer设置是这个。我把它作为占位符,以便以后放入真正的域。我目前正在使用herokuapp.com

config.action_mailer.default_url_options = { host: 'localhost:3000' }
在我的orders\u控制器中的OrderCreate方法中,我调用以下命令

AutoNotifier.orderconf_email(current_user, @order).deliver
自动通知程序.rb

class AutoNotifier < ActionMailer::Base
  default from: "Test Email"

  def orderconf_email(current_user, order)
        @buyer = current_user
        @order =  order
        mail(to: @buyer.email, subject: 'Thank you for your order.')
  end
end
类自动通知程序

我遗漏了什么?它在本地主机和gmail上工作,因此我在sendgrid设置或production.rb文件的默认url中遗漏了一些内容。

更改
默认自:“测试电子邮件”
发送到有效的电子邮件地址,甚至
example@example.com

对于子孙后代,以下是Heroku上Rails中外部SMTP的工作设置:

#config/environments/production.rb
config.action_mailer.smtp_settings = {
    :address   => "smtp.sendgrid.net",
    :port      => 587, # ports 587 and 2525 are also supported with STARTTLS
    :enable_starttls_auto => true, # detects and uses STARTTLS
    :user_name => ENV["SENDGRID_USERNAME"],
    :password  => ENV["SENDGRID_PASSWORD"], # SMTP password is any valid API key, when user_name is "apikey".
    :authentication => 'login',
    :domain => 'yourdomain.com', # your domain to identify your server when connecting
}

我想指出的是,这是通过SMTP发送电子邮件。虽然这个方法是完全好的,你也应该考虑通过API来发送。 要做到这一点,你需要指定一个拦截器。幸运的是,有一个Gem可以帮助你做到这一点。下面是一篇展示如何使用它的好文章


您在生产中使用什么堆栈?heroku配置中是否有ENV?因为heroku只会自动将ENV添加到竹堆上,而在Cedar上,您需要手动添加。我在Cedar上。是的,我在运行heroku配置时看到sendgrid用户/pwd。请尝试将production.rb中的主机更改为您的bla-bla-bla.herokapp.com。事实上,我没有config.action\u mailer…production.rb中的line和Sendgrid工作正常。我尝试在没有该行的情况下运行它,但仍然出现相同的错误我想,我发现了你的问题(:检查答案。哇…我浪费了太多时间盯着设置和文档!!它在localhost上工作,所以我甚至没有考虑更改它。
#config/environments/production.rb
config.action_mailer.smtp_settings = {
    :address   => "smtp.sendgrid.net",
    :port      => 587, # ports 587 and 2525 are also supported with STARTTLS
    :enable_starttls_auto => true, # detects and uses STARTTLS
    :user_name => ENV["SENDGRID_USERNAME"],
    :password  => ENV["SENDGRID_PASSWORD"], # SMTP password is any valid API key, when user_name is "apikey".
    :authentication => 'login',
    :domain => 'yourdomain.com', # your domain to identify your server when connecting
}