Files
glastree/app/Models/EmailSetting.php
T

251 lines
7.4 KiB
PHP
Raw Normal View History

2026-05-26 08:14:29 +02:00
<?php
namespace App\Models;
2026-06-17 13:50:41 +02:00
use App\Services\GoogleOAuthService;
2026-05-26 08:14:29 +02:00
use Illuminate\Database\Eloquent\Model;
2026-06-17 13:50:41 +02:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2026-05-26 08:14:29 +02:00
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\Crypt;
class EmailSetting extends Model
{
protected $table = 'email_settings';
protected $fillable = [
'imap_host', 'imap_port', 'imap_encryption', 'imap_username', 'imap_password',
'smtp_host', 'smtp_port', 'smtp_encryption', 'smtp_username', 'smtp_password',
'email_address', 'email_name', 'reply_to', 'sync_interval_minutes',
2026-06-17 13:50:41 +02:00
'last_sync_at', 'is_active', 'signature', 'signature_enabled',
'auth_method', 'google_oauth_connection_id',
2026-05-26 08:14:29 +02:00
];
protected $casts = [
'is_active' => 'boolean',
'signature_enabled' => 'boolean',
'last_sync_at' => 'datetime',
2026-06-17 13:50:41 +02:00
'google_oauth_connection_id' => 'integer',
2026-05-26 08:14:29 +02:00
];
public function folders(): HasMany
{
return $this->hasMany(EmailFolder::class)->orderBy('sort_order');
}
2026-06-17 13:50:41 +02:00
public function googleOAuthConnection(): BelongsTo
{
return $this->belongsTo(GoogleOAuthConnection::class);
}
2026-05-26 08:14:29 +02:00
public function getImapConfig(): array
{
2026-06-17 13:50:41 +02:00
if ($this->auth_method === 'oauth') {
$token = $this->resolveOAuthToken();
return [
'host' => $this->imap_host,
'port' => $this->imap_port,
'encryption' => $this->imap_encryption,
'username' => $this->imap_username,
'authentication' => 'oauth',
'password' => $token,
];
}
2026-05-26 08:14:29 +02:00
return [
'host' => $this->imap_host,
'port' => $this->imap_port,
'encryption' => $this->imap_encryption,
'username' => $this->imap_username,
'password' => $this->imap_password,
];
}
public function getDecryptedPassword(): string
{
2026-06-17 13:50:41 +02:00
if ($this->auth_method === 'oauth') {
return $this->resolveOAuthToken();
}
2026-05-26 08:14:29 +02:00
if (empty($this->imap_password)) {
return '';
}
try {
return Crypt::decryptString($this->imap_password);
} catch (\Exception $e) {
return $this->imap_password;
}
}
public function getDecryptedSmtpPassword(): string
{
2026-06-17 13:50:41 +02:00
if ($this->auth_method === 'oauth') {
return $this->resolveOAuthToken();
}
2026-05-26 08:14:29 +02:00
if (empty($this->smtp_password)) {
return $this->getDecryptedPassword();
}
try {
return Crypt::decryptString($this->smtp_password);
} catch (\Exception $e) {
return $this->smtp_password;
}
}
2026-06-17 13:50:41 +02:00
protected function resolveOAuthToken(): string
{
if (!$this->relationLoaded('googleOAuthConnection')) {
$this->load('googleOAuthConnection');
}
$connection = $this->googleOAuthConnection;
if (!$connection) {
return '';
}
try {
return app(GoogleOAuthService::class)->getAccessToken($connection);
} catch (\Exception $e) {
return '';
}
}
2026-05-26 08:14:29 +02:00
public function getSmtpConfig(): array
{
return [
'host' => $this->smtp_host ?? $this->imap_host,
'port' => $this->smtp_port ?? 587,
'encryption' => $this->smtp_encryption ?? 'tls',
'username' => $this->smtp_username ?? $this->imap_username,
'password' => $this->smtp_password ?? $this->imap_password,
];
}
public function getImapClient(): ?\DirectoryTree\ImapEngine\Mailbox
{
if (!$this->imap_host || !$this->imap_username) {
return null;
}
try {
$encryption = match ($this->imap_encryption) {
'ssl' => 'ssl',
'tls' => 'tls',
default => 'none',
};
2026-06-17 13:50:41 +02:00
$config = [
2026-05-26 08:14:29 +02:00
'host' => $this->imap_host,
'port' => $this->imap_port ?? 993,
'encryption' => $encryption,
'validate_cert' => false,
'username' => $this->imap_username,
'password' => $this->getDecryptedPassword(),
2026-06-17 13:50:41 +02:00
];
if ($this->auth_method === 'oauth') {
$config['authentication'] = 'oauth';
}
return new \DirectoryTree\ImapEngine\Mailbox($config);
2026-05-26 08:14:29 +02:00
} catch (\Exception $e) {
return null;
}
}
2026-06-17 13:50:41 +02:00
public function getSmtpMailer(): ?\Symfony\Component\Mailer\Mailer
{
if ($this->auth_method === 'oauth') {
return $this->buildOAuthSmtpMailer();
}
$dsn = $this->buildSmtpDsn();
if (!$dsn) {
return null;
}
try {
$transport = \Symfony\Component\Mailer\Transport::fromDsn($dsn);
return new \Symfony\Component\Mailer\Mailer($transport);
} catch (\Exception $e) {
return null;
}
}
protected function buildOAuthSmtpMailer(): ?\Symfony\Component\Mailer\Mailer
{
$token = $this->resolveOAuthToken();
if (empty($token)) {
return null;
}
$username = $this->smtp_username ?? $this->email_address;
$host = $this->smtp_host ?? 'smtp.gmail.com';
$port = $this->smtp_port ?? 587;
$transport = new \Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport($host, $port, true);
$transport->setUsername($username);
$transport->setPassword($token);
$transport->setAuthenticators([
new \Symfony\Component\Mailer\Transport\Smtp\Auth\XOAuth2Authenticator(),
]);
return new \Symfony\Component\Mailer\Mailer($transport);
}
public function buildSmtpDsn(): ?string
{
$password = $this->getDecryptedSmtpPassword();
if (empty($password)) {
return null;
}
$username = $this->smtp_username ?? $this->email_address;
$encryption = $this->smtp_encryption ?? 'tls';
$scheme = ($encryption === 'ssl') ? 'smtps' : 'smtp';
return sprintf(
'%s://%s:%s@%s:%d?encryption=%s',
$scheme,
rawurlencode($username),
rawurlencode($password),
$this->smtp_host ?? 'smtp.gmail.com',
$this->smtp_port ?? 587,
$encryption
);
}
2026-05-26 08:14:29 +02:00
public function testConnection(): array
{
try {
$mailbox = $this->getImapClient();
2026-06-17 13:50:41 +02:00
2026-05-26 08:14:29 +02:00
if (!$mailbox) {
return ['success' => false, 'message' => 'Configurazione IMAP non valida'];
}
$mailbox->connect();
$mailbox->disconnect();
2026-06-17 13:50:41 +02:00
2026-05-26 08:14:29 +02:00
return [
2026-06-17 13:50:41 +02:00
'success' => true,
2026-05-26 08:14:29 +02:00
'message' => 'Connessione IMAP riuscita! Server: ' . $this->imap_host
];
} catch (\Exception $e) {
$message = $e->getMessage();
if (strpos($message, 'Undefined property') !== false) {
return ['success' => true, 'message' => 'Connessione IMAP riuscita! (Server: ' . $this->imap_host . ')'];
}
return ['success' => false, 'message' => $message];
}
}
public static function getActive()
{
return self::where('is_active', true)->first();
}
public function getSignature(): ?string
{
return $this->signature;
}
2026-06-17 13:50:41 +02:00
}