From c237b2ebf2476ef45616f324edf4625563d7933f Mon Sep 17 00:00:00 2001 From: guillaume91 Date: Mon, 17 Jun 2024 16:38:16 +0200 Subject: [PATCH] [fix] Check if the attachments files exists before you send a mail. --- laravel_app/app/Livewire/Forms/MailingForm.php | 1 - laravel_app/app/Mail/ReportMailer.php | 16 +++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/laravel_app/app/Livewire/Forms/MailingForm.php b/laravel_app/app/Livewire/Forms/MailingForm.php index 34ae986..5ad428e 100644 --- a/laravel_app/app/Livewire/Forms/MailingForm.php +++ b/laravel_app/app/Livewire/Forms/MailingForm.php @@ -52,7 +52,6 @@ public static function saveAndSendMailing($report, $subject, $message, $recipien ]); $mailing->recipients()->createMany($recipients); - Mail::to($mailing->recipients()->pluck('email')->toArray()) ->send(new \App\Mail\ReportMailer($mailing)); diff --git a/laravel_app/app/Mail/ReportMailer.php b/laravel_app/app/Mail/ReportMailer.php index a333bdc..05e60a8 100644 --- a/laravel_app/app/Mail/ReportMailer.php +++ b/laravel_app/app/Mail/ReportMailer.php @@ -12,6 +12,7 @@ use Illuminate\Mail\Mailables\Content; use Illuminate\Mail\Mailables\Envelope; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Storage; class ReportMailer extends Mailable @@ -26,9 +27,7 @@ class ReportMailer extends Mailable public function __construct(ProjectMailing $mailing) { $this->mailing = $mailing; - $this->withSwiftMessage(function ($message) use ($mailing) { - $message->getHeaders()->addTextHeader('X-Mailing-ID', $mailing->id); - }); + } /** @@ -68,16 +67,23 @@ public function attachments(): array return $this->mailing->attachments()->get()->map(function (ProjectMailingAttachment $attachment) { $mime = 'application/pdf'; // default MIME type $extension = pathinfo($attachment->path, PATHINFO_EXTENSION); + $attachment_path = $attachment->mailing->project->download_path."/".$attachment->path; + + if (!File::exists($attachment_path)) { + return null; + } if ($extension === 'docx') { $mime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; } return Attachment::fromStorage( - path: $attachment->mailing->project->download_path."/".$attachment->path + path: $attachment_path ) ->as($attachment->path) ->withMime($mime); - })->toArray(); + }) + ->filter() + ->toArray(); } }