Skip to content

Commit

Permalink
Add config for notifications from email address & requiring name and/…
Browse files Browse the repository at this point in the history
…or subject.
  • Loading branch information
JDutil committed Feb 25, 2012
1 parent 5bdac6d commit a13cf0f
Show file tree
Hide file tree
Showing 23 changed files with 267 additions and 86 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
--format=nested --format=nested
--backtrace --backtrace
--profile --profile
--order random
8 changes: 8 additions & 0 deletions CHANGELOG
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,11 @@
*ContactUs 0.2.0 (February 25th 2012)*

* Added ContactUs.mailer_from setting in order to send from verified address, which is better practice than pretending the email is from the user. [Jeff Dutil]

* Added ContactUs.require_name setting in order to ask for the name with the contact form. [Jeff Dutil]

* Added ContactUs.require_subject setting in order to for the subject with the contact form. [Jeff Dutil]

*ContactUs 0.1.5 (January 6th, 2012)* *ContactUs 0.1.5 (January 6th, 2012)*


* Added Chinese zh locale [kinopyo] * Added Chinese zh locale [kinopyo]
Expand Down
23 changes: 21 additions & 2 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -38,9 +38,29 @@ Change to the email address you would like to receive the form submissions at fo


config.mailer_to = "contact@yourdomain.com" config.mailer_to = "contact@yourdomain.com"


By default the emails from field will be the email entered by the user to easily reply, but this may not be allowed if your required to verify your sending email addresses.
You may also specify an email address for the notification emails from field:

config.mailer_from = "dontreply@yourdomain.com"

## UPGRADING

When upgrading from 0.1.x to 0.2.x you should rerun the install generator to install the new settings, views, and locale updates:

$ bundle exec rake contact_us:install

Or you may run the generators for each specific component you would like to update, which is quite useful when upgrading during patch releases for example from 0.2.0 to 0.2.1:

$ bundle exec rake contact_us:copy_locales
$ bundle exec rake contact_us:copy_views

## CONFIGURATION ## CONFIGURATION


The generator copies the view files to `app/views/contact_us`, and you can customize them to suit your needs. The generator copies the view files to `app/views/contact_us`, and you can customize them to suit your needs. If you would like to add a name or subject field to the form you may simply
set the options to true within the contact_us initializer located at `config/initializers/contact_us.rb`:

config.name = true
config.subject = true


You may also update your locales under `config/locales/contact_us.en.yml` or create your own. Please feel free to submit your own locales so that other users will hopefully find this gem more useful. You may also update your locales under `config/locales/contact_us.en.yml` or create your own. Please feel free to submit your own locales so that other users will hopefully find this gem more useful.


Expand Down Expand Up @@ -78,6 +98,5 @@ Here are some ways *you* can contribute:
## TODO ## TODO


* Add new language translations * Add new language translations
* Make requested configurations in branches, and document how to reference within your Gemfile rather than the version. Configurations todo: with name, with subject, with name & subject


