57 lines
1.4 KiB
PHP
57 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Forms;
|
|
|
|
use App\Models\ProjectMailing;
|
|
use App\Models\ProjectReport;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Livewire\Attributes\Rule;
|
|
use Livewire\Form;
|
|
|
|
class MailingForm extends Form
|
|
{
|
|
#[Rule('required|min:3')]
|
|
public string $subject = '';
|
|
|
|
#[Rule('required')]
|
|
public string $message = '';
|
|
|
|
#[Rule('required')]
|
|
public array $recipients = [];
|
|
|
|
#[Rule('required')]
|
|
public ProjectReport $report;
|
|
|
|
public function setReport(ProjectReport $report)
|
|
{
|
|
$this->report = $report;
|
|
$this->subject = $report->project->mail_subject;
|
|
$this->message = $report->project->mail_template;
|
|
$this->recipients = $this->report->project->emailRecipients()->get(['email', 'name'])->toArray();
|
|
}
|
|
|
|
public function save()
|
|
{
|
|
$this->validate();
|
|
|
|
$mailing = $this->report->project->mailings()->create([
|
|
'subject' => $this->subject,
|
|
'message' => $this->message,
|
|
]);
|
|
|
|
$mailing->attachments()->create([
|
|
'name' => $this->report->name,
|
|
'path' => $this->report->path,
|
|
]);
|
|
|
|
$mailing->recipients()->createMany($this->recipients);
|
|
|
|
$this->setReport($this->report);
|
|
|
|
|
|
Mail::to($mailing->recipients()->pluck('email')->toArray())
|
|
->send(new \App\Mail\ReportMailer($mailing));
|
|
|
|
}
|
|
}
|