| Work | Research and Development (R&D) | Engineering | Infrastructure | How to setup Sendgrid on a Rails app
How to setup Sendgrid on a Rails app


This guide is a continuation of How to deploy a Rails app using Digital Ocean and will walk through how to SendGrid on your Rails application.

Sign up

Visit SendGrid and sign up for a free account.
Choose SMPT Relay setup. Enter a name for your api key myapp-prd.

Update Rails config/environment.rb

# Load the Rails application.
require_relative 'application'

# Initialize the Rails application.

ActionMailer::Base.smtp_settings = {
  :user_name => 'apikey',
  :password => Rails.application.secrets.sendgrid_api_key,
  :domain => 'myapp.com',
  :address => 'smtp.sendgrid.net',
  :port => 465,
  :authentication => :plain,
  :enable_starttls_auto => true

Add API key to config/secrets.yml

Paste your API key into your development config/secrets.yml. Make sure to not check this into Git / your code repository.

  secret_key_base: [myapp-dev]
  sendgrid_api_key: [sendgrid-myapp-dev] 

SSH into your server deploy@ip_address.

$ sudo nano myapp/shared/config/secrets.yml 

Paste in your API key, save and exit

  secret_key_base: [myapp-prd]
  sendgrid_api_key: [sendgrid-myapp-prd] 

Update config/environments/production.rb and config/environments/development.rb

Add these two lines to the bottom of your environment files to use smtp

  config.action_mailer.default_url_options = { host: 'myapp.com' }
  config.action_mailer.delivery_method = :smtp