Files
portal-api/app/Support/MailSettings.php
T
baodev0andClaude Opus 5.5 46aa138eec feat(admin): IP pool allocation, billing exports, service attachments
- IP pool: sheet import (xlsx/csv) matched by address, bulk allocate/revoke
  with notification email (ip_allocated/ip_revoked), ip_pool_logs history.
- Orders/invoices: Excel export on branded template (BrandedReportWriter),
  shared billing filters, order item service info in list responses.
- Web design quote Excel export; activity log export.
- Service attachments with copy-to-invoice; deployment invoice attachments.
- Customer email attachments stored on disk and attached by path.
- Admin password change endpoint; user.fullname backfill migration;
  orders.user_id backfill from logs.
- Disable deploy workflow until CI/CD is set up.
- Ignore stored customer email and service attachment uploads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 19:58:36 +07:00

101 lines
3.1 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 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),
];
});
}
}