Files
portal-api/app/Mail/GenericTemplateMail.php
T
2026-09-09 16:56:22 +07:00

48 lines
1.5 KiB
PHP

<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
/**
* Mailable dùng chung cho mọi email render từ Template (EmailTemplateService).
* Không hardcode from/replyTo — lấy từ mailConfig của template, null thì Laravel
* tự fallback về config('mail.from') mặc định.
*/
class GenericTemplateMail extends Mailable
{
use Queueable, SerializesModels;
public function __construct(
public readonly string $renderedSubject,
public readonly string $renderedHtml,
public readonly array $mailConfig = [],
) {}
public function envelope(): Envelope
{
return new Envelope(
subject: $this->renderedSubject,
from: $this->address($this->mailConfig['from_email'] ?? null, $this->mailConfig['from_name'] ?? null),
replyTo: array_filter([$this->address($this->mailConfig['reply_to'] ?? null, $this->mailConfig['reply_to_name'] ?? null)]),
cc: $this->mailConfig['cc'] ?? [],
bcc: $this->mailConfig['bcc'] ?? [],
);
}
public function content(): Content
{
return new Content(htmlString: $this->renderedHtml);
}
private function address(?string $email, ?string $name): ?Address
{
return $email ? new Address($email, $name) : null;
}
}