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" %> +
+ <%= 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) }