From 913edb706a7b51d8176bceddccd9fc1d21c4fa2e Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 18 Nov 2023 16:45:00 +1100 Subject: [PATCH] Better logging for bounced emails --- app/Jobs/Mail/NinjaMailerJob.php | 17 ++++++++++++++++- app/Services/Email/Email.php | 22 +++++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/app/Jobs/Mail/NinjaMailerJob.php b/app/Jobs/Mail/NinjaMailerJob.php index 4ecc5bc456..3b8211f4ba 100644 --- a/app/Jobs/Mail/NinjaMailerJob.php +++ b/app/Jobs/Mail/NinjaMailerJob.php @@ -24,6 +24,7 @@ use App\Models\Invoice; use App\Models\Payment; use App\Models\SystemLog; use App\Models\User; +use App\Repositories\ActivityRepository; use App\Utils\Ninja; use App\Utils\Traits\MakesHash; use GuzzleHttp\Exception\ClientException; @@ -164,7 +165,7 @@ class NinjaMailerJob implements ShouldQueue * this merges a text string with a json object * need to harvest the ->Message property using the following */ - if (stripos($e->getMessage(), 'code 406') || stripos($e->getMessage(), 'code 300') || stripos($e->getMessage(), 'code 413')) { + if (stripos($e->getMessage(), 'code 300') || stripos($e->getMessage(), 'code 413')) { $message = "Either Attachment too large, or recipient has been suppressed."; $this->fail(); @@ -174,6 +175,20 @@ class NinjaMailerJob implements ShouldQueue return; } + if (stripos($e->getMessage(), 'code 406')) { + + $email = $this->nmo->to_user->email ?? ''; + + $message = "Recipient {$email} has been suppressed and cannot receive emails from you."; + + $this->fail(); + $this->logMailError($message, $this->company->clients()->first()); + $this->cleanUpMailers(); + + return; + } + + //only report once, not on all tries if ($this->attempts() == $this->tries) { /* If the is an entity attached to the message send a failure mailer */ diff --git a/app/Services/Email/Email.php b/app/Services/Email/Email.php index 4427a16759..7bcc488f50 100644 --- a/app/Services/Email/Email.php +++ b/app/Services/Email/Email.php @@ -272,11 +272,27 @@ class Email implements ShouldQueue nlog("Mailer failed with {$e->getMessage()}"); $message = $e->getMessage(); - if (stripos($e->getMessage(), 'code 406') || stripos($e->getMessage(), 'code 300') || stripos($e->getMessage(), 'code 413')) { - $message = "Either Attachment too large, or recipient has been suppressed."; + + if (stripos($e->getMessage(), 'code 300') || stripos($e->getMessage(), 'code 413')) { + $message = "Either Attachment too large, or recipient has been suppressed."; + + $this->fail(); + $this->logMailError($e->getMessage(), $this->company->clients()->first()); + $this->cleanUpMailers(); + + return; + } + + if (stripos($e->getMessage(), 'code 406')) { + + $address_object = reset($this->email_object->to); + + $email = $address_object->address ?? ''; + + $message = "Recipient {$email} has been suppressed and cannot receive emails from you."; $this->fail(); - $this->logMailError($e->getMessage(), $this->company->clients()->first()); + $this->logMailError($message, $this->company->clients()->first()); $this->cleanUpMailers(); return;