2014-09-04 67 views
6

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

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

我的environment.rb(我有sendgrid用户名/密码在application.yml)

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 
} 
在production.rb

- 唯一actionamailer设置我已经是这样的。我把它作为一个占位符来放置真正的域名。我目前正在使用herokuapp.com。

config.action_mailer.default_url_options = { host: 'localhost:3000' } 

在我的orders_controller内的order create方法内,我在下面调用。

AutoNotifier.orderconf_email(current_user, @order).deliver 

auto_notifier.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的localhost上工作,所以我在sendgrid设置或production.rb文件的default_url中缺少一些东西。

+0

你在生产中使用什么堆栈?你有英雄配置中的ENV吗?因为heroku自动添加ENV的Bamboo堆栈上,雪松你需要手动添加它们,据我所知。 – 2014-09-04 19:54:36

+0

我在雪松。是的,当我运行heroku配置时,我看到了sendgrid用户/密码。 – Moosa 2014-09-04 19:56:30

+1

尝试将production.rb中的主机更改为bla-bla.herokuapp.com。实际上,我在production.rb中没有config.action_mailer ...行,并且Sendgrid工作正常。 – 2014-09-04 19:58:34

回答

4

default from: "Test Email"更改为有效的电子邮件地址,甚至[email protected]

+1

哇...我浪费了这么多时间盯着设置和文档!!它在本地主机上工作,所以我甚至没有考虑改变它。 – Moosa 2014-09-04 20:39:50

22

对于后人,这里是在Rails的外部SMTP在Heroku工作设置:

#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 
}