breaking into the unknown…

sending email in rails

1 Comment

Sending email in rails is mange by ActionMailer::Base. The detail of this class is available here. I will show you here to send a simple welcome email. You can read it in more detail here at

STEP 1: configuring the mailer in your environment file

First we need to tell our application to handle email request. The basic configuration in your development environment related to action_mailer should look like this

  config.action_mailer.default_url_options = {:host => 'localhost:3000'} 
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true # If set to false, mail will be not delivered in 
  reality, but you can see it in the terminal logs. In development it is false by default, but I 
  suggest to make it true even in development as you can check it directly in your mail box. 
  For testing if you do not want to use a real email id, you can create dummy email 
  config.action_mailer.raise_delivery_errors = true # it will show error while delivering the mail. 
  generally people keep it false in production environment
  config.action_mailer.default :charset => "utf-8"

  config.action_mailer.smtp_settings = {
    address: "",
    port: 587,
    domain: "",
    authentication: "plain",
    enable_starttls_auto: true,
    user_name: "", # this should be a real gmail id
    password: "my_password" # this should be real password of your gmail id

1->If you do not use real gmail id and password in above setting it will throw NET:SMTP authentication error
2->I have used google smtp setting i,e the mail will be delivered by gmail smtp server. There is other smtp service provider also like mandrill . I will discuss there smtp setting in some other post

STEP 2: generating the mailer

As , you generate controller for your application, you can also generate the mailer, which in turn will create some files and folder for you. The mailer class is exactly same as controller, where each action have its associated view and all instance variable in a action is available in the corresponding views also. The only difference is that, you do not write routes for the mailer action

$ rails generate mailer UserMailer # Iam assuming you are using rails3 or above, 
for earlier version use the corresponding command. for example  
ruby/script generate mailer UserMailer for rails2.3.5
create  app/mailers/user_mailer.rb
invoke  erb
create    app/views/user_mailer
invoke  rspec
create    spec/mailers/user_mailer_spec.rb

So, You can see that , it has created user_mailer.rb file and user_mailer folder for you

STEP 3: Defining mailer action

We are assuming that, a user get created in our system with dummy password and we need to send a email to him on his account creation with his username and password. Let we call this mailer action as welcome_email. The code of user_mailer.rb will look like this

class UserMailer < ActionMailer::Base   
  default :from => "" # if from is not specified it will be set as from.
  def welcome_email(user_info)
    email = user_info[:mail]
    @username = user_info[:username]
    @password = user_info[:temp_password]
    @url  = new_user_session_url
    mail(:to => email, :subject => "Your Login credential")

see how we have created the @url object to which user will be redirected from his email. Here, you should use new_user_session_url instead of new_user_session_path. This is because, user email do not have any context to your application so realtive path will not work, you need to specify the absolute path. the url generated by the two is as below

new_user_session_url -> http://localhost:3000/users/sign_in # absoulte path generated with _url suffix. the localhost:3000 coming from your setting in step1
new_user_session_path -> /users/sign_in # relative path generated with _path suffix

STEP 4: generating the mailer view

In the view we will define how our email will look to the user. . Create a erb file with the same name as our mailer action in app/views/user_mailer . So our, mailer view will be welcome_email.erb. The content of this file is simple html as shown below

Welcome to

You have successfully signed up with MyFinance Your login credential is as below :

username: <%= @username %> 
password: <%= @password %> 

To login to the site, just follow this link: <%=link_to @url, @url %> 

Thanks for joining and have a great day!

Note :

The view above is simple text. You can wrap it in html structure as in a normal view file i,e wraped in <head>, <body> etc. .

STEP 5: delivering the email

Now, we come to the last step i,e triggering the email. You can trigger any mailer action anywhere with below syntax

Mailer_class_name.action_name.deliver # This syntax is for rails3 and above

Mailer_class.deliver_action_name # This syntax is for rails version before 3

Example : In our case It will become

UserMailer.welcome_email(@user_info).deliver # It will not throw any exception

UserMailer.welcome_email(@user_info).deliver! # the ! make the deference i,e it will throw exception if anything goes wrong. use this form if want to handle the exception in begin end block. You can read more on exception handling here.

UserMailer.deliver_welcome_email(@user_info) # for rails version older than 3

A sample controller action triggering this email on user creation is as below

class UsersController < ApplicationController 
  def new 
    @user = 

  def create 
    @user = 
    @user = params[:user] 
      user_info = {:mail =>, :username => @user.username, 
      :temp_password => @user.temp_password } # we are creating hash of information 
      which we need to pass to our welcome_email method
      UserMailer.welcome_email(user_info).deliver #this will deliver the mail
     render new
     flash[:notice] = "User failed to create"

Restart your server, create a user. Go to the user email. You can find the welcome message there in the inbox. If not there check the spam folder also. Well….I hope you got the email🙂

Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

One thought on “sending email in rails

  1. Reblogged this on My Corner of the Web and commented:
    Perfect write up for sending emails with RoR

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 )

Google+ photo

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

Connecting to %s