Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Ruby on Rails


This example shows how to send an email for user signups. You can also checkout this gem(link takes you to an external page) for more advanced features.


Setup ActionMailer

setup-actionmailer page anchor

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


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


_10
class UserNotifierMailer < ApplicationMailer
_10
default :from => 'any_from_address@example.com'
_10
_10
# send a signup email to the user, pass in the user object that contains the user's email address
_10
def send_signup_email(user)
_10
@user = user
_10
mail( :to => @user.email,
_10
:subject => 'Thanks for signing up for our amazing app' )
_10
end
_10
end

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:


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

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


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


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

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


Configure ActionMailer to Use SendGrid

configure-actionmailer-to-use-sendgrid page anchor

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


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

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

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


Rate this page: