Posted
21 Jul 2006 @ 21:02

Categories
Tutorial, Ruby on Rails

Comments
1 Comment

Author
Alex

Bookmark
Delicious, Digg, reddit, StumbleUpon

An Email Form with Ruby on Rails

The one thing that I’ve been struggling with the most during my Ruby on Rails learning curve is how to create a simple email form for any web applications I may want to create. There was nothing to be found on Google so in my initial attempt I worked through the ActionMailer documentation on the RoR wiki website. I put it all together and, feeling really pleased with myself, clicked the send button. Nothing at all, just lists of errors, so I modified it and still more errors. After posting on Ruby Forum and with the help of a talented member I finally got it working.

This post is for all the people like me who don’t know what to do.

First of all, this tutorial assumes that you already have a basic RoR web application set up with a ‘Contact’ controller and a main view for the index in the controller.

Now the real work starts. Open up a terminal session (or SSH) and navigate to your rails application’s directory (for this tutorial the rail application will be ‘rails_app’).

localhost:~ User$ cd /rails_app

Then create the mailer, ‘Emailer’ is the name of the model that will be produced.

localhost:~ User$ ./script/generate mailer Emailer

Now, that’s all the terminal work done. Lets start the setup.

In your chosen editor open the file ‘/rails_app/config/environment.rb’ and place the following at the very end of the file customizing all the necessaries to your SMTP server’s configuration.

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.server_settings = {
    :address        => 'smtp.website.co.uk',
    :port           => 25,
    :authentication => :login,    # Don't change this one.
    :user_name      => "smtp_username",
    :password       => "smtp_password"
}
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"

Now open the file ‘/rails_app/app/models/emailer.rb’ and make it look like the below, again customizing the necessaries (but do not touch the variables).

class Emailer < ActionMailer::Base
    def contact_email(email_params, sent_at = Time.now)
        # You only need to customize @recipients.
        @recipients = "contact@website.co.uk"
        @from = email_params[:name] + " <" + email_params[:address] + ">"
        @subject = email_params[:subject]
        @sent_on = sent_at
        @body["email_body"] = email_params[:body]
        @body["email_name"] = email_params[:name]
    end
end

In the controller for your contact page you will need to make the following changes, basically, adding a definition for ‘send_mail’.

def send_mail
    Emailer::deliver_contact_email(params[:email])
end

Go to the ‘/rails_app/views/emailer’ folder and create a file called ‘contact_email.rhtml’. In this file place the following code.

Name:

<%= @email_name %>

Message:

<%= @email_body %>

The final step is to create a form on the ‘Contact’ page that will allow the website to user to input and submit the email. An example for is illustrated below.

<%= start_form_tag :action=> "send_mail" %>
<table>
    <tr>
        <td><label for="email_name">Name:</label></td>
        <td><%= text_field "email", "name", :size => 30 %></td>
    </tr>
    <tr>
        <td><label for="email_address">Email Address:</label></td>
        <td><%= text_field "email", "address", :size => 30 %></td>
    </tr>
    <tr>
        <td><label for="email_subject">Subject:</label></td>
        <td><%= text_field "email", "subject", :size => 30 %></td>
    </tr>
    <tr>
        <td><label for="email_body">Body:</label></td>
        <td><%= text_area "email", "body", :rows => 8, :cols => 30 %></td>
    </tr>
</table>

<input type="submit" value="Send Email" class="primary" />
<%= end_form_tag %>

That’s about it really, you may want to add your own error handling as this example only covers the basics. If you go to the contact page you can see a working example of the above tutorial in action. There are probably easier ways of doing this than the method I have described, but I like my method and hopefully it will work for you.

Update: The code in this post was updated for Rails 2.0.x on 17th March 2008.

Comments

walter Lockhart

walter Lockhart - 31 Jul 2008 @ 13:08

Thanks Alex for your article. Just one question. How would I add contact form input field validation to this example? Thanks in advance. Regards. Walter.

Add A Comment




(Used For Validation Purposes Only)




(No HTML, Textile Markup Only)