Permalink
Browse files

User Mailer for contact emails

- Preview with letter_opener on development
  • Loading branch information...
1 parent 34695be commit 92aae555fd52db20418e387f49dceca9ff6f4fd8 @samrayner committed Sep 15, 2013
View
@@ -1,7 +1,6 @@
source 'https://rubygems.org'
gem 'simple_form', github: 'plataformatec/simple_form'
-gem 'mail_form', github: 'plataformatec/mail_form'
gem 'rails', '4.0.0'
gem 'pg'
@@ -15,10 +14,15 @@ gem 'less-rails'
gem 'haml-rails'
group :test, :development do
+ gem 'factory_girl_rails', '~> 4.2'
gem 'simplecov', require: false
gem 'rspec-rails', '~> 2.14'
end
+group :development do
+ gem 'letter_opener'
+end
+
group :test do
gem 'capybara'
gem 'shoulda-matchers'
View
@@ -1,12 +1,4 @@
GIT
- remote: git://github.com/plataformatec/mail_form.git
- revision: 9eb221a9c5e3f6dad631a547c7518fc0d1cde070
- specs:
- mail_form (1.5.0.rc)
- actionmailer (>= 3.2, < 5)
- activemodel (>= 3.2, < 5)
-
-GIT
remote: git://github.com/plataformatec/simple_form.git
revision: f0b95cb9805ebc895947cafaf33a07b67eeb8c71
specs:
@@ -43,6 +35,7 @@ GEM
multi_json (~> 1.3)
thread_safe (~> 0.1)
tzinfo (~> 0.3.37)
+ addressable (2.3.5)
arel (4.0.0)
atomic (1.1.13)
builder (3.1.4)
@@ -80,6 +73,11 @@ GEM
erubis (2.7.0)
eventmachine (1.0.3)
execjs (2.0.1)
+ factory_girl (4.2.0)
+ activesupport (>= 3.0.0)
+ factory_girl_rails (4.2.1)
+ factory_girl (~> 4.2.0)
+ railties (>= 3.0.0)
formatted_form (2.1.1)
rails (>= 3.1.0)
haml (4.0.3)
@@ -98,13 +96,17 @@ GEM
jquery-rails
railties (>= 3.1.0)
kramdown (1.2.0)
+ launchy (2.3.0)
+ addressable (~> 2.3)
less (2.3.3)
commonjs (~> 0.2.6)
less-rails (2.3.3)
actionpack (>= 3.1)
less (~> 2.3.1)
less-rails-bootstrap (3.0.2)
less-rails (~> 2.3.1)
+ letter_opener (1.1.2)
+ launchy (~> 2.2)
libv8 (3.16.14.3)
mail (2.5.4)
mime-types (~> 1.16)
@@ -198,11 +200,12 @@ PLATFORMS
DEPENDENCIES
capybara
comfortable_mexican_sofa (~> 1.9.0)
+ factory_girl_rails (~> 4.2)
haml-rails
jquery-rails
less-rails
less-rails-bootstrap
- mail_form!
+ letter_opener
pg
rails (= 4.0.0)
rspec-rails (~> 2.14)
@@ -1,21 +1,24 @@
class EmailsController < ApplicationController
+ include ApplicationHelper
+
def new
@email = Email.new
render cms_page: "/contact"
end
def create
@email = Email.new(params[:email])
- @email.request = request
- if @email.deliver
- flash[:success] = "Thank you for your message. We'll respond as soon as we can."
- redirect_to(new_email_path)
- else
- if @email.errors.keys.empty?
- flash.now[:error] = "Sorry, your email could not be delivered."
+ if @email.valid?
+ if @email.submit
+ flash[:success] = "Thank you for your message. We'll respond as soon as we can."
+ return redirect_to(new_email_path)
+ else
+ email = html_obfuscate("info@sheffieldultimate.com")
+ flash[:error] = "Sorry, your message could not be delivered. Please email <a href=\"mailto:#{email}\">#{email}</a>.".html_safe
end
- render cms_page: "/contact"
end
+
+ render cms_page: "/contact"
end
end
@@ -21,4 +21,12 @@ def nav_link(link_text, link_path)
link_to link_text, link_path
end
end
+
+ def html_obfuscate(string)
+ encoded_chars = []
+ string.each_char do |char|
+ encoded_chars << "&##{char[0].ord};"
+ end
+ encoded_chars.join
+ end
end
View
No changes.
@@ -0,0 +1,12 @@
+class ContactMailer < ActionMailer::Base
+ default to: "info@sheffieldultimate.co.uk",
+ subject: "Message From Website Contact Form"
+
+ def contact_email(email)
+ @name = email.name
+ @email = email.email
+ @message = email.message
+ from = "#{@name} <#{@email}>"
+ mail(from: from, reply_to: from)
+ end
+end
View
@@ -1,14 +1,11 @@
-class Email < MailForm::Base
- attribute :name, validate: true
- attribute :email, validate: /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
- attribute :message, validate: true
- attribute :nickname, captcha: true
+class Email < Tableless
+ attr_accessor :name, :email, :message, :first_name
+ validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i }
+ validates_presence_of :name
+ validates_presence_of :message
+ validates_absence_of :first_name
- def headers
- {
- subject: "Message From Website Contact Form",
- to: "info@sheffieldultimate.co.uk",
- from: %("#{name}" <#{email}>)
- }
+ def submit
+ ContactMailer.contact_email(self).deliver
end
end
@@ -0,0 +1,26 @@
+class Tableless
+ include ActiveModel::Validations
+ include ActiveModel::Conversion
+ extend ActiveModel::Naming
+
+ attr_accessor :new_record
+
+ def initialize(attributes={})
+ self.new_record = attributes.blank?
+ attributes && attributes.each do |name, value|
+ send("#{name}=", value) if respond_to? name.to_sym
+ end
+ end
+
+ def new_record?
+ return self.new_record
+ end
+
+ def persisted?
+ false
+ end
+
+ def self.inspect
+ "#<#{ self.to_s} #{ self.attributes.collect{ |e| ":#{ e }" }.join(', ') }>"
+ end
+end
@@ -0,0 +1,14 @@
+!!! 5
+%html
+%head
+ %meta{ charset: "utf-8" }
+%body
+ .name
+ %strong Name:
+ = @name
+ .email
+ %strong Email:
+ = mail_to @email
+ .message
+ = simple_format(@message, {}, sanitize: false)
+
@@ -0,0 +1,4 @@
+Name: <%= @name %>
+Email: <%= @email %>
+
+<%= @message %>
@@ -1,8 +1,9 @@
= simple_form_for @email do |f|
- = f.input :name, required: true, input_html: { autofocus: true }
- = f.input :email, required: true
- = f.input :message, as: :text, required: true, input_html: { rows: 10 }
+ = f.input :name, input_html: { autofocus: true }
+ = f.input :email
+ = f.input :message, as: :text, input_html: { rows: 10 }
.hide
- = f.input :nickname, hint: "Leave this blank!"
+ = f.input :first_name, hint: "Leave this blank!"
+
= f.button :submit, "Send", class: "btn btn-danger"
@@ -17,7 +17,7 @@
= render partial: "shared/navigation"
%header.banner
- .stretcher
+ .stretcher &nbsp;
%h1= @cms_site.label
.main.container
@@ -13,8 +13,8 @@
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
- # Don't care if the mailer can't send.
- config.action_mailer.raise_delivery_errors = false
+ # We care if the mailer can't send.
+ config.action_mailer.raise_delivery_errors = true
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
@@ -26,4 +26,6 @@
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
+
+ config.action_mailer.delivery_method = :letter_opener
end
@@ -20,7 +20,7 @@
describe :create do
before :all do
- @params = { "name" => "Sam Rayner", "email" => "sam@example.com", "message" => "Hello", "nickname" => "" }
+ @params = { "name" => "Sam Rayner", "email" => "sam@example.com", "message" => "Hello" }
end
it "returns http success" do
@@ -37,49 +37,46 @@
it "delivers the email" do
email = Email.new(@params)
Email.stub(:new).and_return(email)
- email.should_receive(:deliver)
+ email.should_receive(:submit)
post :create, email: @params
end
it "sets a success flash message" do
post :create, email: @params
- flash[:success].should include("Thank you")
+ flash[:success].should match(/thank you/i)
end
it "redirects to the contact page" do
post :create, email: @params
response.should redirect_to new_email_path
end
- context "delivery failure" do
+ context "spam" do
before :each do
- @email = Email.new(@params)
- @email.stub(:deliver).and_return(false)
- Email.stub(:new).with(@params).and_return(@email)
- post :create, email: @params
- end
-
- it "sets a flash error message" do
- flash[:error].should include("could not be delivered")
+ spam_params = @params
+ spam_params["first_name"] = "Toast"
+ post :create, email: spam_params
end
- it "renders the email form" do
+ it "should set render the form" do
response.should render_template("emails/_form")
end
end
- context "spam" do
+ context "delivery failure" do
before :each do
- params = { "name" => "Sam Rayner", "email" => "sam@example.com", "message" => "Hello", "nickname" => "Toast" }
- @email = Email.new(params)
- post :create, email: params
+ @email = Email.new(@params)
+ @email.stub(:submit).and_return(false)
+ Email.stub(:new).with(@params).and_return(@email)
+ Email.stub(:attributes).and_return([])
+ post :create, email: @params
end
- it "sets a flash error message" do
- flash[:error].should include("could not be delivered")
+ it "should set the flash" do
+ flash[:error].should match(/could not be delivered/i)
end
- it "renders the email form" do
+ it "should set render the form" do
response.should render_template("emails/_form")
end
end
@@ -0,0 +1,7 @@
+FactoryGirl.define do
+ factory :email do
+ name "Sam Rayner"
+ email "test@example.com"
+ message "Hello"
+ end
+end
@@ -27,21 +27,21 @@
page.body.should have_content("is invalid")
end
- it "does not deliver a message with a missing email" do
+ it "does not deliver a message with an invalid email" do
visit new_email_path
fill_in 'Name', with: 'Sam Rayner'
fill_in 'Message', with: 'What a great website.'
click_button 'Send'
- page.body.should have_content("can't be blank")
+ page.body.should have_content("is invalid")
end
it "does not deliver spam" do
visit new_email_path
fill_in 'Name', with: 'Sam Rayner'
fill_in 'Email', with: 'spammer@spammyjunk.com'
fill_in 'Message', with: "All the junk you'll never need."
- fill_in 'Nickname', with: 'Want to buy some boots?'
+ fill_in 'First name', with: 'Want to buy some gold?'
click_button 'Send'
- page.body.should have_content('could not be delivered')
+ page.body.should have_content('must be blank')
end
end
@@ -20,4 +20,10 @@
nav_link("Title", "not_path").should == '<li class=""><a href="not_path">Title</a></li>'
end
end
+
+ describe :html_obfuscate do
+ it "should encode a string correctly" do
+ html_obfuscate("info@sheffieldultimate.co.uk").should == "&#105;&#110;&#102;&#111;&#64;&#115;&#104;&#101;&#102;&#102;&#105;&#101;&#108;&#100;&#117;&#108;&#116;&#105;&#109;&#97;&#116;&#101;&#46;&#99;&#111;&#46;&#117;&#107;"
+ end
+ end
end
Oops, something went wrong.

0 comments on commit 92aae55

Please sign in to comment.