From 5ff655eb0d03a3473f364e4abd79844faf8443d5 Mon Sep 17 00:00:00 2001
From: kd00506 <kd00506@surrey.ac.uk>
Date: Thu, 19 Nov 2020 13:18:01 +0000
Subject: [PATCH] Added mailer

---
 app/controllers/contact_controller.rb            | 9 +++++++--
 app/mailers/application_mailer.rb                | 2 +-
 app/mailers/contact_mailer.rb                    | 9 +++++++++
 app/views/contact/contact.html.haml              | 3 +++
 app/views/contact_mailer/contact_email.html.haml | 4 ++++
 app/views/layouts/mailer.html.haml               | 8 ++++++++
 app/views/layouts/mailer.text.haml               | 1 +
 test/mailers/contact_mailer_test.rb              | 9 +++++++++
 test/mailers/previews/contact_mailer_preview.rb  | 6 ++++++
 9 files changed, 48 insertions(+), 3 deletions(-)
 create mode 100644 app/mailers/contact_mailer.rb
 create mode 100644 app/views/contact_mailer/contact_email.html.haml
 create mode 100644 app/views/layouts/mailer.html.haml
 create mode 100644 app/views/layouts/mailer.text.haml
 create mode 100644 test/mailers/contact_mailer_test.rb
 create mode 100644 test/mailers/previews/contact_mailer_preview.rb

diff --git a/app/controllers/contact_controller.rb b/app/controllers/contact_controller.rb
index 79f3e44..a046323 100644
--- a/app/controllers/contact_controller.rb
+++ b/app/controllers/contact_controller.rb
@@ -4,7 +4,12 @@ class ContactController < ApplicationController
   end
 
   def sendmessage
-
-    redirect_to homepage_path
+    if params[:email].blank?
+      flash[:alert] = 'Blank email'
+      redirect_to contact_path
+    else
+      ContactMailer.contact_email(params[:email], params[:message]).deliver_now
+      redirect_to homepage_path
+    end
   end
 end
diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb
index 286b223..ce92e2e 100644
--- a/app/mailers/application_mailer.rb
+++ b/app/mailers/application_mailer.rb
@@ -1,4 +1,4 @@
 class ApplicationMailer < ActionMailer::Base
-  default from: 'from@example.com'
+  default to: 'admim@team.com', from: 'a@customer.com'
   layout 'mailer'
 end
diff --git a/app/mailers/contact_mailer.rb b/app/mailers/contact_mailer.rb
new file mode 100644
index 0000000..6de699a
--- /dev/null
+++ b/app/mailers/contact_mailer.rb
@@ -0,0 +1,9 @@
+class ContactMailer < ApplicationMailer
+
+  def contact_email(email, message)
+    @email = email
+    @message = message
+
+    mail cc: email
+  end
+end
diff --git a/app/views/contact/contact.html.haml b/app/views/contact/contact.html.haml
index cfe6997..2faa1c9 100644
--- a/app/views/contact/contact.html.haml
+++ b/app/views/contact/contact.html.haml
@@ -1,5 +1,8 @@
 %h1= "Contact Us"
 
+- flash.each do |key, value|
+  = content_tag :div, content_tag(:p, value), id: "#{key}" unless value.blank?
+
 = form_tag sendmessage_path, method: 'post' do
 
   = label_tag :name, "Email"
diff --git a/app/views/contact_mailer/contact_email.html.haml b/app/views/contact_mailer/contact_email.html.haml
new file mode 100644
index 0000000..2571803
--- /dev/null
+++ b/app/views/contact_mailer/contact_email.html.haml
@@ -0,0 +1,4 @@
+%p= "Customer Email: " + @email
+%br
+%p= "Message:"
+%p= @message
\ No newline at end of file
diff --git a/app/views/layouts/mailer.html.haml b/app/views/layouts/mailer.html.haml
new file mode 100644
index 0000000..cbf6b8e
--- /dev/null
+++ b/app/views/layouts/mailer.html.haml
@@ -0,0 +1,8 @@
+!!!
+%html
+  %head
+    %meta{:content => "text/html; charset=utf-8", "http-equiv" => "Content-Type"}/
+    :css
+      /* Email styles need to be inline */
+  %body
+    = yield
diff --git a/app/views/layouts/mailer.text.haml b/app/views/layouts/mailer.text.haml
new file mode 100644
index 0000000..0a90f09
--- /dev/null
+++ b/app/views/layouts/mailer.text.haml
@@ -0,0 +1 @@
+= yield
diff --git a/test/mailers/contact_mailer_test.rb b/test/mailers/contact_mailer_test.rb
new file mode 100644
index 0000000..fabcff0
--- /dev/null
+++ b/test/mailers/contact_mailer_test.rb
@@ -0,0 +1,9 @@
+require 'test_helper'
+
+class ContactMailerTest < ActionMailer::TestCase
+  test 'should return contact email' do
+    mail = ContactMailer.contact_email('kieran@dore.com', "what's up bro")
+    assert_equal ['admin@team.com'], mail.to
+    assert_equal ['no@one.com'], mail.from
+  end
+end
diff --git a/test/mailers/previews/contact_mailer_preview.rb b/test/mailers/previews/contact_mailer_preview.rb
new file mode 100644
index 0000000..ab080a0
--- /dev/null
+++ b/test/mailers/previews/contact_mailer_preview.rb
@@ -0,0 +1,6 @@
+# Preview all emails at http://localhost:3000/rails/mailers/contact_mailer
+class ContactMailerPreview < ActionMailer::Preview
+  def contact_email
+    ContactMailer.contact_email('some@one.com', 'watm')
+  end
+end
\ No newline at end of file
-- 
GitLab