[fix] Check if the attachments files exists before you send a mail.
This commit is contained in:
parent
2d532d5aa8
commit
c237b2ebf2
|
|
@ -52,7 +52,6 @@ public static function saveAndSendMailing($report, $subject, $message, $recipien
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$mailing->recipients()->createMany($recipients);
|
$mailing->recipients()->createMany($recipients);
|
||||||
|
|
||||||
Mail::to($mailing->recipients()->pluck('email')->toArray())
|
Mail::to($mailing->recipients()->pluck('email')->toArray())
|
||||||
->send(new \App\Mail\ReportMailer($mailing));
|
->send(new \App\Mail\ReportMailer($mailing));
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@
|
||||||
use Illuminate\Mail\Mailables\Content;
|
use Illuminate\Mail\Mailables\Content;
|
||||||
use Illuminate\Mail\Mailables\Envelope;
|
use Illuminate\Mail\Mailables\Envelope;
|
||||||
use Illuminate\Queue\SerializesModels;
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Illuminate\Support\Facades\File;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
||||||
class ReportMailer extends Mailable
|
class ReportMailer extends Mailable
|
||||||
|
|
@ -26,9 +27,7 @@ class ReportMailer extends Mailable
|
||||||
public function __construct(ProjectMailing $mailing)
|
public function __construct(ProjectMailing $mailing)
|
||||||
{
|
{
|
||||||
$this->mailing = $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) {
|
return $this->mailing->attachments()->get()->map(function (ProjectMailingAttachment $attachment) {
|
||||||
$mime = 'application/pdf'; // default MIME type
|
$mime = 'application/pdf'; // default MIME type
|
||||||
$extension = pathinfo($attachment->path, PATHINFO_EXTENSION);
|
$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') {
|
if ($extension === 'docx') {
|
||||||
$mime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
|
$mime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
|
||||||
}
|
}
|
||||||
|
|
||||||
return Attachment::fromStorage(
|
return Attachment::fromStorage(
|
||||||
path: $attachment->mailing->project->download_path."/".$attachment->path
|
path: $attachment_path
|
||||||
)
|
)
|
||||||
->as($attachment->path)
|
->as($attachment->path)
|
||||||
->withMime($mime);
|
->withMime($mime);
|
||||||
})->toArray();
|
})
|
||||||
|
->filter()
|
||||||
|
->toArray();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue