Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sendgrid set up on Rails 4

I have a rails 4 app. I set up ActionMailer and I can send order confirmation emails via localhost and gmail.

I installed Sendgrid on Heroku and followed the set up instructions. I get a Net::SMTPSyntaxError (501 Syntax error

my environment.rb (i have sendgrid user/pwd in 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
}

in production.rb - the only actionamailer setting i have is this. I have this as a placeholder to put the real domain in later. I'm currently using herokuapp.com.

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

in my orders_controller within the order create method, I call the below.

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

What am I missing? It works on localhost with gmail so I'm missing something in the sendgrid settings or in the default_url in production.rb file.

like image 601
Moosa Avatar asked Sep 04 '14 19:09

Moosa


2 Answers

For posterity, here's a working setup for external SMTP in Rails on 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
}
like image 164
Richard Peck Avatar answered Oct 05 '22 23:10

Richard Peck


Change default from: "Test Email" to valid email address, even [email protected].

like image 43
Peter Tretyakov Avatar answered Oct 05 '22 23:10

Peter Tretyakov