2014-09-04 48 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 
}