Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page
Looking for more inspiration?Visit the

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(link takes you to an external page).


Set up ActionMailer

set-up-actionmailer page anchor

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

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.

1
class UserNotifierMailer < ApplicationMailer
2
default :from => 'any_from_address@example.com'
3
4
# send a signup email to the user, pass in the user object that contains the user's email address
5
def send_signup_email(user)
6
@user = user
7
mail( :to => @user.email,
8
:subject => 'Thanks for signing up for our amazing app' )
9
end
10
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:

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

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

1
rails generate scaffold user name email login
2
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.

1
class UsersController < ApplicationController
2
def create
3
# Create the user from params
4
@user = User.new(user_params)
5
if @user.save
6
# Deliver the signup email
7
UserNotifierMailer.send_signup_email(@user).deliver
8
redirect_to(@user, :notice => 'User created')
9
else
10
render :action => 'new'
11
end
12
end
13
14
private
15
16
def user_params
17
params.require(:user).permit(:name, :email, :login)
18
end
19
end

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


Configure ActionMailer to use SendGrid

configure-actionmailer-to-use-sendgrid page anchor

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

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

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

(error)

Danger

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(link takes you to an external page). And for Rails Versions 5.2+ see Securely storing custom credentials in Rails.(link takes you to an external page)