Rate this page:

Ruby on Rails

This example shows how to send an email for user signups. You can also checkout this gem for more advanced features.

Setup ActionMailer

Let's generate a Mailer class. Mailer classes function as our controllers for email views.

$ rails generate mailer UserNotifierMailer

Now we open up the mailer we've just generated, app/mailers/user_notifier_mailer.rb and add a mailer action that sends users a signup email.

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' )

Now we need a view that corresponds to our action and outputs HTML for our email. Create a file app/views/user_notifier_mailer/send_signup_email.html.erb as follows:

<!DOCTYPE html>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
    <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>

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

$ rails generate scaffold user name email login
$ rake db:migrate

Now 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.

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


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

Alright, now we're cooking! Let's get it all going through SendGrid.

Configure ActionMailer to Use SendGrid

In config/environment.rb specify your ActionMailer settings to point to SendGrid's servers.

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 => 'yourdomain.com',
  :address => 'smtp.sendgrid.net',
  :port => 587,
  :authentication => :plain,
  :enable_starttls_auto => true

That's it! When a new user object is saved, an email will be sent to the user via SendGrid.

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. And for Rails Versions 5.2+ see Securely storing custom credentials in Rails.

Rate this page:

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the SendGrid tag on Stack Overflow.


        Thank you for your feedback!

        We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

        Sending your feedback...
        🎉 Thank you for your feedback!
        Something went wrong. Please try again.

        Thanks for your feedback!

        Refer us and get $10 in 3 simple steps!

        Step 1

        Get link

        Get a free personal referral link here

        Step 2

        Give $10

        Your user signs up and upgrade using link

        Step 3

        Get $10

        1,250 free SMSes
        OR 1,000 free voice mins
        OR 12,000 chats
        OR more