特性:增加创始人专享的在线 SMTP 配置系统并在 AppServiceProvider 中动态加载以拦截系统发件

This commit is contained in:
2026-02-27 09:47:47 +08:00
parent 5e6101483d
commit baae2cc26f
6 changed files with 269 additions and 2 deletions

View File

@@ -3,6 +3,9 @@
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Models\Sysparam;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
@@ -19,6 +22,32 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot(): void
{
//
// 动态加载自定义 SMTP 配置 (如果有数据库则执行)
try {
if (Schema::hasTable('sysparam')) {
$smtpConfig = Sysparam::where('alias', 'like', 'smtp_%')->pluck('body', 'alias');
if ($smtpConfig->isNotEmpty() && $smtpConfig->get('smtp_host')) {
Config::set('mail.default', 'smtp');
Config::set('mail.mailers.smtp', [
'transport' => 'smtp',
'host' => $smtpConfig->get('smtp_host'),
'port' => $smtpConfig->get('smtp_port', 465),
'encryption' => $smtpConfig->get('smtp_encryption', 'ssl'),
'username' => $smtpConfig->get('smtp_username'),
'password' => $smtpConfig->get('smtp_password'),
'timeout' => null,
'local_domain' => env('MAIL_EHLO_DOMAIN'),
]);
Config::set('mail.from', [
'address' => $smtpConfig->get('smtp_from_address', $smtpConfig->get('smtp_username')),
'name' => $smtpConfig->get('smtp_from_name', '飘落流星聊天室'),
]);
}
}
} catch (\Exception $e) {
// 在安装初期表不存在时忽略,防止应用崩溃
}
}
}