How to Send Emails with Action Mailer and SendGrid in Rails 5

This article focuses on sending email in production and assumes that you’ve worked with ActionMailer previously. If you’d like a full article on using ActionMailer for development, let me know in the comments!

💌 What is ActionMailer?

ActionMailer is a core part of Rails. It’s the easiest way to send email through your Rails application (RIP ar_mailer). Think of it as a separate set of controllers and views that instead of rendering HTML on a page through an HTTP request, render an email template that is sent via SMTP to an email address.

💌 Why SendGrid?

I like SendGrid, because unlike PostMark, or many other similar services, you can sign up with a regular Gmail email account. It doesn’t require you to already have a domain with an email (which is great if you are still testing, don’t have MX servers set up yet, or if you are using a 3rd party service like Heroku).

I also like that SendGrid doesn’t make you install complicated gems or Heroku add-ons. It uses SMTP for its sending protocol and it simply just works.

🎉🎉🎉 for SendGrid. (not sponsored)

How to send email with SendGrid in production

Step 1: sign up for SendGrid account

Create an account on SendGrid. You can send up to 100 emails a day on free plan and 100,000/month will only cost you $14.95. Not too bad!

Step 2: setup your Rails 5 production environment

The first step is setting the delivery method in your Rails app as SMTP. This will ensure emails are actually send to production. The second step is to put your application’s default URL, otherwise any links back to the site that you add inside of your emails won’t work.

#config/environments/production.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { host: "http://TODO_PUT_YOUR_DOMAIN_HERE" }

Step 3. Add your email credentials in an SMTP initializer file.

This step is important. DO NOT, I repeat, DO NOT, leave your credentials exposed inside of your rails application. Protect them with a gem like dot-env rails because if you do not and you expose credentials to GitHub, bad things can happen (true story: I now someone who had to fight a 10,000$ charge after leaving their AWS S3 credentials on GitHub).

The SMTP initializer includes the address and port used to connect to SendGrid as well as your credentials. There are two ways to connect to SendGrid. The easy way is to just connect your username and password, but if you want to level it up a notch, instead of these two credentials, you can create a SendGrid API key and set the credentials with the key instead.

Create the smtp.rb file in the initializers folder if it doesn’t already exist.

# config/initializers/smtp.rb
ActionMailer::Base.smtp_settings = {
address: 'smtp.sendgrid.net',
port: 587,
domain: 'yourdomain.com',
user_name: ENV['SENDGRID_USERNAME'],
password: ENV['SENDGRID_PASSWORD'],
authentication: :login,
enable_starttls_auto: true
}

If you are using the dot-env rails gem, make sure to add your credentials to the environment file.

#.env
SENDGRID_USERNAME=your_user
SENDGRID_PASSWORD=your_password

Step 4. Send your credentials to your production environment.

If you are using Heroku to deploy, don’t forget to set the credentials in your terminal, so they’re visible on your production environment

heroku config:get SENDGRID_USERNAME
heroku config:get SENDGRID_PASSWORD

💌 Step 5. You’re now ready to send email!

Write that first email and watch SendGrid do the rest.