Thoughts on development, design and the world we live in.

Simple Email Form with ActionMailer

By sarah in Uncategorized. Posted on November 17th

Working with ActionMailer is a bit different from working with ActiveRecord. While it makes sense to me that you would have a model for the data of the email message you send, the flow of control seems inverted from the rest of Rails. With ActionMailer call the model from the controller, then the model triggers the view code, rather than rendering the view from the controller with the model data as is typical with Rails. From the ActionMailer docs: “Emails are defined by creating methods within the model which are then used to set variables to be used in the mail template, to change options on the mail, or to add attachments. ” What follows is the steps that I followed to create a webform that sends an email in Rails, which turned out to be fairly simple, but it did take some digging to put the pieces together, so I figured other folks might find them useful.

Set up
This exercise requires Ruby 1.8.7 and Rails 2.2.1 or later to use TLS (required for Gmail SMTP). For older versions of either Ruby or Rails, you can try a plugin like action_mailer_optional_tls. There is a nice screencast introductory tutorial that runs through a subset of the following steps and is definitely worth watching.

To set up the context for this example, imagine we’re creating comment forms for a news website, where on each article page there is a comment form. To simulate this, we’ll just scaffold up some article pages:

In test-driven style (thanks to rubytutorials.net), here’s a spec for we’ll make work (at least the sending mail part of it). Note that “from” doesn’t meet expectations in 2.3.3 and 2.3.4 it is an array, rather than a string, but it has been fixed for a future release.


Mailer Configuraton
Typically the settings would be different per environment and would be specified in config/environment/development.rb, etc. For this exercise, I just modified the main environment.rb file.

in config/environment.rb:

ActionMailer has a generator which creates files for the model and a view file, along with a unit test which is a lot less blank that the ActiveRecord unit tests.

Edit the model, so that it takes a hash as it’s first parameter (we’ll end up getting this from the form).

Controller and Views
Rename the file “feedback.erb” to “feedback.html.text.erb” This will be the template used for the email, formatted in HTML. Had we wanted to send text-only emails, the file would have been called feedback.text.plain.erb. Rails sets the content type of the email to be the one in the filename. Or we could supply both and Rails will generate a “multipart/alternative” message with both template rendered for the html and plain mime parts.

Next we’ll create a form in the article show page and a controller action that will send the email.

in /views/articles/show.html.erb

in /controllers/articles_controller.rb

Now you should have a form that sends email. Yay!

By sarah | Posted in Uncategorized | Tagged | Comments (5)


  1. Posted June 7, 2010 at 9:47 am | Permalink

    Great tips! I will try it definitely
    thanks for sharing this!

  2. Posted January 30, 2011 at 1:55 am | Permalink

    Respect to post author, some good selective information .

  3. Posted February 11, 2011 at 6:37 pm | Permalink

    Excellent write-up, especially the portion on testing. I followed the Railscasts mailer recipe, which did not include tests and was struggling with testing mailer. Thanks!

  4. Posted November 2, 2012 at 1:54 pm | Permalink

    Hello, i read your blog from time to time and i own a similar one and i
    was just wondering if you get a lot of spam feedback?
    If so how do you protect against it, any plugin or anything you can suggest?
    I get so much lately it’s driving me crazy so any help is very much appreciated.

  5. Posted November 5, 2012 at 6:33 am | Permalink

    Great site you have here.. It’s hard to find excellent writing like yours these days. I seriously appreciate people like you! Take care!!

Post a Comment

Your email is never shared. Required fields are marked *