86 lines
2.5 KiB
PHP
86 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Http\Services\SignRSA;
|
|
use App\Models\Setting;
|
|
use App\Support\Cache\CacheKey;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
class MailConfigServiceProvider extends ServiceProvider
|
|
{
|
|
private static ?array $resolved = null;
|
|
|
|
public function register()
|
|
{
|
|
//
|
|
}
|
|
|
|
public function boot()
|
|
{
|
|
if (App::runningInConsole()) {
|
|
return;
|
|
}
|
|
|
|
if (self::$resolved !== null) {
|
|
Config::set('mail', self::$resolved);
|
|
return;
|
|
}
|
|
|
|
$config = Cache::remember(CacheKey::build('settings', 'mail'), 3600, function () {
|
|
if (!Schema::hasTable('setting')) {
|
|
return null;
|
|
}
|
|
|
|
$mail = Setting::where('key', 'smtp')->first();
|
|
$setting = Setting::where('key', 'setting')->first();
|
|
|
|
if (!$mail || !$setting) {
|
|
return null;
|
|
}
|
|
|
|
$mail = json_decode($mail->value);
|
|
$company = json_decode($setting->value)->company ?? 'Your Company';
|
|
|
|
$password = '';
|
|
if (isset($mail->password)) {
|
|
$decrypted = (new SignRSA())->DecryptFunc($mail->password);
|
|
$password = $decrypted ?: $mail->password;
|
|
}
|
|
|
|
$transport = $mail->transport ?? 'smtp';
|
|
$encryption = $mail->encryption ?? config('mail.mailers.smtp.encryption');
|
|
|
|
if ($encryption === 0 || $encryption === '0' || $encryption === 'starttls') {
|
|
$encryption = 'tls';
|
|
}
|
|
|
|
$config = config('mail');
|
|
$config['default'] = $transport;
|
|
$config['from'] = [
|
|
'address' => $mail->from_address ?? $mail->user ?? config('mail.from.address', 'no-reply@example.com'),
|
|
'name' => $mail->from_name ?? $company,
|
|
];
|
|
$config['mailers'][$transport] = array_merge($config['mailers'][$transport] ?? [], [
|
|
'transport' => $transport,
|
|
'host' => $mail->host ?? '',
|
|
'port' => $mail->port ?? 587,
|
|
'encryption' => $encryption,
|
|
'username' => $mail->user ?? '',
|
|
'password' => $password,
|
|
]);
|
|
|
|
return $config;
|
|
});
|
|
|
|
if ($config) {
|
|
self::$resolved = $config;
|
|
Config::set('mail', $config);
|
|
}
|
|
}
|
|
}
|