Sending emails using gmail in Rails

Hi…to all today i learned how to send emails using gmail in rails

here are the steps

Create application using

$ rails new mailit

change the directory using      $ cd   /mailit

next create the scaffolder name User with name and email

$ rails g scaffold user name:string email:string

then run the database migrations

$ rake db:migrate

next we have to do configuration settings for gmail

create a file in  config/initializers named setup_mail.rb  

ActionMailer::Base.smtp_settings = {
:address => “”,
:port => 587,
:domain => “”,
:user_name => “”,
:password => “yourpassword”,
:authentication => “plain”,
:enable_starttls_auto => true

now we completed the configuration ,now generate the mailer using

$ rails g mailer user_mailer

this will create the user_mail.rb in  /apps/mailer

now add the following code in to that file

  1. class UserMailer < ActionMailer::Base  
  2.   default :from => “”  
  4.   def registration_confirmation(user)  
  5.     mail(:to =>, :subject => “Registered” , :body =>”Thanks for registering”)  
  6.   end  
  7. end

create textfile named regisetration_confirm.text/erb in apps/views/user_mailer

Thanks for registering!!!

Add the following line to the  /app/controllers/users_controller.rb


def create
@user =[:user])

respond_to do |format|
UserMailer.registration_confirmation(@user).deliver # added line 
format.html { redirect_to @user, :notice => ‘User was successfully created.’ }
format.json { render :json => @user, :status => :created, :location => @user }
format.html { render :action => “new” }
format.json { render :json => @user.errors, :status => :unprocessable_entity }

Thats it rin the server by $ rails server 

then give name and emailid to which you have to send email..

then click create button ,now you will see like this

now go to your mail and check your inbox

Thanks to all…happy times!!!!

Author: Balaji

Hi..My name is Balaji and i am working as a Senior Software Developer in India. I am interested in Shell scripts, python, erlang , linux kernel .

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: