From e65402ffa4aba58137a080ce907f2b615ee64da3 Mon Sep 17 00:00:00 2001 From: Amanda Kom Date: Wed, 8 Apr 2015 13:06:38 -0700 Subject: [PATCH 1/2] start to mailer --- Gemfile | 4 +++ Gemfile.lock | 7 +++++ app/assets/javascripts/contact_form.js.coffee | 3 ++ app/assets/stylesheets/contact_form.css.scss | 3 ++ app/controllers/contact_forms_controller.rb | 19 ++++++++++++ app/helpers/contact_form_helper.rb | 2 ++ app/mailers/application_mailer.rb | 4 +++ app/mailers/user_mailer.rb | 12 ++++++++ app/views/contact_form/create.html.erb | 2 ++ app/views/contact_form/new.html.erb | 29 ++++++++++++++++++ app/views/user_mailer/contact_email.html.erb | 15 +++++++++ app/views/user_mailer/contact_email.text.erb | 6 ++++ config/environments/development.rb | 12 +++++++- config/routes.rb | 4 +++ dump.rdb | Bin 0 -> 1204 bytes .../contact_form_controller_test.rb | 14 +++++++++ test/features/mailer_test.rb | 18 +++++++++++ test/helpers/contact_form_helper_test.rb | 9 ++++++ test/mailers/previews/user_mailer_preview.rb | 4 +++ test/mailers/user_mailer_test.rb | 7 +++++ test/test_helper.rb | 2 ++ 21 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/contact_form.js.coffee create mode 100644 app/assets/stylesheets/contact_form.css.scss create mode 100644 app/controllers/contact_forms_controller.rb create mode 100644 app/helpers/contact_form_helper.rb create mode 100644 app/mailers/application_mailer.rb create mode 100644 app/mailers/user_mailer.rb create mode 100644 app/views/contact_form/create.html.erb create mode 100644 app/views/contact_form/new.html.erb create mode 100644 app/views/user_mailer/contact_email.html.erb create mode 100644 app/views/user_mailer/contact_email.text.erb create mode 100644 dump.rdb create mode 100644 test/controllers/contact_form_controller_test.rb create mode 100644 test/features/mailer_test.rb create mode 100644 test/helpers/contact_form_helper_test.rb create mode 100644 test/mailers/previews/user_mailer_preview.rb create mode 100644 test/mailers/user_mailer_test.rb diff --git a/Gemfile b/Gemfile index 9784b5b..12807a4 100644 --- a/Gemfile +++ b/Gemfile @@ -42,6 +42,10 @@ gem 'fog-aws' gem 'carrierwave_direct' gem 'sidekiq' +group :test do + gem 'email_spec' +end + # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' diff --git a/Gemfile.lock b/Gemfile.lock index 7208fcd..9eb2483 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -28,6 +28,7 @@ GEM minitest (~> 5.1) thread_safe (~> 0.1) tzinfo (~> 1.1) + addressable (2.3.8) arel (5.0.1.20140414130214) autoprefixer-rails (2.2.0.20140727) execjs @@ -71,6 +72,9 @@ GEM dotenv (2.0.0) dotenv-rails (2.0.0) dotenv (= 2.0.0) + email_spec (1.6.0) + launchy (~> 2.1) + mail (~> 2.2) erubis (2.7.0) excon (0.45.1) execjs (2.3.0) @@ -174,6 +178,8 @@ GEM railties (>= 3.0, < 5.0) thor (>= 0.14, < 2.0) json (1.8.2) + launchy (2.4.3) + addressable (~> 2.3) mail (2.5.4) mime-types (~> 1.16) treetop (~> 1.4.8) @@ -313,6 +319,7 @@ DEPENDENCIES coffee-rails (~> 4.0.0) devise dotenv-rails + email_spec fog fog-aws font-awesome-rails diff --git a/app/assets/javascripts/contact_form.js.coffee b/app/assets/javascripts/contact_form.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/contact_form.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/contact_form.css.scss b/app/assets/stylesheets/contact_form.css.scss new file mode 100644 index 0000000..d3f44be --- /dev/null +++ b/app/assets/stylesheets/contact_form.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the contact_form controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/contact_forms_controller.rb b/app/controllers/contact_forms_controller.rb new file mode 100644 index 0000000..4bba28e --- /dev/null +++ b/app/controllers/contact_forms_controller.rb @@ -0,0 +1,19 @@ +class ContactFormsController < ApplicationController + def new + @contact_form = ContactForm.new + end + + def create + begin + @contact_form = ContactForm.new(params[:contact_form]) + @contact_form.request = request + if @contact_form.deliver + flash.now[:notice] = "Thank you for your message!" + else + render :new + end + rescue ScriptError + flash[:error] = "Sorry, this image appears to be spam and was not delivered." + end + end +end diff --git a/app/helpers/contact_form_helper.rb b/app/helpers/contact_form_helper.rb new file mode 100644 index 0000000..603a2a7 --- /dev/null +++ b/app/helpers/contact_form_helper.rb @@ -0,0 +1,2 @@ +module ContactFormHelper +end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb new file mode 100644 index 0000000..d25d889 --- /dev/null +++ b/app/mailers/application_mailer.rb @@ -0,0 +1,4 @@ +class ApplicationMailer < ActionMailer::Base + default from: "from@example.com" + layout 'mailer' +end diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb new file mode 100644 index 0000000..db9ca6d --- /dev/null +++ b/app/mailers/user_mailer.rb @@ -0,0 +1,12 @@ +class UserMailer < ApplicationMailer + default from: 'mandakomrodrigues@gmail.com' + + def contact_me(user) + @user = user + @email = email + @message = message + mail(to: "mandareneekom@gmail.com", + from: @email, + subject: "Contact from #{@user}") + end +end diff --git a/app/views/contact_form/create.html.erb b/app/views/contact_form/create.html.erb new file mode 100644 index 0000000..073bf14 --- /dev/null +++ b/app/views/contact_form/create.html.erb @@ -0,0 +1,2 @@ +

