pre version 2
This commit is contained in:
+120
-7
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Services\GoogleOAuthService;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
|
||||
@@ -14,13 +16,15 @@ class EmailSetting extends Model
|
||||
'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',
|
||||
'last_sync_at', 'is_active', 'signature', 'signature_enabled'
|
||||
'last_sync_at', 'is_active', 'signature', 'signature_enabled',
|
||||
'auth_method', 'google_oauth_connection_id',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_active' => 'boolean',
|
||||
'signature_enabled' => 'boolean',
|
||||
'last_sync_at' => 'datetime',
|
||||
'google_oauth_connection_id' => 'integer',
|
||||
];
|
||||
|
||||
public function folders(): HasMany
|
||||
@@ -28,8 +32,25 @@ class EmailSetting extends Model
|
||||
return $this->hasMany(EmailFolder::class)->orderBy('sort_order');
|
||||
}
|
||||
|
||||
public function googleOAuthConnection(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(GoogleOAuthConnection::class);
|
||||
}
|
||||
|
||||
public function getImapConfig(): array
|
||||
{
|
||||
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,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'host' => $this->imap_host,
|
||||
'port' => $this->imap_port,
|
||||
@@ -41,6 +62,10 @@ class EmailSetting extends Model
|
||||
|
||||
public function getDecryptedPassword(): string
|
||||
{
|
||||
if ($this->auth_method === 'oauth') {
|
||||
return $this->resolveOAuthToken();
|
||||
}
|
||||
|
||||
if (empty($this->imap_password)) {
|
||||
return '';
|
||||
}
|
||||
@@ -53,6 +78,10 @@ class EmailSetting extends Model
|
||||
|
||||
public function getDecryptedSmtpPassword(): string
|
||||
{
|
||||
if ($this->auth_method === 'oauth') {
|
||||
return $this->resolveOAuthToken();
|
||||
}
|
||||
|
||||
if (empty($this->smtp_password)) {
|
||||
return $this->getDecryptedPassword();
|
||||
}
|
||||
@@ -63,6 +92,22 @@ class EmailSetting extends Model
|
||||
}
|
||||
}
|
||||
|
||||
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 '';
|
||||
}
|
||||
}
|
||||
|
||||
public function getSmtpConfig(): array
|
||||
{
|
||||
return [
|
||||
@@ -87,33 +132,101 @@ class EmailSetting extends Model
|
||||
default => 'none',
|
||||
};
|
||||
|
||||
return new \DirectoryTree\ImapEngine\Mailbox([
|
||||
$config = [
|
||||
'host' => $this->imap_host,
|
||||
'port' => $this->imap_port ?? 993,
|
||||
'encryption' => $encryption,
|
||||
'validate_cert' => false,
|
||||
'username' => $this->imap_username,
|
||||
'password' => $this->getDecryptedPassword(),
|
||||
]);
|
||||
];
|
||||
|
||||
if ($this->auth_method === 'oauth') {
|
||||
$config['authentication'] = 'oauth';
|
||||
}
|
||||
|
||||
return new \DirectoryTree\ImapEngine\Mailbox($config);
|
||||
} catch (\Exception $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
public function testConnection(): array
|
||||
{
|
||||
try {
|
||||
$mailbox = $this->getImapClient();
|
||||
|
||||
|
||||
if (!$mailbox) {
|
||||
return ['success' => false, 'message' => 'Configurazione IMAP non valida'];
|
||||
}
|
||||
|
||||
$mailbox->connect();
|
||||
$mailbox->disconnect();
|
||||
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
'success' => true,
|
||||
'message' => 'Connessione IMAP riuscita! Server: ' . $this->imap_host
|
||||
];
|
||||
} catch (\Exception $e) {
|
||||
@@ -134,4 +247,4 @@ class EmailSetting extends Model
|
||||
{
|
||||
return $this->signature;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user