[fix] Check if the attachments files exists before you send a mail.

This commit is contained in:
guillaume91 2024-06-17 16:38:16 +02:00
parent 2d532d5aa8
commit c237b2ebf2
2 changed files with 11 additions and 6 deletions

View file

@ -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));

View file

@ -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();
}
}