From 9ad3bca2ea475b709c8311ab25194e4578c1a0d9 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Thu, 12 Oct 2023 21:51:02 -0400 Subject: Webmaster gets an email when a comment is posted --- app/controllers/comments_controller.rb | 4 ++++ app/mailers/application_mailer.rb | 2 +- app/mailers/comment_mailer.rb | 7 +++++++ .../comment_mailer/new_comment_email.html.haml | 22 ++++++++++++++++++++++ .../comment_mailer/new_comment_email.text.erb | 20 ++++++++++++++++++++ config/deploy.rb | 2 +- config/environments/development.rb | 1 + config/environments/production.rb | 4 +++- config/mail.yml | 8 ++++++++ test/mailers/comment_mailer_test.rb | 7 +++++++ test/mailers/previews/comment_mailer_preview.rb | 4 ++++ 11 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 app/mailers/comment_mailer.rb create mode 100644 app/views/comment_mailer/new_comment_email.html.haml create mode 100644 app/views/comment_mailer/new_comment_email.text.erb create mode 100644 config/mail.yml create mode 100644 test/mailers/comment_mailer_test.rb create mode 100644 test/mailers/previews/comment_mailer_preview.rb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 60c8f6a..c66365b 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -43,6 +43,10 @@ class CommentsController < ApplicationController if @comment.save flash.notice = flash_message + + if @comment.status == :published + CommentMailer.with(comment: @comment).new_comment_email.deliver_later + end else flash.alert = "Error posting comment." end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 286b223..4862159 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 from: 'no-reply@fourisland.com' layout 'mailer' end diff --git a/app/mailers/comment_mailer.rb b/app/mailers/comment_mailer.rb new file mode 100644 index 0000000..363875a --- /dev/null +++ b/app/mailers/comment_mailer.rb @@ -0,0 +1,7 @@ +class CommentMailer < ApplicationMailer + def new_comment_email + @comment = params[:comment] + @admin = User.first # this is weird + mail(to: @admin.email, subject: "[Four Island] Comment on #{@comment.blog.title}") + end +end diff --git a/app/views/comment_mailer/new_comment_email.html.haml b/app/views/comment_mailer/new_comment_email.html.haml new file mode 100644 index 0000000..bd9e91c --- /dev/null +++ b/app/views/comment_mailer/new_comment_email.html.haml @@ -0,0 +1,22 @@ +%h1 Four Island +%p + A comment has been posted on + = link_to @comment.blog.title, @comment.blog + by the following user: +%ul + %li + Name: + = @comment.username + %li + Email: + = @comment.email + - unless @comment.website.empty? + %li + Website: + = @comment.website +%p Here is the comment: +%blockquote= @comment.body +%p + Posted: + = @comment.published_at.strftime("%B #{@comment.published_at.day.ordinalize}, %Y at %-I:%M:%S%P") +%p= link_to "See the comment on the web", blog_url(@comment.blog, anchor: "comment-#{@comment.id}") diff --git a/app/views/comment_mailer/new_comment_email.text.erb b/app/views/comment_mailer/new_comment_email.text.erb new file mode 100644 index 0000000..e37046f --- /dev/null +++ b/app/views/comment_mailer/new_comment_email.text.erb @@ -0,0 +1,20 @@ +Four Island +=========== + +A comment has been posted on <%= @comment.blog.title %> by the following user: + +* Name: <%= @comment.username %> +* Email: <%= @comment.email %> +<% unless @comment.website.empty? %>* Website: <%= @comment.website %> +<% end %> +Here is the comment: + +--- + +<%= @comment.body %> + +--- + +Posted: <%= @comment.published_at.strftime("%B #{@comment.published_at.day.ordinalize}, %Y at %-I:%M:%S%P") %> + +See the comment on the web: <%= blog_url(@comment.blog, anchor: "comment-#{@comment.id}") %> diff --git a/config/deploy.rb b/config/deploy.rb index cbac23c..05b137d 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -21,7 +21,7 @@ set :deploy_to, "/srv/www/thoughts" # set :pty, true # Default value for :linked_files is [] -append :linked_files, "config/database.yml", "config/secrets.yml", "config/lingo.yml", "config/akismet.yml" +append :linked_files, "config/database.yml", "config/secrets.yml", "config/lingo.yml", "config/akismet.yml", "config/mail.yml" # Default value for linked_dirs is [] append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/uploads" diff --git a/config/environments/development.rb b/config/environments/development.rb index 8500f45..e683ebb 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -37,6 +37,7 @@ Rails.application.configure do config.active_storage.service = :local # Don't care if the mailer can't send. + config.action_mailer.default_url_options = { host: 'localhost:3000' } config.action_mailer.raise_delivery_errors = false config.action_mailer.perform_caching = false diff --git a/config/environments/production.rb b/config/environments/production.rb index 182b74e..e814a66 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -66,7 +66,9 @@ Rails.application.configure do # Set this to true and configure the email server for immediate delivery to raise delivery errors. # config.action_mailer.raise_delivery_errors = false - config.action_mailer.default_url_options = { host: 'feffernoo.se' } + config.action_mailer.default_url_options = { host: 'www.fourisland.com' } + config.action_mailer.delivery_method = :smtp + config.action_mailer.smtp_settings = Rails.application.config_for(:mail)[:smtp_settings] # Enable locale fallbacks for I18n (makes lookups for any locale fall back to # the I18n.default_locale when a translation cannot be found). diff --git a/config/mail.yml b/config/mail.yml new file mode 100644 index 0000000..3c88234 --- /dev/null +++ b/config/mail.yml @@ -0,0 +1,8 @@ +production: + smtp_settings: + address: "" + port: 25 + user_name: "" + password: "" + authentication: "" + openssl_verify_mode: "" diff --git a/test/mailers/comment_mailer_test.rb b/test/mailers/comment_mailer_test.rb new file mode 100644 index 0000000..996d230 --- /dev/null +++ b/test/mailers/comment_mailer_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class CommentMailerTest < ActionMailer::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/mailers/previews/comment_mailer_preview.rb b/test/mailers/previews/comment_mailer_preview.rb new file mode 100644 index 0000000..20fc4e6 --- /dev/null +++ b/test/mailers/previews/comment_mailer_preview.rb @@ -0,0 +1,4 @@ +# Preview all emails at http://localhost:3000/rails/mailers/comment_mailer +class CommentMailerPreview < ActionMailer::Preview + +end -- cgit 1.4.1