title: Trying to set up action_mailer with Sendgrid and heroku on Rails 5 API only app
body: <p>I'm trying to set up a Rails 5 API only app that receives a POST request with form data and emails that information to a specific email. I'm basically trying this to a contact form and trying to get rails to email that message to me.</p>
  
  <p>I set up a Rails 5 API only app and got everything to work in local server. But when I deploy it to heroku, the app keeps crashing. Here's the logs:-</p>
  
  <pre><code>2017-10-21T12:31:49.779066+00:00 heroku[web.1]: Starting process with command `bin/rails server -p 28435 -e production`
      2017-10-21T12:31:53.419975+00:00 app[web.1]: =&gt; Run `rails server -h` for more startup options
      2017-10-21T12:31:53.419961+00:00 app[web.1]: =&gt; Booting Puma
      2017-10-21T12:31:53.419975+00:00 app[web.1]: =&gt; Rails 5.1.4 application starting in production 
      2017-10-21T12:31:53.419976+00:00 app[web.1]: Puma starting in single mode...
      2017-10-21T12:31:53.419981+00:00 app[web.1]: * Version 3.10.0 (ruby 2.3.4-p301), codename: Russell's Teapot
      2017-10-21T12:31:53.419982+00:00 app[web.1]: * Min threads: 5, max threads: 5
      2017-10-21T12:31:53.419982+00:00 app[web.1]: * Environment: production
      2017-10-21T12:31:53.568829+00:00 heroku[web.1]: Process exited with status 1
      2017-10-21T12:31:53.420126+00:00 app[web.1]: * Listening on tcp://******:28435
      2017-10-21T12:31:53.463851+00:00 app[web.1]: Exiting
      2017-10-21T12:31:53.465644+00:00 app[web.1]: /app/vendor/bundle/ruby/2.3.0/gems/puma-3.10.0/lib/puma/binder.rb:270:in `initialize': getaddrinfo: Name or service not known (SocketError)
      2017-10-21T12:31:53.465656+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.3.0/gems/puma-3.10.0/lib/puma/binder.rb:270:in `new'
      2017-10-21T12:31:53.465664+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.3.0/gems/puma-3.10.0/lib/puma/binder.rb:105:in `block in parse'
      2017-10-21T12:31:53.465663+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.3.0/gems/puma-3.10.0/lib/puma/binder.rb:270:in `add_tcp_listener'
      2017-10-21T12:31:53.465665+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.3.0/gems/puma-3.10.0/lib/puma/binder.rb:88:in `each'
      2017-10-21T12:31:53.465665+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.3.0/gems/puma-3.10.0/lib/puma/binder.rb:88:in `parse'
      2017-10-21T12:31:53.465666+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.3.0/gems/puma-3.10.0/lib/puma/runner.rb:144:in `load_and_bind'
      2017-10-21T12:31:53.465667+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.3.0/gems/puma-3.10.0/lib/puma/single.rb:87:in `run'
      2017-10-21T12:31:53.465667+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.3.0/gems/puma-3.10.0/lib/puma/launcher.rb:183:in `run'
      2017-10-21T12:31:53.465668+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.3.0/gems/puma-3.10.0/lib/rack/handler/puma.rb:69:in `run'
      2017-10-21T12:31:53.465669+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.3.0/gems/rack-2.0.3/lib/rack/server.rb:297:in `start'
      2017-10-21T12:31:53.465679+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:44:in `start'
      2017-10-21T12:31:53.465680+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:135:in `block in perform'
      2017-10-21T12:31:53.465680+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `tap'
      2017-10-21T12:31:53.465681+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `perform'
      2017-10-21T12:31:53.465681+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.3.0/gems/thor-0.20.0/lib/thor/command.rb:27:in `run'
      2017-10-21T12:31:53.465682+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.3.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command'
      2017-10-21T12:31:53.465683+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.3.0/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
      2017-10-21T12:31:53.465687+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/command.rb:44:in `invoke'
      2017-10-21T12:31:53.465686+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/command/base.rb:63:in `perform'
      2017-10-21T12:31:53.465687+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/commands.rb:16:in `&lt;top (required)&gt;'
      2017-10-21T12:31:53.465688+00:00 app[web.1]:    from bin/rails:9:in `require'
      2017-10-21T12:31:53.465689+00:00 app[web.1]:    from bin/rails:9:in `&lt;main&gt;'
      2017-10-21T12:31:53.582296+00:00 heroku[web.1]: State changed from starting to crashed
      2017-10-21T12:32:04.663844+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=****** request_id=ae664dd1-a48e-4033-9533-79464373cba8 fwd="157.50.15.44" dyno= connect= service= status=503 bytes= protocol=https
      2017-10-21T12:32:06.597178+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=****** request_id=48a773e2-e949-4820-b415-42a3071f79b9 fwd="157.50.15.44" dyno= connect= service= status=503 bytes= protocol=https
  </code></pre>
  
  <p>There's this specific error " getaddrinfo: Name or service not known (SocketError)" that I'm getting. I looked this up and found several sources suggesting to add 'require resolv-replace' in the initializers. I did and there's no change. The app keeps crashing.</p>
  
  <p>I must mention that I copy pasted code from my working Rails 5 (non-API) application and adapted controller methods to render in JSON. </p>
  
  <p>And as I mentioned, I got it to work in the local server, so it's got something to do with heroku and the sendgrid addon that I added via the command on the CLI. </p>
  
  <p>Here's the code:-</p>
  
  <pre><code>class ContactsController &lt; ApplicationController
  
  def create
      @message = Message.new(message_params)
      if @message.valid?
          ContactMailer.contact(@message).deliver_now
          render json: {status: 'SUCCESS', message: 'Message sent successfully!'}, status: :ok
      else
          render json: {status: 'ERROR', message: 'Message sending failed!', data: @message.errors}, status: :unprocessable_entity
      end
  end
  
    private
  
      def message_params
        params.permit(:subject, :name,
                                        :email, :content)
      end
  end
  </code></pre>
  
  <p>Lines added to production.rb</p>
  
  <pre><code>   config.action_mailer.raise_delivery_errors = true
      config.action_mailer.delivery_method = :smtp
      host = ENV['HOST']
      config.action_mailer.default_url_options = { host: host }
      ActionMailer::Base.smtp_settings = {
        :port           =&gt; ENV['SMTP_PORT'],
        :address        =&gt; ENV['SMTP_ADDRESS'],
        :authentication =&gt; :plain,
        :user_name      =&gt; ENV['SENDGRID_USERNAME'],
        :password       =&gt; ENV['SENDGRID_PASSWORD'],
        :domain         =&gt; 'heroku.com',
        :enable_starttls_auto =&gt; true
      } 
  </code></pre>
  
  <p>The environment variables are added to heroku config variables via the settings page online.</p>
  
  <p>Please let me know if any other information would be helpful.</p>