---
"@context": https://schema.org
"@type": TechArticle
"@id": https://www.twilio.com/docs/sendgrid/for-developers/sending-email/rubyonrails#article
headline: Ruby on Rails
description: View instructions on how to easily send email with Ruby on Rails using SendGrid, by setting up setting up ActionMailer or using a gem.
url: https://www.twilio.com/docs/sendgrid/for-developers/sending-email/rubyonrails
inLanguage: en
dateModified: 2026-07-17T03:36:59.000Z
author:
  "@type": Organization
  name: Twilio Developer Education Team
publisher:
  "@type": Organization
  name: Twilio
---

# Ruby on Rails

This example shows how to send an email for user signups with Ruby on Rails and SendGrid. For more advanced features, see the [SendGrid Ruby gem](https://github.com/sendgrid/sendgrid-ruby).

## Set up ActionMailer

To send email through ActionMailer, first generate a Mailer class. Mailer classes function as the controllers for email views.

```bash
rails generate mailer UserNotifierMailer
```

Open the mailer you just generated, `app/mailers/user_notifier_mailer.rb`, and add a mailer action that sends users a signup email.

```ruby
class UserNotifierMailer < ApplicationMailer
  default :from => 'any_from_address@example.com'

  # send a signup email to the user, pass in the user object that   contains the user's email address
  def send_signup_email(user)
    @user = user
    mail( :to => @user.email,
    :subject => 'Thanks for signing up for our amazing app' )
  end
end
```

Next, create a view that corresponds to the action and outputs HTML for the email. Create a file `app/views/user_notifier_mailer/send_signup_email.html.erb` as follows:

```html
<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>Thanks for signing up, <%= @user.name %>!</h1>
    <p>Thanks for joining and have a great day! Now sign in and do
awesome things!</p>
  </body>
</html>
```

If you don't have a user model quite yet, generate one quickly.

```bash
rails generate scaffold user name email login
rake db:migrate
```

In the controller for the user model, `app/controllers/users_controller.rb`, add a call to `UserNotifierMailer.send_signup_email` when a user is saved.

```ruby
class UsersController < ApplicationController
  def create
    # Create the user from params
    @user = User.new(user_params)
    if @user.save
      # Deliver the signup email
      UserNotifierMailer.send_signup_email(@user).deliver
      redirect_to(@user, :notice => 'User created')
    else
      render :action => 'new'
    end
  end

  private

  def user_params
    params.require(:user).permit(:name, :email, :login)
  end
end
```

With the mailer in place, configure it to send through SendGrid.

## Configure ActionMailer to use SendGrid

To route email through SendGrid, specify your ActionMailer settings in `config/environment.rb` to point to SendGrid's servers.

```ruby
ActionMailer:\:Base.smtp_settings = {
  :user_name => 'apikey', # This is the string literal 'apikey', NOT the ID of your API key
  :password => '<SENDGRID_API_KEY>', # This is the secret sendgrid API key which was issued during API key creation
  :domain => 'example.com',
  :address => 'smtp.sendgrid.net',
  :port => 587,
  :authentication => :plain,
  :enable_starttls_auto => true
}
```

When a new user object is saved, SendGrid sends an email to the user.

> \[!CAUTION]
>
> As a best practice, you should not store your credentials directly in the source but should instead store them in configuration files or environment variables. See this tutorial on [environment variables in Rails](https://railsapps.github.io/rails-environment-variables.html). And for Rails Versions 5.2+ see [Securely storing custom credentials in Rails.](https://guides.rubyonrails.org/security.html#custom-credentials)