ContactForm#create

+

Find me in app/views/contact_form/create.html.erb

diff --git a/app/views/contact_form/new.html.erb b/app/views/contact_form/new.html.erb new file mode 100644 index 0000000..dcdc05e --- /dev/null +++ b/app/views/contact_form/new.html.erb @@ -0,0 +1,29 @@ +<%= form_for @contact_form do |f| %> + <% if @contact_form.errors.any? %> +
+

<%= pluralize(@contact_form.errors.count, "error") %> prohibited this form from being saved:

+ + +
+ <% end %> + +
+ <%= f.label :name, "Name:" %> + <%= f.text_area :name %> +
+
+ <%= f.label :email, "Name:" %> + <%= f.text_area :email %> +
+
+ <%= f.label :message, "Name:" %> + <%= f.text_area :message %> +
+
+ <%= f.submit "Send"%> +
+<% end %> diff --git a/app/views/user_mailer/contact_email.html.erb b/app/views/user_mailer/contact_email.html.erb new file mode 100644 index 0000000..8f0d87d --- /dev/null +++ b/app/views/user_mailer/contact_email.html.erb @@ -0,0 +1,15 @@ + + + + + + +

Contact from <%= @user.name %>

+

+ <%= @message %> +

+

+ Reply to: <%= @email %>. +

+ + diff --git a/app/views/user_mailer/contact_email.text.erb b/app/views/user_mailer/contact_email.text.erb new file mode 100644 index 0000000..1f4bb93 --- /dev/null +++ b/app/views/user_mailer/contact_email.text.erb @@ -0,0 +1,6 @@ +Contact from <%= @user.name %> +============================== + +<%= @message %> + +Reply to: <%= @email %>. diff --git a/config/environments/development.rb b/config/environments/development.rb index 34c267f..c722ecd 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -14,7 +14,17 @@ config.action_controller.perform_caching = false # Don't care if the mailer can't send. - config.action_mailer.raise_delivery_errors = false + config.action_mailer.raise_delivery_errors = true + config.action_mailer.delivery_method = :smtp + config.action_mailer.smtp_settings = { + address: "smtp.gmail.com", + port: 587, + domain: "mandakom.com", + authentication: "plain", + enable_starttls_auto: true, + user_name: ENV["GMAIL_USERNAME"], + password: ENV["GMAIL_PASSWORD"] + } config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } diff --git a/config/routes.rb b/config/routes.rb index c5081b3..98cc184 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,9 @@ Rails.application.routes.draw do + resources :contact_forms + + get 'contact_form/create' + devise_for :users, path_names: {sign_in: "Sign In", sign_out: "Sign Out"}, controllers: {omniauth_callbacks: "omniauth_callbacks"} resources :users diff --git a/dump.rdb b/dump.rdb new file mode 100644 index 0000000000000000000000000000000000000000..89ac3af5860348eb5f966fad41d49584c2cde008 GIT binary patch literal 1204 zcmajf%WvF790zba+0B!t4TbP%)1A(VN?vy!epkS5)m59Qk<#6w3biOZ9*=_)$KKlG zmIxxH;>Hzmq2fR##05AbA*58f^uQmnhy!ZjhQuiXyqi=?pr}Vb^qb#&f1jCl>(W98 zA!Plfj8-!tvrTok`E~Pg?L#zIM|_)IbEeRD(q?PFawj7q(^Cq3P5eVV>Z1gL1#_``E1u z#;^(0SA2~9^9xItS1i*Y`r`6s_?`yWq5!O)4p_*zi!2sk-!K`WCgC_l2GpQPx)0`p z6(*sF4dR&Cp@ixNauIewZ6eFkY-K}~Eq?_TYn^LXBPpooT`cm=)(2elWRxZz2$*C( z%f-te%0Zm+brE>ro#ji->n(5#_O^c;%Wpx@XOaoggK>r$6nm;{y~kcn~V?=$@A^dW|XQp45_*S0OPrNOL7 z`qfjay|wpy`^(RVQJsBmRHGHYWV)kPq9jar8{am5C_{Qb&1J$i2B-301 Date: Sun, 26 Apr 2015 23:58:36 -0700 Subject: [PATCH 2/2] updating devise key --- config/initializers/devise.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 6c07616..15b3571 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -5,7 +5,7 @@ # random tokens. Changing this key will render invalid all existing # confirmation, reset password and unlock tokens in the database. # config.secret_key = 'd256155b9e876f5dd6ab1f7391d7e74bab439451b987c8de3da64a4787efc4c8571a0e27735f22003d33bd0f2f0a6a3366536d16d2e6be970d04822b06663901' - + config.secret_key = ENV["DEVISE_KEY"] # ==> Mailer Configuration # Configure the e-mail address which will be shown in Devise::Mailer, # note that it will be overwritten if you use your own mailer class