Copyright (c) 2011 Jeff Dutil, released under the [MIT license](https://github.com/jdutil/contact_us/tree/master/MIT-LICENSE). Copyright (c) 2011 Jeff Dutil, released under the [MIT license](https://github.com/jdutil/contact_us/tree/master/MIT-LICENSE).
6 changes: 3 additions & 3 deletions app/mailers/contact_us/contact_mailer.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,9 +1,9 @@
class ContactUs::ContactMailer < ActionMailer::Base class ContactUs::ContactMailer < ActionMailer::Base
def contact_email(contact) def contact_email(contact)
@message = contact.message @contact = contact


mail :from => contact.email, mail :from => (ContactUs.mailer_from || contact.email),
:subject => t('contact_us.contact_mailer.contact_email.subject', :email => contact.email), :subject => (ContactUs.require_subject ? @contact.subject : t('contact_us.contact_mailer.contact_email.subject', :email => contact.email)),
:to => ContactUs.mailer_to :to => ContactUs.mailer_to
end end
end end
4 changes: 3 additions & 1 deletion app/models/contact_us/contact.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ class ContactUs::Contact
include ActiveModel::Conversion include ActiveModel::Conversion
include ActiveModel::Validations include ActiveModel::Validations


attr_accessor :email, :message attr_accessor :email, :message, :name, :subject


validates :email, :format => { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i }, validates :email, :format => { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i },
:presence => true :presence => true
validates :message, :presence => true validates :message, :presence => true
validates :name, :presence => {:if => Proc.new{ContactUs.require_name}}
validates :subject, :presence => {:if => Proc.new{ContactUs.require_subject}}


def initialize(attributes = {}) def initialize(attributes = {})
attributes.each do |key, value| attributes.each do |key, value|
Expand Down
4 changes: 2 additions & 2 deletions app/views/contact_us/contact_mailer/contact_email.html.erb
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,3 @@
<p><%= @message %></p> <p><%= @contact.message %></p>
<p>---------------------</p> <p>---------------------</p>
<p><%= t('.sent_by_contact_form') %></p> <p><%= ContactUs.require_name ? t('.sent_by_name', :email => @contact.email, :name => @contact.name) : t('.sent_by_contact_form', :email => @contact.email) %></p>
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= @message %> <%= @contact.message %>


------------------------------------- -------------------------------------
<%= t('.sent_by_contact_form') %> <%= ContactUs.require_name ? t('.sent_by_name', :email => @contact.email, :name => @contact.name) : t('.sent_by_contact_form', :email => @contact.email) %>
2 changes: 2 additions & 0 deletions app/views/contact_us/contacts/new.html.erb
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,8 @@
<h2><%= t('.contact_us') %></h2> <h2><%= t('.contact_us') %></h2>
<%= semantic_form_for @contact, :url => contacts_path do |f| %> <%= semantic_form_for @contact, :url => contacts_path do |f| %>
<%= f.input :name, :label => t('.name') if ContactUs.require_name %>
<%= f.input :email, :label => t('.email') %> <%= f.input :email, :label => t('.email') %>
<%= f.input :subject, :label => t('.subject') if ContactUs.require_subject %>
<%= f.input :message, :as => :text, :label => t('.message') %> <%= f.input :message, :as => :text, :label => t('.message') %>
<%= f.commit_button t('.submit'), :button_html => { :alt => t('.submit'), :id => 'contact_us_contact_submit', :title => t('.submit') } %> <%= f.commit_button t('.submit'), :button_html => { :alt => t('.submit'), :id => 'contact_us_contact_submit', :title => t('.submit') } %>
<% end %> <% end %>
5 changes: 4 additions & 1 deletion config/locales/contact_us.de.yml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ de:
contact_us: contact_us:
contact_mailer: contact_mailer:
contact_email: contact_email:
sent_by_contact_form: "Gesendet durch das Kontaktformular." sent_by_contact_form: "Gesendet durch das Kontaktformular. %{email}"
sent_by_name: "Sent by %{name} from %{email}"
subject: "Contact Us message from %{email}" subject: "Contact Us message from %{email}"
contacts: contacts:
new: new:
contact_us: "Kontaktiere Uns" contact_us: "Kontaktiere Uns"
email: "Email" email: "Email"
message: "Nachricht" message: "Nachricht"
name: "Name"
subject: "Subject"
submit: "Abschicken" submit: "Abschicken"
notices: notices:
error: "Beide Felder müssen ausgefüllt werden." error: "Beide Felder müssen ausgefüllt werden."
Expand Down
5 changes: 4 additions & 1 deletion config/locales/contact_us.en.yml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ en:
contact_us: contact_us:
contact_mailer: contact_mailer:
contact_email: contact_email:
sent_by_contact_form: "Sent by contact form." sent_by_contact_form: "Sent by contact form from %{email}"
sent_by_name: "Sent by %{name} from %{email}"
subject: "Contact Us message from %{email}" subject: "Contact Us message from %{email}"
contacts: contacts:
new: new:
contact_us: "Contact Us" contact_us: "Contact Us"
email: "Email" email: "Email"
message: "Message" message: "Message"
name: "Name"
subject: "Subject"
submit: "Submit" submit: "Submit"
notices: notices:
error: "You must enter both fields." error: "You must enter both fields."
Expand Down
5 changes: 4 additions & 1 deletion config/locales/contact_us.es.yml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ es:
contact_us: contact_us:
contact_mailer: contact_mailer:
contact_email: contact_email:
sent_by_contact_form: "Enviado por el formulario de contacto." sent_by_contact_form: "Enviado por el formulario de contacto. %{email}"
sent_by_name: "Sent by %{name} from %{email}"
subject: "Contact Us message from %{email}" subject: "Contact Us message from %{email}"
contacts: contacts:
new: new:
contact_us: "Contactanos" contact_us: "Contactanos"
email: "Email" email: "Email"
message: "Mensaje" message: "Mensaje"
name: "Name"
subject: "Subject"
submit: "Enviar" submit: "Enviar"
notices: notices:
error: "Debes de ingresar ambos campos." error: "Debes de ingresar ambos campos."
Expand Down
5 changes: 4 additions & 1 deletion config/locales/contact_us.it.yml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ it:
contact_us: contact_us:
contact_mailer: contact_mailer:
contact_email: contact_email:
sent_by_contact_form: "Inviato dal formato di contatto." sent_by_contact_form: "Inviato dal formato di contatto. %{email}"
sent_by_name: "Sent by %{name} from %{email}"
subject: "Contact Us message from %{email}" subject: "Contact Us message from %{email}"
contacts: contacts:
new: new:
contact_us: "Contattaci" contact_us: "Contattaci"
email: "Email" email: "Email"
message: "Messaggio" message: "Messaggio"
name: "Name"
subject: "Subject"
submit: "Inviare" submit: "Inviare"
notices: notices:
error: "Devi inserire entrambi i campi." error: "Devi inserire entrambi i campi."
Expand Down
5 changes: 4 additions & 1 deletion config/locales/contact_us.pt-BR.yml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ pt-BR:
contact_us: contact_us:
contact_mailer: contact_mailer:
contact_email: contact_email:
sent_by_contact_form: "Enviado pelo formulário de contato." sent_by_contact_form: "Enviado pelo formulário de contato. %{email}"
sent_by_name: "Sent by %{name} from %{email}"
subject: "Contact Us message from %{email}" subject: "Contact Us message from %{email}"
contacts: contacts:
new: new:
contact_us: "Contate-nos" contact_us: "Contate-nos"
email: "Email" email: "Email"
message: "Messagem" message: "Messagem"
name: "Name"
subject: "Subject"
submit: "Enviar" submit: "Enviar"
notices: notices:
error: "Você deve preencher os campos." error: "Você deve preencher os campos."
Expand Down
5 changes: 4 additions & 1 deletion config/locales/contact_us.zh.yml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ zh:
contact_us: contact_us:
contact_mailer: contact_mailer:
contact_email: contact_email:
sent_by_contact_form: "由contact form发送。" sent_by_contact_form: "由contact form发送。 %{email}"
sent_by_name: "Sent by %{name} from %{email}"
subject: "Contact Us message from %{email}" subject: "Contact Us message from %{email}"
contacts: contacts:
new: new:
contact_us: "联系我们" contact_us: "联系我们"
email: "邮箱" email: "邮箱"
message: "消息" message: "消息"
name: "Name"
subject: "Subject"
submit: "发送" submit: "发送"
notices: notices:
error: "请正确输入邮箱与信息。" error: "请正确输入邮箱与信息。"
Expand Down
1 change: 1 addition & 0 deletions contact_us.gemspec
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Gem::Specification.new do |s|


s.add_development_dependency "capybara", "~> 1.1" s.add_development_dependency "capybara", "~> 1.1"
s.add_development_dependency "rspec-rails", "~> 2.8" s.add_development_dependency "rspec-rails", "~> 2.8"
s.add_development_dependency "shoulda-matchers", "~> 1.0"
s.add_development_dependency "simplecov", "~> 0.6" s.add_development_dependency "simplecov", "~> 0.6"
s.add_development_dependency "sqlite3", "~> 1.3.5" s.add_development_dependency "sqlite3", "~> 1.3.5"


Expand Down
13 changes: 11 additions & 2 deletions lib/contact_us.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,9 +1,18 @@
module ContactUs module ContactUs
require 'contact_us/engine' require 'contact_us/engine'


# Address to send ContactUs e-mails. # Address ContactUs email notifications are sent from.
mattr_accessor :mailer_from

# Address to send ContactUs email notifications to.
mattr_accessor :mailer_to mattr_accessor :mailer_to


# Enable or Disable name field.
mattr_accessor :require_name

# Enable or Disable subject field.
mattr_accessor :require_subject

# Default way to setup ContactUs. Run rake contact_us:install to create # Default way to setup ContactUs. Run rake contact_us:install to create
# a fresh initializer with all configuration values. # a fresh initializer with all configuration values.
def self.setup def self.setup
Expand Down
18 changes: 17 additions & 1 deletion lib/templates/contact_us.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,22 @@
# Use this hook to configure contact mailer. # Use this hook to configure contact mailer.
ContactUs.setup do |config| ContactUs.setup do |config|

# ==> Mailer Configuration # ==> Mailer Configuration
# Configure the e-mail address which will be shown in ContactMailer.
# Configure the e-mail address which email notifications should be sent from. If emails must be sent from a verified email address you may set it here.
# Example:
# config.mailer_from = "contact@please-change-me.com"
config.mailer_from = nil

# Configure the e-mail address which should receive the contact form email notifications.
config.mailer_to = "contact@please-change-me.com" config.mailer_to = "contact@please-change-me.com"

# ==> Form Configuration

# Configure the form to ask for the users name.
config.require_name = false

# Configure the form to ask for a subject.
config.require_subject = false

end end
24 changes: 0 additions & 24 deletions spec/contact_us_spec.rb

This file was deleted.

Loading

0 comments on commit a13cf0f

Please sign in to comment.