- TenantMailConfigController: 목록, 편집, 저장, SMTP 테스트 API - TenantMailConfig, MailLog 모델 추가 - SmtpConnectionTester: SMTP 연결 테스트 서비스 (에러 코드, 트러블슈팅) - TenantMailService: 테넌트 설정 기반 메일 발송 (쿼터, Fallback) - config/mail-presets.php: Gmail/Naver/MS365 등 8개 SMTP 프리셋 - Blade 뷰: 테넌트 목록 현황 + 설정 폼 (프리셋 자동 채움, 연결 테스트) - 라우트 추가: /system/tenant-mail/*
156 lines
3.5 KiB
PHP
156 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Tenants;
|
|
|
|
use App\Traits\BelongsToTenant;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class TenantMailConfig extends Model
|
|
{
|
|
use BelongsToTenant, SoftDeletes;
|
|
|
|
protected $fillable = [
|
|
'tenant_id',
|
|
'provider',
|
|
'from_address',
|
|
'from_name',
|
|
'reply_to',
|
|
'is_verified',
|
|
'daily_limit',
|
|
'is_active',
|
|
'options',
|
|
'created_by',
|
|
'updated_by',
|
|
'deleted_by',
|
|
];
|
|
|
|
protected $casts = [
|
|
'is_verified' => 'boolean',
|
|
'daily_limit' => 'integer',
|
|
'is_active' => 'boolean',
|
|
'options' => 'array',
|
|
];
|
|
|
|
// Options 키 상수
|
|
public const OPTION_SMTP = 'smtp';
|
|
|
|
public const OPTION_PRESET = 'preset';
|
|
|
|
public const OPTION_BRANDING = 'branding';
|
|
|
|
public const OPTION_CONNECTION_TEST = 'connection_test';
|
|
|
|
public function tenant(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Tenant::class);
|
|
}
|
|
|
|
public function getOption(string $key, mixed $default = null): mixed
|
|
{
|
|
return data_get($this->options, $key, $default);
|
|
}
|
|
|
|
public function setOption(string $key, mixed $value): static
|
|
{
|
|
$options = $this->options ?? [];
|
|
data_set($options, $key, $value);
|
|
$this->options = $options;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getSmtpHost(): ?string
|
|
{
|
|
return $this->getOption('smtp.host');
|
|
}
|
|
|
|
public function getSmtpPort(): int
|
|
{
|
|
return (int) $this->getOption('smtp.port', 587);
|
|
}
|
|
|
|
public function getSmtpEncryption(): string
|
|
{
|
|
return $this->getOption('smtp.encryption', 'tls');
|
|
}
|
|
|
|
public function getSmtpUsername(): ?string
|
|
{
|
|
return $this->getOption('smtp.username');
|
|
}
|
|
|
|
public function getSmtpPassword(): ?string
|
|
{
|
|
$encrypted = $this->getOption('smtp.password');
|
|
if (! $encrypted) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
return decrypt($encrypted);
|
|
} catch (\Exception $e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public function getPreset(): ?string
|
|
{
|
|
return $this->getOption('preset');
|
|
}
|
|
|
|
public function getPresetLabel(): string
|
|
{
|
|
$preset = $this->getPreset();
|
|
if (! $preset) {
|
|
return '-';
|
|
}
|
|
|
|
$presets = config('mail-presets', []);
|
|
|
|
return $presets[$preset]['label'] ?? $preset;
|
|
}
|
|
|
|
public function getProviderLabel(): string
|
|
{
|
|
return match ($this->provider) {
|
|
'platform' => 'SAM 기본',
|
|
'smtp' => '자체 SMTP',
|
|
'ses' => 'Amazon SES',
|
|
'mailgun' => 'Mailgun',
|
|
default => $this->provider,
|
|
};
|
|
}
|
|
|
|
public function getStatusLabel(): string
|
|
{
|
|
if (! $this->is_active) {
|
|
return '비활성';
|
|
}
|
|
|
|
$lastTest = $this->getOption('connection_test.last_result');
|
|
|
|
return match ($lastTest) {
|
|
'success' => '활성',
|
|
'failed' => '연결 오류',
|
|
default => $this->provider === 'platform' ? '활성' : '미테스트',
|
|
};
|
|
}
|
|
|
|
public function getStatusColor(): string
|
|
{
|
|
if (! $this->is_active) {
|
|
return 'gray';
|
|
}
|
|
|
|
$lastTest = $this->getOption('connection_test.last_result');
|
|
|
|
return match ($lastTest) {
|
|
'success' => 'green',
|
|
'failed' => 'red',
|
|
default => $this->provider === 'platform' ? 'green' : 'yellow',
|
|
};
|
|
}
|
|
}
|