From 1de8361d2e4309df097d33381d885df9cbbd3b93 Mon Sep 17 00:00:00 2001 From: David Battersby Date: Thu, 18 May 2023 10:39:37 +0800 Subject: [PATCH] FIX: Prevent Email Processor errors when mail is blank or nil (#21292) Currently processing emails that are blank or have a nil value for the mail will cause several errors. This update allows emails with blank body or missing sender to log the blank email error to the mail logs rather than throwing an error. --- lib/email/processor.rb | 4 ++-- spec/lib/email/processor_spec.rb | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/email/processor.rb b/lib/email/processor.rb index 9c807fc2b8e..30c50c2baef 100644 --- a/lib/email/processor.rb +++ b/lib/email/processor.rb @@ -24,7 +24,7 @@ module Email raise end rescue => e - return handle_bounce(e) if @receiver.is_bounce? + return handle_bounce(e) if @receiver&.is_bounce? log_email_process_failure(@mail, e) incoming_email = @receiver.try(:incoming_email) @@ -156,7 +156,7 @@ module Email end def can_send_rejection_email?(email, type) - return false if @receiver.sent_to_mailinglist_mirror? + return false if @receiver&.sent_to_mailinglist_mirror? return true if type == :email_reject_unrecognized_error key = "rejection_email:#{email}:#{type}:#{Date.today}" diff --git a/spec/lib/email/processor_spec.rb b/spec/lib/email/processor_spec.rb index 4f57124145f..2d0d797b495 100644 --- a/spec/lib/email/processor_spec.rb +++ b/spec/lib/email/processor_spec.rb @@ -48,6 +48,13 @@ RSpec.describe Email::Processor do end end + describe "when mail is not set" do + it "does not raise an error" do + expect { Email::Processor.process!(nil) }.not_to raise_error + expect { Email::Processor.process!("") }.not_to raise_error + end + end + describe "rate limits" do let(:mail) { "From: #{from}\nTo: bar@foo.com\nSubject: FOO BAR\n\nFoo foo bar bar?" } let(:limit_exceeded) { RateLimiter::LimitExceeded.new(10) }