06 November 2011

Action Mailer 2.3.9 — Easy email delivery and testing

Action Mailer allows you to send email from your application using a mailer model and views.

This tutorial is about how to send an email using gmail free service in Ruby on Rails 2.3.9, for versions higher than this refer rails guides .

Creating Demo project

 rails mail_demo –d mysql

Creating a User model

 Ruby script/generate scaffold User name:string email:string content:string

Creating a Mailer model

 Ruby script/generate mailer user_mailer


Setting up the development.rb for configuring application settings to access mail smtp server

In development.rb

config.action_mailer.delivery_method = :smtp
config.action_mailer.raise_delivery_errors = true

config.action_mailer.smtp_settings = {
  :enable_starttls_auto => true,
  :address        => 'smtp.gmail.com',
  :port           => 587,
  :domain         => 'your.domain.com',
  :authentication => :login,
  :content_type   => "text/html",
  :user_name      => your full username example xyz@gmail.com',
  :password       => 'password'

Remember these are the basic settings needed to send mails from gmail ,other server may have different settings .


Creating the mailer

Now we will write the method in user_mailer model to send the mail .Remember in the mailer model

def send_email(user)
   recipients  user.email
   from        "webmaster@example.com"
   subject     "Thank you for Registering"
   part :content_type => "text/html",
    :body => render_message("registration_confirmation", :user => user)

The from field can be your own or your domains email address. Please check if the from for field has invalid email the user will not be able to reply .

Creating Email Template

Now we will create a view which would form a the email template .It is named as same as the method in the model user_mailer ie. Send_email.html.erb

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

<html lang="en">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style type="text/css" media="screen">
      h3 { color: #f00; }
      ul { list-style: none; }
      <h2 id = "banner">Your account signup details are below</h2>

    <ul><br /><br />
      <li><h2>Name: <b><%= @user.name %></b> </h2></li>

      <li>Login: <%= @user.email %></li><br /><br />

      <li>Image: <%= image_tag 'rails.png' %></li><br /><br />

      <li>Content: <%= @user.content %></li>

In the end add a line to the create method of the Users_controller


The method is referred to as deliver_send_email because rails appends it to a mailer method.

Now run the server and try creating a new user . The email is sent to the address the user enters for the new record.


  1. Hi Ashish ,
    Thanks for the information .
    I tried to use action mailer in a rails 3 application.
    Did all that was required .
    But the app was not sending an email on User generation as per requirement.
    You can check the code at :


    If you could provide a solution .


  2. So karan i have a new article for you .. please have a look and revert back

  3. Most ISPs monitor and measure the amount of emails that are coming from your mail server. These ISPs will throttle your email delivery rate back if they detect large amounts of mail being sent from your domain or mail server. It is important to send your mailings in small batches that will not trigger the ISPs to temporarily or permanently block emails from your domain.

    Email Delivery Service


Any inputs are welcomed : ashish.cse2010@gmail.com