Contact email form with Ruby on Rails and GMail (plus, reply-to error)

UPDATE AUGUST 6TH, 2008: Gmail’s reply-to was failing with the rails action-mailer. It would show the reply-to email as the email from gmail rather than the email address entered by the user. I did the following, and it fixed the reply-to, but now the @from does not work. At least the reply-to works!

sources: rails-send-email-tutorial and Action-mailer reply-to header

def contact_email(email_params, sent_at = Time.now)
    # You only need to customize @recipients.
    @recipients = "scott@scottmotte.com"
    @from = 'Does not work'
    @headers['Reply-To'] = "'#{email_params[:name]}' <#{email_params[:email_address]}>"
    @subject    = 'Contact Form'
    @sent_on    = sent_at
    @body["email_email_address"] = email_params[:email_address]
    @body["email_message"] = email_params[:message]
    @body["email_name"] = email_params[:name]
  end


This assumes you already have a rails app

Create emailer model

script/generate model emailer

Edit app/model/emailer.rb

class Emailer < ActionMailer::Base
    def contact_email(email_params, sent_at = Time.now)
        # You only need to customize @recipients.
        @recipients = "contact@website.com"
        @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

Create a file called lib/smtp_tls.rb and paste this code into it.

Create config/initializers/mail.rb & put the following in there:

#from http://www.prestonlee.com/archives/63
require "smtp_tls"
# from http://www.avnetlabs.com/rails/restful-authentication-with-rails-2
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
    :address => "smtp.gmail.com",
    :port => 587,
    :domain => "gmail.com",
    :authentication => :plain,
    :user_name => "username",
    :password => "password"
}

In controller for your contact form add

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

Create file app/views/emailer/contact_email.html.erb

Name:

<%= @email_name %>

Message:

<%= @email_body %>

Create form

<% form_tag :action => "send_mail" do %>

Name: <%= text_field "email", "name", :size => 30 %>

Email: <%= text_field "email", "address", :size => 30 %>

Subject: <%= text_field "email", "subject", :size => 30 %>

Body: <%= text_area "email", "body", :rows => 8, :cols => 30 %>


<% end %>

Resources:
- useful but out of date

Comments

  1. stephen | November 12, 2008

    Hi, thanks for the nice tutorial, it works perfectly.

    I did things a little differently from yourself because I already have an ActionMailer set up to handle signup emails and activation codes (a la restful_authentication) and I have been using the very same smtp_tls.rb file that you use in your tutorial. Nice little bit of code that. I do appreciate it.

    I put the gmail configuration stuff you mention at the end of my environment.rb file and then I put my own personal gmail details in the file you specify - this way my site can send and receive emails.

    Anyway, this was a good tutorial, it worked right away, cheers.
    I am in the middle of throwing my own site together right now and it only took me 20 minutes to get the contact form working, and that includes the 16 or so minutes spent reading the football scores on bbc.com.

    Thanks again,
    Stephen

  2. stephen | November 12, 2008

    Oh, and I hope you don’t mind me mentioning, just for all the noobs our there who might not know this, but it is a good idea to put an if statement into your controller code:

    def send_mail
    if request.post?
    Emailer::deliver_contact_email(params[:email])
    redirect_back_or_default(’/')
    flash[:notice] = “Message sent, thanks very much etc etc etc”
    end

    Now your users will know if the mail was sent. I needed the redirect_back_or_default(’/') statement in my controller too as I was getting a ‘template missing error’ without it.

    Thanks again

  3. scott | November 12, 2008

    Thanks Stephen for your comments. By the way, great country Scotland, I really enjoyed my brief time there a few years ago.