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.

<div class="code-wrapper" id="wrapper_5dcef33143a20f01715a6aabf9f14805">
  <div class="code-buttons">
    <ul class="nav nav-tabs">
      <li class="pull-right">
        <a class="copycode code-button btn-mini" id="copy_5dcef33143a20f01715a6aabf9f14805">
          <i class="icon-copy"></i> Copy
        </a>
      </li>
      <li class="pull-right">
        <a class="expandcode code-button btn-mini" id="expand_5dcef33143a20f01715a6aabf9f14805">
          <i class="icon-fullscreen"></i> Fullscreen
        </a>
      </li>
    </ul>
  </div>
  <div class='bogus-wrapper'><notextile><figure class="code" id="code_5dcef33143a20f01715a6aabf9f14805">

1
$ rails generate mailer Notifier

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

<div class="code-wrapper" id="wrapper_bc6dfb8d0742265a3ed09a6b15b5d53f">
  <div class="code-buttons">
    <ul class="nav nav-tabs">
      <li class="pull-right">
        <a class="copycode code-button btn-mini" id="copy_bc6dfb8d0742265a3ed09a6b15b5d53f">
          <i class="icon-copy"></i> Copy
        </a>
      </li>
      <li class="pull-right">
        <a class="expandcode code-button btn-mini" id="expand_bc6dfb8d0742265a3ed09a6b15b5d53f">
          <i class="icon-fullscreen"></i> Fullscreen
        </a>
      </li>
    </ul>
  </div>
  <div class='bogus-wrapper'><notextile><figure class="code" id="code_bc6dfb8d0742265a3ed09a6b15b5d53f">

1
2
3
4
5
6
7
8
9
10
class Notifier < ActionMailer::Base
  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

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

<div class="code-wrapper" id="wrapper_6dcde77ed8249488eeb30c3e4b30e437">
  <div class="code-buttons">
    <ul class="nav nav-tabs">
      <li class="pull-right">
        <a class="copycode code-button btn-mini" id="copy_6dcde77ed8249488eeb30c3e4b30e437">
          <i class="icon-copy"></i> Copy
        </a>
      </li>
      <li class="pull-right">
        <a class="expandcode code-button btn-mini" id="expand_6dcde77ed8249488eeb30c3e4b30e437">
          <i class="icon-fullscreen"></i> Fullscreen
        </a>
      </li>
    </ul>
  </div>
  <div class='bogus-wrapper'><notextile><figure class="code" id="code_6dcde77ed8249488eeb30c3e4b30e437">

1
2
3
4
5
6
7
8
9
10
11
<!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.

<div class="code-wrapper" id="wrapper_4f880d901930c12b833250d0f1f4c98d">
  <div class="code-buttons">
    <ul class="nav nav-tabs">
      <li class="pull-right">
        <a class="copycode code-button btn-mini" id="copy_4f880d901930c12b833250d0f1f4c98d">
          <i class="icon-copy"></i> Copy
        </a>
      </li>
      <li class="pull-right">
        <a class="expandcode code-button btn-mini" id="expand_4f880d901930c12b833250d0f1f4c98d">
          <i class="icon-fullscreen"></i> Fullscreen
        </a>
      </li>
    </ul>
  </div>
  <div class='bogus-wrapper'><notextile><figure class="code" id="code_4f880d901930c12b833250d0f1f4c98d">

1
2
$ 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 Notifier.send_signup_email when a user is saved.

<div class="code-wrapper" id="wrapper_645a9ccc53a3e72e05ffb8384931faf3">
  <div class="code-buttons">
    <ul class="nav nav-tabs">
      <li class="pull-right">
        <a class="copycode code-button btn-mini" id="copy_645a9ccc53a3e72e05ffb8384931faf3">
          <i class="icon-copy"></i> Copy
        </a>
      </li>
      <li class="pull-right">
        <a class="expandcode code-button btn-mini" id="expand_645a9ccc53a3e72e05ffb8384931faf3">
          <i class="icon-fullscreen"></i> Fullscreen
        </a>
      </li>
    </ul>
  </div>
  <div class='bogus-wrapper'><notextile><figure class="code" id="code_645a9ccc53a3e72e05ffb8384931faf3">

1
2
3
4
5
6
7
8
9
10
11
12
13
class UsersController < ApplicationController
  def create
    # Create the user from params
    @user = User.new(params[:user])
    if @user.save
      # Deliver the signup email
      Notifier.send_signup_email(@user).deliver
      redirect_to(@user, :notice => 'User created')
    else
      render :action => 'new'
    end
  end
end

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.

<div class="code-wrapper" id="wrapper_f4b290ee2b8544b12dbaab6605a3095a">
  <div class="code-buttons">
    <ul class="nav nav-tabs">
      <li class="pull-right">
        <a class="copycode code-button btn-mini" id="copy_f4b290ee2b8544b12dbaab6605a3095a">
          <i class="icon-copy"></i> Copy
        </a>
      </li>
      <li class="pull-right">
        <a class="expandcode code-button btn-mini" id="expand_f4b290ee2b8544b12dbaab6605a3095a">
          <i class="icon-fullscreen"></i> Fullscreen
        </a>
      </li>
    </ul>
  </div>
  <div class='bogus-wrapper'><notextile><figure class="code" id="code_f4b290ee2b8544b12dbaab6605a3095a">

1
2
3
4
5
6
7
8
9
ActionMailer::Base.smtp_settings = {
  :user_name => 'your_sendgrid_username',
  :password => 'your_sendgrid_password',
  :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.
Was this helpful? + Yes - No Feedback