From 243e9b5524cbae50b68dd4e2d273ea5e9f0def1c Mon Sep 17 00:00:00 2001 From: Jacob Herrington Date: Tue, 17 Mar 2020 10:19:42 -0500 Subject: [PATCH] Add a form to email users from internal (#6668) Co-authored-by: MichaelaHunter Co-authored-by: MichaelaHunter --- app/controllers/internal/users_controller.rb | 8 +++++ app/mailers/notify_mailer.rb | 7 ++++ app/views/internal/users/show.html.erb | 15 +++++++++ .../notify_mailer/user_contact_email.html.erb | 3 ++ .../notify_mailer/user_contact_email.text.erb | 1 + config/routes.rb | 1 + spec/mailers/notify_mailer_spec.rb | 32 +++++++++++++++++++ 7 files changed, 67 insertions(+) create mode 100644 app/views/mailers/notify_mailer/user_contact_email.html.erb create mode 100644 app/views/mailers/notify_mailer/user_contact_email.text.erb diff --git a/app/controllers/internal/users_controller.rb b/app/controllers/internal/users_controller.rb index 3ebc752de..fd949ac59 100644 --- a/app/controllers/internal/users_controller.rb +++ b/app/controllers/internal/users_controller.rb @@ -101,6 +101,14 @@ class Internal::UsersController < Internal::ApplicationController redirect_to "/internal/users/#{@user.id}/edit" end + def send_email + if NotifyMailer.user_contact_email(params).deliver + redirect_back(fallback_location: "/users") + else + flash[:danger] = "Email failed to send!" + end + end + private def manage_credits diff --git a/app/mailers/notify_mailer.rb b/app/mailers/notify_mailer.rb index 5f31b74be..ce1b820a8 100644 --- a/app/mailers/notify_mailer.rb +++ b/app/mailers/notify_mailer.rb @@ -66,6 +66,13 @@ class NotifyMailer < ApplicationMailer mail(to: params[:email_to], subject: params[:email_subject]) end + def user_contact_email(params) + @user = User.find(params[:user_id]) + @email_body = params[:email_body] + track utm_campaign: "user_contact" + mail(to: @user.email, subject: params[:email_subject]) + end + def new_message_email(direct_message) @message = direct_message @user = @message.direct_receiver diff --git a/app/views/internal/users/show.html.erb b/app/views/internal/users/show.html.erb index 67e3abe63..10e0ad73f 100644 --- a/app/views/internal/users/show.html.erb +++ b/app/views/internal/users/show.html.erb @@ -49,6 +49,21 @@ <%= render "notes" %> <%= render "user_organizations" %> +
+
+

Email <%= @user.name %>

+ + <%= form_with url: send_email_internal_user_path(@user), local: true do |f| %> + <%= f.hidden_field(:user_id, value: @user.id) %> +

Subject:

+ <%= f.text_field :email_subject, class: "form-control my-1" %> +

Body:

+ <%= f.text_area :email_body, class: "form-control my-1", rows: "10" %> + <%= f.submit "Send Email", class: "btn btn-primary float-right" %> + <% end %> +
+
+

Recent Emails

diff --git a/app/views/mailers/notify_mailer/user_contact_email.html.erb b/app/views/mailers/notify_mailer/user_contact_email.html.erb new file mode 100644 index 000000000..2e34d87eb --- /dev/null +++ b/app/views/mailers/notify_mailer/user_contact_email.html.erb @@ -0,0 +1,3 @@ +

+ <%= simple_format @email_body %> +

diff --git a/app/views/mailers/notify_mailer/user_contact_email.text.erb b/app/views/mailers/notify_mailer/user_contact_email.text.erb new file mode 100644 index 000000000..90ef1c329 --- /dev/null +++ b/app/views/mailers/notify_mailer/user_contact_email.text.erb @@ -0,0 +1 @@ +<%= @email_body %> diff --git a/config/routes.rb b/config/routes.rb index 83df70993..5c74d6abe 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -78,6 +78,7 @@ Rails.application.routes.draw do post "merge" delete "remove_identity" post "recover_identity" + post "send_email" end end resources :organization_memberships, only: %i[update destroy create] diff --git a/spec/mailers/notify_mailer_spec.rb b/spec/mailers/notify_mailer_spec.rb index 1280feb22..b0ffd61bd 100644 --- a/spec/mailers/notify_mailer_spec.rb +++ b/spec/mailers/notify_mailer_spec.rb @@ -228,6 +228,38 @@ RSpec.describe NotifyMailer, type: :mailer do end end + describe "#user_contact_email" do + let(:email_params) do + { + user_id: user.id, + email_subject: "Buddy", + email_body: "Laugh with me, buddy" + } + end + let(:email) { described_class.user_contact_email(email_params) } + + it "renders proper subject" do + expect(email.subject).to eq("Buddy") + end + + it "renders proper sender" do + expect(email.from).to eq([SiteConfig.default_site_email]) + expect(email["from"].value).to eq("DEV Community <#{SiteConfig.default_site_email}>") + end + + it "renders proper receiver" do + expect(email.to).to eq([user.email]) + end + + it "includes the tracking pixel" do + expect(email.html_part.body).to include("open.gif") + end + + it "includes UTM params" do + expect(email.html_part.body).to include(CGI.escape("utm_campaign=user_contact")) + end + end + describe "#new_message_email" do let(:direct_channel) { ChatChannel.create_with_users([user, user2], "direct") } let(:direct_message) { create(:message, user: user, chat_channel: direct_channel) }