Easiest way to send EMail via Rails : Action mailer

2 Min. Read
Sep 29, 2014

 Rails Ruby JS Action Mailer Email

Action Mailer

Action Mailer allows you to send emails from your application using mailer classes and views.

Mailers are similar to controllers They inherit from ActionMailer::Base and live in app/mailers. Mailers also work very similarly to controllers. Some examples of similarities are enumerated below. Mailers have:

  • Actions, and also, associated views that appear in app/views.
  • Instance variables that are accessible in views.
  • The ability to utilise layouts and partials.
  • The ability to access a params hash.

How?

It’s very easy to send Email via rails 3 and above. You just need to write few lines of codes and you are done. There are two ways you want to send email

  1. You can just send email via any Email address you wish, eg: someone@example.com
  2. Next is, you can send email via email servers of any services you are registered with, such as Google Mail, Outlook, sendgrid etc

The First way: Sending Email from anonymous Email Address

Create a file names setup_mail.rb in config/initializers/ directory and write the following code

1
2
3
4
5
6
7
8
9
10
#config/initializers/setup_mail.rb

ActionMailer::Base.smtp_settings = {
 :address => "smtp.ntc.net.np",
 :port => 25
 }

#smtp.ntc.net.np is the smtp address of my ISP, see this for your ISP in your ISP's website

#25 is default port number for SMTP, see if it is different for your's ISP

Then generate a mailer ( Mailer is just like controller). Write following code to generate mailer

1
2
3
rails generate mailer mailer_name

eg: rails g mailer user_mailer

This will create a file names user_mailer.rb in app/mailers directory and user_mailer directory in app/views directory

replace the codes in the file with following

1
2
3
4
5
6
7
8
9
#app/mailers/user_mailer.rb

class UserMailer < ActionMailer::Base
  defregistration\_confirmation(user)
    mail(:to => 'destination@example.com', :subject => "Newly Registered", :from => "sourceemail@example.com")
  end
end

#'destination@example.com' can be replace with user.email if you have sentUserobject via method call

Like controllers, mailers also have views associated with it in views directory. No file is deafulty created in views directory, you just need to create one. Create a file named registration_confirmation.html.erb in user_mailer directory and write text you want your user to see in email. Like

1
2
<h1>Thank you dear for registering</h1>  
<p>The new one</p>

You can also embed ruby code as in other views. You can also render these views to see Email Preview.

Now you need to trigger the rails to send the Email. You can do this by invoking the action you created in mailer. In this  case

1
UserMailer.registration_confirmation(@user).deliver

Example

1
2
3
4
5
6
7
8
9
#app/controllers/user\_controller.rb

def create  
    @user = User.new(params[:user])

    if @user.save  
      UserMailer.registration\_confirmation(@user).deliver  
    end  
end

To send Email via Google, or Microsoft Mail servers, you need to change settings in setup_mail.rb file

1
2
3
4
5
6
7
8
9
10
11
ActionMailer::Base.delivery_method = :smtp

ActionMailer::Base.smtp_settings = {
  :address => "smtp.gmail.com",
  :port => "587",
  :domain => "gmail.com",
  :user_name => "xxx@gmail.com",
  :password => "yyy",
  :authentication => "plain",
  :enable_starttls_auto => true
}

For Outlook just change the attributes according to the vendor

You might also like

It’s very easy to send Email via rails 3 and above. You just need to write few lines of codes and you are done. There are two ways you want to send email You can just send email via any Email address you wish, eg: someone@example.com Next is, you can send email via email servers […]


Read More..


Write To Me

Hire me shiva bhusal
We'll never share your email with anyone else.
I'll never share your email with anyone else.