FIX: Detect SNS notifications for SES correctly (#7284)

FIX: Detect SNS notifications for SES correctly (#7284)

  • Fix handling SNS notifications for AWS SES

This fixes detection of email bounce by:

  • removing hard requirement for email ID, ID in webhook msg never equals this in email_log
  • gets bounce_score from user stats instead of nonexistent field in webhook msg
  • Remove empty line

  • Prettify access to EmailLog for parsing SNS notification

Co-Authored-By: SystemZ SystemZ@users.noreply.github.com

diff --git a/app/jobs/regular/process_sns_notification.rb b/app/jobs/regular/process_sns_notification.rb
index ab26c25..fb8447d 100644
--- a/app/jobs/regular/process_sns_notification.rb
+++ b/app/jobs/regular/process_sns_notification.rb
@@ -22,11 +22,11 @@ module Jobs
       return unless Aws::SNS::MessageVerifier.new.authentic?(raw)
 
       message.dig("bounce", "bouncedRecipients").each do |r|
-        if email_log = EmailLog.find_by(message_id: message_id, to_address: r["emailAddress"])
+        if email_log = EmailLog.order("created_at DESC").where(to_address: r["emailAddress"]).first
           email_log.update_columns(bounced: true)
 
           if email_log.user&.email.present?
-            if r["status"]&.start_with?["4."] || bounce_type == "Transient"
+            if email_log.user.user_stat.bounce_score.to_s.start_with?("4.") || bounce_type == "Transient"
               Email::Receiver.update_bounce_score(email_log.user.email, SiteSetting.soft_bounce_score)
             else
               Email::Receiver.update_bounce_score(email_log.user.email, SiteSetting.hard_bounce_score)

GitHub sha: 8f7cfc29

@ZogStriP fyi I merged this, maybe you wanted to have a quick look as well.

Yup that’s why I did not approve it. I need to test this properly.

1 Like