Archive for the ‘Ruby ON Rails (Skid Software)’ Category

How to send mail with Action Mailer in Ruby on Rails

Action Mailer is the Rails component that enables applications to send and receive e-mail.

Create a New Project emails.

D:\ruby\> rails emails
this requires framework i suggest you to use Instant Rails 2.0 framework.
just by writting this command all controllers, views, models & helpers etc Automatically gets created.

Action Mailer – ConfigurationFollowing

Go into config folder of your emails project and open environment.rb file and add the following lines at the bottom of this file

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {:address => “”,
:port => 25,
:domain => “”,
:authentication => :login,
:user_name => “username”,
:password => “password”,



Remember you should have account on Ruby On Rails Server.
Replace each hash value with proper settings for your Simple Mail Transfer Protocol (SMTP) server. You don’t need to change port number 25 and authentication type if you are using standard SMTP server

Now the Next step is to create mailer

Generate a mailer
D:\ruby\> cd emails
D:\ruby\emails> ruby script/generate mailer Emailer

This will create a file emailer.rb in app\models directory.Check the content of this file is as follows
class Emailer < ActionMailer::Base
Create One Method as follows:-
class Emailer < ActionMailer::Base
def contact(recipient, subject, message, sent_at =
@subject = subject
@recipients = recipient
@from = ‘’
@sent_on = sent_at
@body[“title”] = ‘This is title’
@body[“email”] = ‘’
@body[“message”] = message

The contact method has four parameters a recipient, subject, message and a sent_at, which defines when the e-mail is sent.
@subject defines the e-mail subject.
@body is a Ruby hash that contains values with which you can populate the mail template. You created three key-value pairs: title, email, and message
@recipients is a list of the people to whom the message is being sent.
@from defines who the e-mail is from.
@sent_on takes the sent_at parameter and sets the timestamp of the e-mail

Now we will create a mailer template which is just text with standard Rails <%= %> placeholders scattered throughout

Put following code in app/views/contact.html.erb
(if you are using Instant Rails 2.0 or more)

or put the following code in app/views/contact.rhtml (if you are using Instant Rails version 1.7)

<%= @message %>

Now we had to create a controller for this application as follows
D:\ruby\emails> ruby script/generate controller Emailer

class EmailerController < ApplicationController
def sendmail
email = @params[“email”]
recipient = email[“recipient”]
subject = email[“subject”]
message = email[“message”]
Emailer.deliver_contact(recipient, subject, message)
return if request.xhr?
render :text => ‘Message sent successfully’

To deliver e-mail using the mailer’s contact method, you have to add deliver_ to the beginning of the method name. You add a return if request.xhr? so that you can escape to Rails Java Script (RJS) if the browser does not support JavaScript and then tell the method to render a text message.

Now The Task Is Almost done Except for creating the view part for the user.

Now let’s define our view in app\views\emails\index.html.erb
<head><title>Send Mail</title></head>
<h1>Send Email</h1>
<% form_tag ({:action => “sendmail”},{ :method=>”post”}) do-%>
<label for=”email_subject”>Subject</label>:
<%= text_field ’email’, ‘subject’ %>
<label for=”email_recipient”>Recipient</label>:
<%= text_field ’email’, ‘recipient’ %>
</p> <p><label for=”email_message”>Message</label><br/>
<%= text_area ’email’, ‘message’ %></p>
<%= submit_tag “Send” %>
<% end-%></body>
This will send your message and will display text message that “Message sent successfully”
The modified version of this Application is working in

Queries Are Welcomed