Easiest way to send EMail via Rails : Action mailer

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

#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

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

#app/mailers/user_mailer.rb

class UserMailer < ActionMailer::Base
  def registration_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 sent User object 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

<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

UserMailer.registration_confirmation(@user).deliver

Example

#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

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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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