Quick-Start Guide to SMTP Settings in Rails 6 Environments

Allene Norton
3 min readJan 18, 2021

Rails 6 allows you to create applications that send automated emails using its Action Mailer feature. In order to use this feature, you need to allow your app to communicate with your remote email server via the SMTP protocol. In simple terms, SMTP is a protocol for handling the treatment and formatting of data relayed between a client and a server. SMTP servers usually call for authentication of clients through credentials before allowing access, so you’ll need to code these settings into your environment files before your mailers will function. Let’s walk through how to configure SMTP settings for the development and production environments in Rails 6.

Encrypting Credentials

Before you configure your environment mailers, it is highly recommended to encrypt your credentials. This will keep your sensitive data from being exposed. I wrote a short guide to setting environment credentials in Rails 6 that you can check out here. Once you’ve verified that you can access your credentials in the console, you can use them to set up your SMTP settings.

Development

I have found it helpful to test my mailers in the development environment before preparing them for production. To set up SMTP in your development environment, open ‘config/environments/development.rb’. You will want to either uncomment, edit, or add the following lines of code:

 config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { host: ‘localhost:3000’, protocol: ‘http’ }
config.action_mailer.perform_deliveries = true
config.action_mailer.default :charset => “utf-8”
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: ‘smtp.gmail.com’,
port: 587,
user_name: Rails.application.credentials.smtp[:SMTP_USERNAME],
password: Rails.application.credentials.smtp[:SMTP_PASSWORD],
authentication: :plain,
enable_starttls_auto: true
}

Note: these settings are specific to Gmail, if you are using another email provider, you will need to look up the port, address, and settings for your specific provider.

Production

Setting SMTP for production is basically the same process as setting SMTP for development, with a minor change. Open ‘config/environments/production.rb’ and uncomment, edit, or add the following lines of code:

 config.action_mailer.raise_delivery_errors = false
config.action_mailer.default_url_options = { host: ‘$YOUR_DOMAIN.COM’, protocol: ‘http’ }
config.action_mailer.perform_deliveries = true
config.action_mailer.default :charset => “utf-8”
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: ‘smtp.gmail.com’,
port: 587,
user_name: Rails.application.credentials.smtp[:SMTP_USERNAME],
password: Rails.application.credentials.smtp[:SMTP_PASSWORD],
authentication: :plain,
enable_starttls_auto: true
}

Instead of ‘localhost:3000’, once your app is hosted you will need to change the default url to the domain at which your app is hosted. This way, any links in mailers will be redirected to ‘yourdomain.com/link’ instead of local host.

Google Workspace (formerly G-Suite)

If your organization uses a custom domain through Google Workspace, there are a couple changes you’ll need to make in addition to what’s outlined above.

 config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { host: ‘$YOUR_DOMAIN.COM’, protocol: ‘http’ }
config.action_mailer.perform_deliveries = true
config.action_mailer.default :charset => “utf-8”
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: ‘smtp.gmail.com’,
port: 587,
domain: ‘$YOUR_DOMAIN.COM’,
user_name: Rails.application.credentials.smtp[:SMTP_USERNAME],
password: Rails.application.credentials.smtp[:SMTP_PASSWORD],
authentication: :plain,
enable_starttls_auto: true
}

You’ll need to add the domain to the SMTP settings. This domain should also be the one your app is hosted at, and it also must be a trusted domain in your Google Workspace settings. Adding this line lets your Workspace know that the requests coming from your domain are trustworthy.

Devise

If your Rails app uses Devise for authentication, your user mailer may be handled by a Devise mailer class. You’ll need to add or edit the following line in your ‘config/initializers/devise.rb’ config file:

config.mailer_sender = ‘Your_App_Name <youremail@email.com>’

This ensures that the correct name and email will be shown in any mailers handled by Devise.

That’s it, happy mailing.

--

--

Allene Norton

Full stack developer and Flatiron graduate who recently made the jump from a career as a professional musician and audio engineer | Austin, TX