Files
portal-api/app/Support/MailSettings.php
T
baodev0andClaude Opus 5.5 40bf6b54f9 feat(admin): star/tags marks, service product-type filter, campaign bulk actions, SMTP test
- Star (is_starred) and colored tags for services, customers, orders, invoices:
  Markable trait, tags/taggables tables, PATCH administrator/marks/{type}/{key},
  tag CRUD, list filters starred / tag_ids[]
- GET administrator/services accepts product_type (falls back to product type
  for legacy rows without services.type)
- Mail campaigns: bulk cancel / retry failed / delete; cancelled campaigns are
  skipped by SendMailCampaignRecipient
- POST administrator/settings/test-smtp sends a test email through the saved
  mail transport

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 21:53:41 +07:00

106 lines
3.2 KiB
PHP

<?php
namespace App\Support;
use App\Http\Services\SignRSA;
use App\Models\Setting;
use App\Support\Cache\CacheKey;
use Illuminate\Mail\MailManager;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Schema;
final class MailSettings
{
private const TTL_SECONDS = 3600;
private const NON_DELIVERING_MAILERS = ['array', 'log'];
public static function cacheKey(): string
{
return CacheKey::build('settings', 'smtp');
}
public static function apply(): void
{
$settings = self::resolve();
if ($settings === null) {
return;
}
$config = config('mail');
if (!in_array($config['default'], self::NON_DELIVERING_MAILERS, true)) {
$config['default'] = $settings['transport'];
}
$config['from'] = [
'address' => $settings['from_address'] ?: ($config['from']['address'] ?? null) ?: 'no-reply@example.com',
'name' => $settings['from_name'],
];
$config['mailers'][$settings['transport']] = array_merge(
$config['mailers'][$settings['transport']] ?? [],
$settings['mailer'],
);
Config::set('mail', $config);
}
public static function transport(): ?string
{
return self::resolve()['transport'] ?? null;
}
public static function refresh(object $mailer): void
{
self::apply();
if ($mailer instanceof MailManager) {
$mailer->forgetMailers();
}
}
private static function resolve(): ?array
{
return Cache::remember(self::cacheKey(), self::TTL_SECONDS, 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 ?? null) ?: 'smtp';
$encryption = $mail->encryption ?? null;
if ($encryption === 0 || $encryption === '0' || $encryption === 'starttls') {
$encryption = 'tls';
}
return [
'transport' => $transport,
'from_address' => ($mail->from_address ?? null) ?: ($mail->user ?? null),
'from_name' => ($mail->from_name ?? null) ?: $company,
'mailer' => array_filter([
'transport' => $transport,
'host' => $mail->host ?? '',
'port' => $mail->port ?? 587,
'encryption' => $encryption,
'username' => $mail->user ?? '',
'password' => $password,
], fn ($value) => $value !== null),
];
});
}
}