added mailer and boot method create on project mailing

This commit is contained in:
Martin Folkerts 2024-01-31 21:13:39 +01:00
parent 3a18e3b8a7
commit 540e9113dd
5 changed files with 39 additions and 8 deletions

View file

@ -35,6 +35,5 @@ public function download()
public function createMailing() {
$this->mailingForm->save();
$this->reset('createMailingModal');
}
}

View file

@ -2,23 +2,30 @@
namespace App\Mail;
use App\Models\ProjectMailing;
use App\Models\ProjectMailingAttachment;
use App\Models\ProjectMosaic;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Attachment;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
class ReportMailer extends Mailable
{
use Queueable, SerializesModels;
public ProjectMailing $mailing;
/**
* Create a new message instance.
*/
public function __construct()
public function __construct(ProjectMailing $mailing)
{
//
$this->mailing = $mailing;
}
/**
@ -27,7 +34,7 @@ public function __construct()
public function envelope(): Envelope
{
return new Envelope(
subject: 'Report Mailer',
subject: $this->mailing->subject,
);
}
@ -37,9 +44,15 @@ public function envelope(): Envelope
public function content(): Content
{
return new Content(
view: 'view.name',
htmlString: $this->mailing->message,
);
}
public function build(): self
{
$this->to($this->mailing->recipients()->pluck('email')->toArray());
return $this;
}
/**
* Get the attachments for the message.
@ -48,6 +61,14 @@ public function content(): Content
*/
public function attachments(): array
{
return [];
return $this->mailing->attachments()->get()->map(function (ProjectMailingAttachment $attachment) {
$projectPath = $attachment->mailing()->project()->first()->path;
return Attachment::fromStorage(
path: $projectPath.$attachment->path
)
->as($attachment->name)
->withMime('application/pdf');
})->toArray();
}
}

View file

@ -17,12 +17,19 @@ class ProjectMailing extends Model
'message',
];
protected static function booted()
{
static::created(function ($projectMailing) {
Mail::send(new \App\Mail\ReportMailer($projectMailing));
});
}
public function addAttachment($name, UploadedFile $file)
{
$prefix = Str::random(10);
$originalFileName = $file->getClientOriginalName();
$extension = pathinfo($originalFileName, PATHINFO_EXTENSION);
$newFileName = $prefix . '_' . pathinfo($originalFileName, PATHINFO_FILENAME) . '.' . $extension;
$newFileName = $prefix.'_'.pathinfo($originalFileName, PATHINFO_FILENAME).'.'.$extension;
$path = Storage::disk('local')->putFileAs(
$this->project->attachmentPath, $file, $newFileName

View file

@ -13,4 +13,9 @@ class ProjectMailingAttachment extends Model
'name',
'path',
];
public function mailing()
{
return $this->belongsTo(ProjectMailing::class);
}
}

View file

@ -22,7 +22,6 @@
</x-menu.button>
<x-menu.items>
<x-menu.close>
<x-menu.item wire:click="download">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"