Files
portal-api/app/Mail/VmCredentialsMail.php
baodev0 520f21cb11 feat: add GeneratesUniqueSlug class for unique slug generation
feat: create PostCategoryFactory for generating post category data

feat: create PostFactory for generating post data

feat: add retry_requested_at column to services table

feat: add vcenter provisioning fields to products table

feat: add module_host_id to os table

feat: create email_notification_templates table for email templates

feat: add scenario and format fields to template table

feat: add admin fields to transactions table

fix: update ticket table columns and types

feat: add uuid to ticket table and backfill existing records

fix: make ticket_reply user_id nullable

feat: create notifications table for user notifications

fix: make ticket uuid nullable

feat: create post_categories table for blog categories

feat: create posts table for blog posts

feat: add invoice email template for PDF generation
2026-05-25 15:39:42 +07:00

73 lines
2.4 KiB
PHP

<?php
namespace App\Mail;
use App\Models\EmailNotificationTemplate;
use App\Models\Invoice;
use App\Services\EmailNotificationTemplateRenderer;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Attachment;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Support\Facades\Log;
/**
* Gửi thông tin VPS + đính kèm PDF hoá đơn.
*
* KHÔNG implement ShouldQueue — mailable này được gọi từ bên trong
* SendInformationVirtualMachine (đã là queued job). Gửi đồng bộ để
* tránh Laravel cố JSON-encode $invoicePdf (binary PDF).
*/
class VmCredentialsMail extends Mailable
{
public function __construct(
public readonly EmailNotificationTemplate $template,
public readonly array $emailData,
public readonly ?Invoice $invoice,
public readonly ?string $invoicePdf,
public readonly ?string $invoicePdfFileName,
) {}
public function envelope(): Envelope
{
$subject = str_replace(
['{{os_name}}', '{{client_name}}', '{{company_name}}'],
[
$this->emailData['os_name'] ?? '',
$this->emailData['client_name'] ?? '',
$this->emailData['company_name'] ?? '',
],
$this->template->subject
);
return new Envelope(subject: $subject);
}
public function content(): Content
{
try {
$renderer = app(EmailNotificationTemplateRenderer::class);
$html = $renderer->renderVmCredentials($this->template, $this->emailData, $this->invoice);
return new Content(htmlString: $html);
} catch (\Throwable $e) {
Log::warning('VmCredentialsMail: renderer failed, falling back to plain message', [
'template_id' => $this->template->id,
'error' => $e->getMessage(),
]);
return new Content(htmlString: '<p>Thông tin dịch vụ đã được chuẩn bị. Vui lòng liên hệ hỗ trợ nếu cần.</p>');
}
}
public function attachments(): array
{
if ($this->invoicePdf !== null && $this->invoicePdfFileName !== null) {
return [
Attachment::fromData(fn() => $this->invoicePdf, $this->invoicePdfFileName)
->withMime('application/pdf'),
];
}
return [];
}
}