Files
portal-api/app/Mail/Customer/VmCredentials.php
T
2026-09-23 15:17:18 +07:00

96 lines
3.9 KiB
PHP

<?php
namespace App\Mail\Customer;
use App\Mail\Concerns\RequiresDbTemplate;
use App\Models\Setting;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Attachment;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
/**
* Email thông tin truy cập VPS sau khi khởi tạo — render từ Template DB
* (scenario=vm_credentials), thay cho blade tĩnh send-info-vps trước đây.
*/
class VmCredentials extends Mailable
{
use Queueable, SerializesModels, RequiresDbTemplate;
public function __construct(
public array $data,
public readonly ?string $invoicePdf = null,
public readonly ?string $invoicePdfFileName = null,
) {}
public function envelope(): Envelope
{
return new Envelope(
subject: $this->renderFromTemplate('vm_credentials', $this->vars())['subject'],
);
}
public function content(): Content
{
return new Content(
htmlString: $this->renderFromTemplate('vm_credentials', $this->vars())['html'],
);
}
public function attachments(): array
{
if ($this->invoicePdf !== null && $this->invoicePdfFileName !== null) {
return [
Attachment::fromData(fn () => $this->invoicePdf, $this->invoicePdfFileName)
->withMime('application/pdf'),
];
}
return [];
}
private function payload(): array
{
try {
$setting = Setting::where('key', 'setting')->first();
$settingValue = $setting ? json_decode((string) $setting->value, true) : [];
} catch (\Throwable) {
$settingValue = [];
}
return array_merge([
'support_email' => $settingValue['email'] ?? config('mail.support_email', 'support@thegioiso.com'),
'support_phone' => $settingValue['hotline'] ?? ($settingValue['phone'] ?? config('mail.support_phone', '1900.xxxx')),
'login_url' => rtrim(env('FRONTEND_URL', config('app.url')), '/') . '/dang-nhap',
], $this->data);
}
private function vars(): array
{
$data = $this->payload();
$row = fn (string $label, ?string $value) => $value
? "<tr bgcolor=\"#FFF1E6\"><td style=\"padding: 7px 11px; border-right: 1px solid #e8e8e8; width: 200px;\">{$label}</td><td style=\"padding: 7px 11px; border-right: 1px solid #e8e8e8;\"><strong>{$value}</strong></td></tr>"
: '';
return [
'app_name' => $data['company_name'] ?? config('app.name', 'The Gioi So'),
'full_name' => $data['client_name'] ?? 'Quý khách',
'product_name' => $data['product_name'] ?? 'Cloud VPS',
'product_billing_row' => $row('Chu kỳ thanh toán:', $data['product_billing'] ?? null),
'product_price_row' => $row('Giá dịch vụ:', !empty($data['product_price']) ? $data['product_price'] . ' VNĐ' : null),
'os_name_row' => $row('Hệ điều hành:', $data['os_name'] ?? null),
'service_host' => $data['service_host'] ?? '',
'service_username' => $data['service_username'] ?? '',
'service_password' => $data['service_password'] ?? '',
'service_port_row' => $row('Cổng kết nối:', $data['service_port'] ?? null),
'service_due_exp_row' => $row('Hạn sử dụng:', $data['service_due_exp'] ?? null),
'login_url' => $data['login_url'] ?? rtrim(env('FRONTEND_URL', config('app.url')), '/') . '/dang-nhap',
'support_email' => $data['support_email'] ?? config('mail.support_email', 'support@thegioiso.com'),
'support_phone' => $data['support_phone'] ?? config('mail.support_phone', '1900.xxxx'),
];
}
}