Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Add PM whisper feedback for email sending failures (#40250)

Roman Rizzi751a1ce43c25
diff --git a/app/models/problem_check.rb b/app/models/problem_check.rb
index 0d7c8d5c688..a4c3300ade6 100644
--- a/app/models/problem_check.rb
+++ b/app/models/problem_check.rb
@@ -67,7 +67,7 @@ class ProblemCheck
#
CORE_PROBLEM_CHECKS = [
ProblemCheck::BadFaviconUrl,
- ProblemCheck::EmailDeliveryFailures,
+ ProblemCheck::EmailSendingFailures,
ProblemCheck::EmailPollingErroredRecently,
ProblemCheck::FacebookConfig,
ProblemCheck::FailingEmails,
diff --git a/app/services/problem_check/email_delivery_failures.rb b/app/services/problem_check/email_sending_failures.rb
similarity index 80%
rename from app/services/problem_check/email_delivery_failures.rb
rename to app/services/problem_check/email_sending_failures.rb
index ee56e58293a..649dce5b63c 100644
--- a/app/services/problem_check/email_delivery_failures.rb
+++ b/app/services/problem_check/email_sending_failures.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-class ProblemCheck::EmailDeliveryFailures < ProblemCheck
+class ProblemCheck::EmailSendingFailures < ProblemCheck
self.priority = "low"
self.perform_every = 1.hour
@@ -10,14 +10,14 @@ class ProblemCheck::EmailDeliveryFailures < ProblemCheck
# De-duplicate with ProblemCheck::FailingEmails so we only show one
# admin alert for the same SMTP incident when retry jobs are already failing.
return no_problem if failing_email_job_count > 0
- return no_problem if failed_delivery_count == 0
+ return no_problem if failed_send_count == 0
problem
end
private
- def failed_delivery_count
+ def failed_send_count
@failed_delivery_count ||=
SkippedEmailLog
.where(reason_type: SkippedEmailLog.reason_types[:custom])
@@ -30,6 +30,6 @@ class ProblemCheck::EmailDeliveryFailures < ProblemCheck
end
def translation_data
- { count: failed_delivery_count }
+ { count: failed_send_count }
end
end
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 0394aa165b4..73d8e1ed65c 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -1800,9 +1800,9 @@ en:
failing_emails: 'There are %{num_failed_jobs} email jobs that failed. Check your app.yml and ensure that the mail server settings are correct. <a href="%{base_path}/sidekiq/retries" target="_blank">See the failed jobs in Sidekiq</a>.'
subfolder_ends_in_slash: "Your subfolder setup is incorrect; the DISCOURSE_RELATIVE_URL_ROOT ends in a slash."
translation_overrides: "Some of your translation overrides are out of date. Please check your <a href='%{base_path}/admin/customize/site_texts?outdated=true'>text customizations</a>."
- email_delivery_failures:
- one: "Email delivery has failed once in the past 24 hours. Check the <a href='%{base_path}/admin/email-logs/skipped' target='_blank'>skipped email logs</a> for SMTP error details."
- other: "Email delivery has failed %{count} times in the past 24 hours. Check the <a href='%{base_path}/admin/email-logs/skipped' target='_blank'>skipped email logs</a> for SMTP error details."
+ email_sending_failures:
+ one: "Email sending has failed once in the past 24 hours. Check the <a href='%{base_path}/admin/email-logs/skipped' target='_blank'>skipped email logs</a> for SMTP error details."
+ other: "Email sending has failed %{count} times in the past 24 hours. Check the <a href='%{base_path}/admin/email-logs/skipped' target='_blank'>skipped email logs</a> for SMTP error details."
email_polling_errored_recently:
one: "Email polling has generated an error in the past 24 hours. Look at <a href='%{base_path}/logs' target='_blank'>the logs</a> for more details."
other: "Email polling has generated %{count} errors in the past 24 hours. Look at <a href='%{base_path}/logs' target='_blank'>the logs</a> for more details."
@@ -4147,6 +4147,15 @@ en:
%{raw}
```
+ email_sending_failed: |
+ Sending the message to %{email} failed.
+
+ ### Details
+
+ ``` text
+ %{raw}
+ ```
+
ignored_users_summary:
title: "Ignored User passed threshold"
subject_template: "A user is being ignored by many other users"
diff --git a/lib/email/sender.rb b/lib/email/sender.rb
index a8c558c5592..0202a5d1a89 100644
--- a/lib/email/sender.rb
+++ b/lib/email/sender.rb
@@ -301,7 +301,9 @@ module Email
email_log.smtp_transaction_response = message_response.message&.chomp
end
rescue *SMTP_CLIENT_ERRORS => e
- return skip(SkippedEmailLog.reason_types[:custom], custom_reason: smtp_error_message(e))
+ error_message = smtp_error_message(e)
+ create_failed_email_whisper(topic, error_message)
+ return skip(SkippedEmailLog.reason_types[:custom], custom_reason: error_message)
rescue Net::SMTPError => e
response = e.try(:response)
response = " response: #{response.try(:string) || response}" if response
@@ -513,6 +515,23 @@ module Email
response_string.presence || error.message
end
+ def create_failed_email_whisper(topic, error_message)
+ return if topic.blank? || !topic.private_message?
+ return if SiteSetting.whispers_allowed_groups.blank?
+
+ PostCreator.create!(
+ Discourse.system_user,
+ topic_id: topic.id,
+ post_type: Post.types[:whisper],
+ skip_validations: true,
+ raw: I18n.t("system_messages.email_sending_failed", email: to_address, raw: error_message),
+ )
+ rescue StandardError => error
+ Rails.logger.warn(
+ "Failed to create email sending failure whisper for topic #{topic.id}: #{error.message}",
+ )
+ end
+
def merge_json_x_header(name, value)
data =
begin
diff --git a/spec/lib/email/sender_spec.rb b/spec/lib/email/sender_spec.rb
index 71dc9c06b88..3ecaacabe14 100644
--- a/spec/lib/email/sender_spec.rb
+++ b/spec/lib/email/sender_spec.rb
@@ -1112,5 +1112,44 @@ RSpec.describe Email::Sender do
"552-5.7.0 This message was blocked because its content presents a potential\n552 5.7.0 issue",
)
end
+
+ context "when the email is related to a PM topic" do
+ fab!(:pm_creator, :user)
+ fab!(:pm_target, :user)
+ fab!(:pm_topic) { Fabricate(:private_message_topic, user: pm_creator, recipient: pm_target) }
+ fab!(:pm_post) { Fabricate(:post, topic: pm_topic, user: pm_creator) }
+
+ before do
+ SiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}"
+ message.header["X-Discourse-Post-Id"] = pm_post.id
+ message.header["X-Discourse-Topic-Id"] = pm_topic.id
+ end
+
+ it "creates a staff whisper with the SMTP failure details" do
+ error = Net::SMTPFatalError.new("552-5.7.0 This message was blocked")
+ smtp_response =
+ Net::SMTP::Response.new(
+ "552",
+ "552-5.7.0 This message was blocked because its content presents a potential\n552 5.7.0 issue",
+ )
+ error.define_singleton_method(:response) { smtp_response }
+ message.expects(:deliver!).raises(error)
+
+ expect { email_sender.send }.to change {
+ Post.where(topic_id: pm_topic.id, post_type: Post.types[:whisper]).count
+ }.by(1)
+
+ whisper = Post.where(topic_id: pm_topic.id, post_type: Post.types[:whisper]).last
+ expect(whisper.user).to eq(Discourse.system_user)
+ expect(whisper.raw).to eq(
+ I18n.t(
+ "system_messages.email_sending_failed",
+ email: "[email protected]",
+ raw:
+ "552-5.7.0 This message was blocked because its content presents a potential\n552 5.7.0 issue",
+ ).strip,
+ )
+ end
+ end
end
end
diff --git a/spec/services/problem_check/email_delivery_failures_spec.rb b/spec/services/problem_check/email_sending_failures_spec.rb
similarity index 85%
rename from spec/services/problem_check/email_delivery_failures_spec.rb
rename to spec/services/problem_check/email_sending_failures_spec.rb
index 032090b2942..1057a16b58a 100644
--- a/spec/services/problem_check/email_delivery_failures_spec.rb
+++ b/spec/services/problem_check/email_sending_failures_spec.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-RSpec.describe ProblemCheck::EmailDeliveryFailures do
+RSpec.describe ProblemCheck::EmailSendingFailures do
subject(:check) { described_class.new }
describe ".call" do
@@ -34,7 +34,7 @@ RSpec.describe ProblemCheck::EmailDeliveryFailures do
it do
expect(check).to(
have_a_problem.with_priority("low").with_message(
- "Email delivery has failed once in the past 24 hours. Check the <a href='/admin/email-logs/skipped' target='_blank'>skipped email logs</a> for SMTP error details.",
+ "Email sending has failed once in the past 24 hours. Check the <a href='/admin/email-logs/skipped' target='_blank'>skipped email logs</a> for SMTP error details.",
),
)
end