Files
portal-api/app/Providers/MailConfigServiceProvider.php
T
2025-07-28 15:34:15 +07:00

56 lines
1.6 KiB
PHP

<?php
namespace App\Providers;
use App\Http\Facade\SignRSA;
use App\Models\Setting;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
class MailConfigServiceProvider extends ServiceProvider
{
public function register()
{
//
}
public function boot()
{
// 🔐 Không làm gì nếu đang chạy trong CLI (migrate, cache:clear, ...)
if (App::runningInConsole()) {
return;
}
// ⛔ Tránh lỗi nếu database hoặc bảng `setting` chưa tồn tại
if (!Schema::hasTable('setting')) {
return;
}
// ✅ Truy vấn an toàn
$mail = Setting::where('key', 'smtp')->first();
$setting = Setting::where('key', 'setting')->first();
if ($mail && $setting) {
$mail = json_decode($mail->value);
$company = json_decode($setting->value)->company ?? 'Your Company';
$config = [
'driver' => $mail->transport ?? 'smtp',
'host' => $mail->host ?? '',
'port' => $mail->port ?? 587,
'from' => [
'address' => $mail->user ?? 'no-reply@example.com',
'name' => $company,
],
'encryption' => $mail->encryption === 0 ? 'tls' : $mail->encryption,
'username' => $mail->user ?? '',
'password' => isset($mail->password) ? SignRSA::DecryptFunc($mail->password) : '',
];
Config::set('mail', $config);
}
}
}