diff --git a/lib/email/sender.rb b/lib/email/sender.rb index df3d519c139..414a5a41e31 100644 --- a/lib/email/sender.rb +++ b/lib/email/sender.rb @@ -12,7 +12,15 @@ require 'uri' require 'net/smtp' SMTP_CLIENT_ERRORS = [Net::SMTPFatalError, Net::SMTPSyntaxError] -BYPASS_DISABLE_TYPES = ["admin_login", "test_message"] +BYPASS_DISABLE_TYPES = %w( + admin_login + test_message + new_version + group_smtp + invite + download_backup_message + admin_confirmation_message +) module Email class Sender @@ -37,7 +45,7 @@ module Email return skip(SkippedEmailLog.reason_types[:sender_message_to_blank]) if @message.to.blank? if SiteSetting.disable_emails == "non-staff" && !bypass_disable - return unless User.find_by_email(to_address)&.staff? + return unless find_user&.staff? end return skip(SkippedEmailLog.reason_types[:sender_message_to_invalid]) if to_address.end_with?(".invalid") @@ -221,6 +229,11 @@ module Email email_log end + def find_user + return @user if @user + User.find_by_email(to_address) + end + def to_address @to_address ||= begin to = @message.try(:to) diff --git a/spec/components/email/sender_spec.rb b/spec/components/email/sender_spec.rb index 26d4dcffbe9..b0f675814c9 100644 --- a/spec/components/email/sender_spec.rb +++ b/spec/components/email/sender_spec.rb @@ -49,6 +49,20 @@ describe Email::Sender do message = Mail::Message.new(to: moderator.email, body: "hello") Email::Sender.new(message, :hello).send end + + it "delivers mail to staff user when confirming new email if user is provided" do + Mail::Message.any_instance.expects(:deliver_now).once + Fabricate(:email_change_request, { + user: moderator, + new_email: "newemail@testmoderator.com", + old_email: moderator.email, + change_state: EmailChangeRequest.states[:authorizing_new] + }) + message = Mail::Message.new( + to: "newemail@testmoderator.com", body: "hello" + ) + Email::Sender.new(message, :confirm_new_email, moderator).send + end end end