Skip to content

Instantly share code, notes, and snippets.

@wrburgess
Created November 3, 2011 21:49
Show Gist options
  • Save wrburgess/1337891 to your computer and use it in GitHub Desktop.
Save wrburgess/1337891 to your computer and use it in GitHub Desktop.
Rails Contact Form w/ Gmail SMTP
# Majority of this solution was written and posted here:
# http://matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3
# http://railscasts.com/episodes/206-action-mailer-in-rails-3
# config/initializers/smtp_settings.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "example.com",
:user_name => "[email protected]",
:password => "secret",
:authentication => "plain",
:enable_starttls_auto => true
}
#create a message class in file app/models/message.rb
class Message
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name, :email, :subject, :body
validates :name, :email, :subject, :body, :presence => true
validates :email, :format => { :with => %r{.+@.+\..+} }, :allow_blank => true
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
# run rails g mailer ContactMailer
# creates file called app/mailers/contact_mailer.rb
class ContactMailer< ActionMailer::Base
default :from => "[email protected]"
default :to => "[email protected]"
def new_message(message)
@message = message
mail(:subject => "#{message.subject}")
end
end
# create the file app/views/notifications_mailer/new_message.text.erb
Name: <%= @message.name %>
Email: <%= @message.email %>
Subject: <%= @message.subject %>
Body: <%= @message.body %>
# Run rails g controller contact and then open app/controllers/contact_controller.rb
class ContactController < ApplicationController
def new
@message = Message.new
end
def create
@message = Message.new(params[:message])
if @message.valid?
NotificationsMailer.new_message(@message).deliver
redirect_to(root_path, :notice => "Message was successfully sent.")
else
flash.now.alert = "Please fill all fields."
render :new
end
end
end
# To get these actions working, open up config/routes.rb and insert the following two lines
match 'contact' => 'contact#new', :as => 'contact', :via => :get
match 'contact' => 'contact#create', :as => 'contact', :via => :post
# create app/views/contact/new.html.erb
<%= form_for @message, :url => contact_path do |form| %>
<fieldset class="fields">
<div class="field">
<%= form.label :name %>
<%= form.text_field :name %>
</div>
<div class="field">
<%= form.label :email %>
<%= form.text_field :email %>
</div>
<div class="field">
<%= form.label :subject %>
<%= form.text_field :subject %>
</div>
<div class="field">
<%= form.label :body %>
<%= form.text_area :body %>
</div>
</fieldset>
<fieldset class="actions">
<%= form.submit "Send" %>
</fieldset>
<% end %>
@troystarwalt
Copy link

Thanks for this!

@ccarvalho-eng
Copy link

define your routes like this:
get 'contact' => 'contact#new'
post 'contact' => 'contact#create'

@limjinyung
Copy link

Screenshot from 2020-01-14 12-12-36

Sorry, I've like to ask why I am having this error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment