request = $request; $this->template_alias = $template_alias; } public function handle(): void { $document = Document::find($this->request->get('document_id')); event(new DocumentSending($document)); $mail_request = $this->request->only(['to', 'subject', 'body']); if ($this->request->get('user_email', false)) { $mail_request['cc'] = user()->email; } $attachments = collect($this->request->get('attachments', [])) ->filter(fn($value) => $value == true) ->keys() ->all(); $attach_pdf = in_array('pdf', $attachments); $notification = config('type.document.' . $document->type . '.notification.class'); $contacts = $document->contact->withPersons(); $counter = 1; foreach ($contacts as $contact) { if (! in_array($contact->email, $mail_request['to'])) { continue; } $custom_mail = [ 'subject' => $mail_request['subject'], 'body' => $mail_request['body'], ]; if (($counter == 1) && ! empty($mail_request['cc'])) { $custom_mail['cc'] = $mail_request['cc']; } $contact->notify(new $notification($document, $this->template_alias, $attach_pdf, $custom_mail, $attachments)); $counter++; } event(new DocumentSent($document)); } }