feat: add mail_accounts migration and MailAccount model

This commit is contained in:
Ghassan Yusuf 2026-05-26 12:24:44 +03:00
parent 33c0a5eb73
commit bbc06738e9
2 changed files with 73 additions and 0 deletions

View File

@ -0,0 +1,45 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use PromoSeven\AzureMailer\Graph\GraphClient;
use PromoSeven\AzureMailer\Graph\TokenManager;
use PromoSeven\AzureMailer\Transport\AzureTransport;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
use Symfony\Component\Mailer\Transport\TransportInterface;
class MailAccount extends Model
{
protected $fillable = ['name', 'label', 'type', 'from_address', 'from_name', 'config', 'enabled'];
protected $casts = [
'config' => 'encrypted:array',
'enabled' => 'boolean',
];
public function buildTransport(): TransportInterface
{
if ($this->type === 'azure') {
$config = array_merge($this->config, [
'from_address' => $this->from_address,
'graph_api_version' => 'v1.0',
'timeout' => 30,
'save_to_sent_items' => false,
]);
return new AzureTransport(
new GraphClient(new TokenManager($config), $config),
$config
);
}
$cfg = $this->config;
$tls = ($cfg['encryption'] ?? 'tls') === 'ssl';
$transport = new EsmtpTransport($cfg['host'], (int) ($cfg['port'] ?? 587), $tls);
if (!empty($cfg['username'])) {
$transport->setUsername($cfg['username']);
$transport->setPassword($cfg['password'] ?? '');
}
return $transport;
}
}

View File

@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('mail_accounts', function (Blueprint $table) {
$table->id();
$table->string('name', 100)->unique();
$table->string('label', 150);
$table->enum('type', ['azure', 'smtp']);
$table->string('from_address', 255);
$table->string('from_name', 150)->nullable();
$table->text('config');
$table->boolean('enabled')->default(true);
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('mail_accounts');
}
};