Send Emails with SendGrid: A Step-by-Step Guide for Ruby on Rails Applications

Send Emails with SendGrid: A Step-by-Step Guide for Ruby on Rails Applications

Sending emails from a Ruby on Rails application can be a critical aspect of many web applications. Whether it’s for sending password resets, notifications, newsletters or transactional emails, you want your emails to be reliable and fast. That’s where SendGrid comes in. This article guides you through the process of sending emails from your Ruby on Rails application using SendGrid.

Before you start, it’s crucial to understand that username and password authentication for email delivery is no longer sufficient. Nowadays, email provider services like SendGrid require more rigorous verification processes to ensure secure and reliable email delivery. As such, the first step in this journey is setting up your SendGrid account properly.

SendGrid Account Setup

To get started with SendGrid in your Ruby on Rails application, you first need to sign up for a SendGrid account and obtain an API key. Once you have created it, make sure to copy it somewhere. Otherwise, you won’t be able to read it later.

It’s recommended to have completed the Domain Authentication process to ensure the deliverability of your emails. This step requires registering your domain, either your own or one provided by other services such as Heroku that generate it automatically. By authenticating your domain, you can verify that you are a legitimate sender, improve email deliverability, and reduce the risk of your emails being flagged as spam.

When using SendGrid to send emails from your Ruby on Rails application, it’s essential to verify the email address from which the emails will be sent. This verification process involves adding the email address to your SendGrid account and proving that you have access to it. Failure to verify the email address can result in errors when attempting to send emails, such as authentication failures or email delivery issues. Therefore, it’s crucial to verify any email addresses you plan to use for sending emails to ensure reliable and error-free email delivery.

After these steps completed, you should have:

  1. The API key (a long abrakadabra string) like this: SG.ve9OolaiLeenoh0iatoon0.hei8geiz1Shoorohtai6choopah8cahjae9vako8uBa
  2. Verified domain, let it by mydomain.com
  3. Verified sender email address, let it be [email protected]

Configure server

The Rails application will be configured via environment variables, which should be defined on your server (or on your local machine if you want to send emails from it):

SMTP_DEFAULT_FROM:        <your sender email>
SMTP_DOMAIN:              <your domain>
SMTP_PASSWORD:            <your api key>
SMTP_LOGIN:               apikey
SMTP_PORT:                587
SMTP_SERVER:              smtp.sendgrid.net

Note that some of the values are static, while others are placeholders that you should replace with your own values. SMTP_LOGIN, SMTP_PORT, and SMTP_SERVER are static and will always have the same values. It is important to note that SMTP_LOGIN will always equal apikey, and you will not have your own value for that.

Here is an example of how to do that using the Heroku command line:

heroku config:set SMTP_DEFAULT_FROM="Hello <[email protected]>" SMTP_DOMAIN=mydomain.com SMTP_LOGIN=apikey SMTP_PASSWORD=SG.ve9OolaiLeenoh0iatoon0.hei8geiz1Shoorohtai6choopah8cahjae9vako8uBa SMTP_PORT=587

Configure Ruby On Rails

Emails are typically sent from a production server. Therefore, you need to add the necessary configuration to the config/environments/production.rb file as follows:

  config.action_mailer.smtp_settings = {
    port: ENV.fetch('SMTP_PORT'),
    address: ENV.fetch('SMTP_SERVER'),
    user_name: ENV.fetch('SMTP_LOGIN'),
    password: ENV.fetch('SMTP_PASSWORD'),
    domain: ENV.fetch('SMTP_DOMAIN'),
    authentication: :plain,
    enable_starttls_auto: true
  }
  config.action_mailer.delivery_method = :smtp

Make sure that these settings are not overridden elsewhere in this file.

If you need to send emails from development or staging mode, add these lines to the respective files config/environments/development.rb or config/environments/staging.rb. Deliver these changes to your server.

Testing

That’s all the steps needed to get it done. But before moving on to your next task, let’s make sure everything is working correctly. Open the Rails console and define a variable with the email address you want to send a verification email to (for this example, we’ll use [email protected]; replace it with your own):

to_email = '[email protected]'

And then paste there the following code snippet:

mailer = ActionMailer::Base.new
mailer.mail(from: ENV['SMTP_DEFAULT_FROM'], to: to_email, subject: 'Test from WideFix guide', body: "Hello, you've got mail!").deliver

Wait a bit and you should receive the email in your inbox.

If you follow these steps correctly, all mailers defined in your Rails application will start using SendGrid for sending emails, and no additional configuration or checks are required.

Are you seeking assistance with Ruby on Rails development?

Read also