Sunday, July 1, 2012

Revised: Sending mail through ruby on rails

I have previously added post Sending mail through ruby on rails for adding mail configuration to Ruby on Rails application. But as we know in rails 3 there are separate mailers and we dont have to add mailers in models folder any more.
So I though just to add revised code.

First I created a setup_mail.rb in config/initializers and added below code which is for my heroku app (sendgrid)
 ActionMailer::Base.delivery_method = :smtp
 ActionMailer::Base.smtp_settings = {
  :user_name => "send grid user name",
  :password => "sendgrid password",
  :domain => "your domain name",
  :address => "",
  :port => 25,
  :authentication => :plain,
  :enable_starttls_auto => true
Now just created user_mailer.rb in app/mailers and added a below code
  File Name : user_mailer.rb (UserMailer)
  Description : File added to hold mailer template actions for users.
  Date : 13/04/2012
  Dependencies : ActionMailer::Base
  Note : If file not in use please remove the method calls from the other controllers. Information for other controllers and actions is given above the respective actions.

class UserMailer < ActionMailer::Base
  default :from => ""
  # registration_confirmation(new_user_information) : 13/04/2012
  # This action is called from users_controller.rb in create action after successful user registration.
  # (Autoresponder Email on new Registration)Task
  def registration_confirmation(user)
    @user = user
    mail(:to =>, :subject => "Registered with Test App") do |format|
Well, lots of comments. But I prefer to keep those for other programmers to know where the code is used or for what purpose. Thats really useful for me too to find the code quickly and reminding what the action does.

After setup files done, added 1 single line as below, in users_controller.rb in create action after the user is successfully stored.
And at last created a user_mailer folder in app/views. Again in that created registration_confirmation.html.erb
  Hi <%= %>, 
  < br />< br />
  == Some Demo Text ==
  < br />< br />
  Thanks For Signing Up ==
  Test App Team
Thats it. We are done with mailer configuration and now just restart your server. For more information go through Action Mailer Basics from Guides Ruby On Rails.

No comments:

Post a Comment