How to Send Email in Rails application

Send Emails with Action Mailer and SendGrid in Ruby on Rails application
Luan Nguyen
Luan Nguyen
Nov 10, 2020 · 4 min read

What'll we learn:

  • Introduction to Action Mailer
  • Create an Action Mailer
  • Action Mailer Preview
  • Configuration
  • Integrate with SendGrid
  • Conclusion

Photo by @campaign_creators on Unsplash

Introduction to Action Mailer

  • Action Mailer is a component in Ruby on Rails that allows you to send emails from your Rails application.
  • Action Mailer works similar to Controller, that means Mailers have:
    • Actions, and associated views that appear in app/views.
    • Instance variables that are accessible in views.
    • The ability to utilize layouts and partials.
    • The ability to access a params hash.

Create an Action Mailer

* Idea: We'll send a welcome email to the subscriber in Blog.

Generate an Action Mailer for Subscriber
rails generate mailer Subscriber
Running via Spring preloader in process 7586
      create  app/mailers/subscriber_mailer.rb
      invoke  erb
      create    app/views/subscriber_mailer
      invoke  test_unit
      create    test/mailers/subscriber_mailer_test.rb
      create    test/mailers/previews/subscriber_mailer_preview.rb

We focus on 2 files:
  • SubscriberMailer: app/mailers/subscriber_mailer.rb
  • SubscriberMailerPreview: test/mailers/previews/subscriber_mailer_preview.rb
SubscriberMailer looks like:
class SubscriberMailer < ApplicationMailer
end

SubscriberMailerPreview:
This is an Action Mailer Preview that support to preview the mails on the browser through a URL to renders them. 
# Preview all emails at http://localhost:3000/rails/mailers/subscriber_mailer
class SubscriberMailerPreview < ActionMailer::Preview
end

That means you can access to http://localhost:3000/rails/mailers/subscriber_mailer to preview emails in SubscriberMailer.


Create a Welcome Email

Step 1: Add a method called welcome_email to SubscriberMailer as below:
class SubscriberMailer < ApplicationMailer
  def welcome_email(subscriber)
    @subscriber = subscriber

    mail(
      from:    'Luan Nguyen <[email protected]>',
      to:      @subscriber.email,
      subject: 'Welcome to FullstackRubyonRails.com'
    )
  end
end


Step 2: Create a View for welcome_email 
Create a file called welcome_email.html.erb in app/views/subscriber_mailer/
This is the content of the email, formatted in HTML like views in Controller:
# app/views/subscriber_mailer/welcome_email.html.erb
<p>Hi <%= @subscriber.name %></p>
<p>Thanks for subscribing to our newsletters!</p>
<br>
<p>Cheers,</p>
<p>Luan Nguyen</p>


Update in Subscriber Mailer Preview

# Preview all emails at http://localhost:3000/rails/mailers/subscriber_mailer
class SubscriberMailerPreview < ActionMailer::Preview
  def welcome_email
    subscriber = Subscriber.find_or_create_by(
      name: "User", 
      email: '[email protected]'
    )
    SubscriberMailer.welcome_email(subscriber)
  end
end

Access to http://localhost:3000/rails/mailers/subscriber_mailer/welcome_email to preview welcome_email email in browser. The email looks like:

Welcome email in SubscriberMailer



Calling the Mailer

If you want to send emails right away just call deliver_now:
SubscriberMailer.welcome_email(subscriber).deliver_now

Or using deliver_later when you want to send emails asynchronously in the background job (Active Job)
SubscriberMailer.welcome_email(subscriber).deliver_later


Action Mailer Configuration

  • In Development environment (config/environments/development.rb)
We use letter_opener gem to test the mailer in development mode.
Letter Opener is a gem help we preview email in the default browser instead of sending it.
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.delivery_method    = :letter_opener
config.action_mailer.perform_deliveries = true

  • In Production environment (config/environments/production.rb)
We integrate with Sendgrid SMTP API to send emails to the users.
config.action_mailer.default_url_options = { host: 'you-domain.com' }
config.action_mailer.delivery_method     = :smtp
config.action_mailer.smtp_settings       = {
  authentication: :plain,
  address:        'smtp.sendgrid.net',
  port:           587,
  user_name:      SENDGRID_NAME_KEY,
  password:       SENDGRID_API_KEY,
  domain:         'you-domain.com'
}


Integrating with Sendgrid SMTP API:

Creating an API key in Sendgrid 
  1. Create an account in Sendgrid.
  2. Navigate to Settings on the left navigation bar, and then select API Keys.
  3. Click Create API Key.
  4. Give your API key a name.
  5. Select Full Access, Restricted Access, or Billing Access.
    • Full Access allows the API key to access GET, PATCH, PUT, DELETE and POST endpoints for all parts of your account, excluding billing and Email Address Validation.
    • Restricted Access customizes levels of access for all parts of your account, excluding billing and Email Address Validation.
    • Billing Access allows the API key to access billing endpoints for the account.
  6. Click Create & View
Create API Key in Sendgrid

Update API key to the configuration of Production
  • user_name: SENDGRID_NAME_KEY 
  • password:  SENDGRID_API_KEY  
Also, you can learn more about the API Keys of Sendgrid in their document.

Conclusion

In this article, I guided about how to send emails from your Rails application through Action Mailer. Also, introduce to you Sendgrid - an email delivery service and how to integrate with Sendgrid API to send emails on Production.