feat(mail): implement MailService for tenant-specific SMTP configuration and update MailTestService to utilize it
This commit is contained in:
96
app/Domains/Mail/Services/MailService.php
Normal file
96
app/Domains/Mail/Services/MailService.php
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Mail\Services;
|
||||||
|
|
||||||
|
use App\Domains\Integration\Models\TenantIntegration;
|
||||||
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
|
use Illuminate\Contracts\Mail\Factory as MailFactory;
|
||||||
|
use Illuminate\Contracts\Mail\Mailable;
|
||||||
|
use Illuminate\Contracts\Mail\Mailer;
|
||||||
|
use Illuminate\Mail\MailManager;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
|
||||||
|
class MailService
|
||||||
|
{
|
||||||
|
private const INTEGRATION_CODE = 'email';
|
||||||
|
|
||||||
|
private const REQUIRED_SMTP_FIELDS = [
|
||||||
|
'MAIL_HOST',
|
||||||
|
'MAIL_PORT',
|
||||||
|
'MAIL_USERNAME',
|
||||||
|
'MAIL_PASSWORD',
|
||||||
|
'MAIL_FROM_ADDRESS',
|
||||||
|
];
|
||||||
|
|
||||||
|
private readonly MailFactory $mailFactory;
|
||||||
|
|
||||||
|
private readonly ?TenantIntegration $emailIntegration;
|
||||||
|
|
||||||
|
private readonly Mailer $mailer;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private readonly Tenant $tenant,
|
||||||
|
?MailFactory $mailFactory = null,
|
||||||
|
) {
|
||||||
|
$this->mailFactory = $mailFactory ?? app(MailFactory::class);
|
||||||
|
$this->emailIntegration = TenantIntegration::query()
|
||||||
|
->where('tenant_code', $this->tenant->codigo)
|
||||||
|
->where('integration_code', self::INTEGRATION_CODE)
|
||||||
|
->first();
|
||||||
|
$this->mailer = $this->resolveMailer();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function send(string|array $recipient, Mailable $mail): void
|
||||||
|
{
|
||||||
|
$this->mailer->to($recipient)->send($mail);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function mailerName(): string
|
||||||
|
{
|
||||||
|
return $this->emailIntegration ? 'tenant-smtp' : (string) config('mail.default');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function resolveMailer(): Mailer
|
||||||
|
{
|
||||||
|
if (! $this->emailIntegration) {
|
||||||
|
return $this->mailFactory->mailer();
|
||||||
|
}
|
||||||
|
|
||||||
|
// MailFake implements the mail factory contract, but deliberately cannot
|
||||||
|
// build transports. Returning it keeps normal Mail::fake() assertions useful.
|
||||||
|
if (! $this->mailFactory instanceof MailManager) {
|
||||||
|
return $this->mailFactory->mailer();
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = $this->emailIntegration->integration_data;
|
||||||
|
|
||||||
|
if (! is_array($data)) {
|
||||||
|
throw new InvalidArgumentException('La configuración SMTP del tenant no es válida.');
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (self::REQUIRED_SMTP_FIELDS as $field) {
|
||||||
|
if (! array_key_exists($field, $data) || $data[$field] === null || $data[$field] === '') {
|
||||||
|
throw new InvalidArgumentException("Falta {$field} en la configuración SMTP del tenant.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$mailer = $this->mailFactory->build([
|
||||||
|
'name' => "tenant-smtp-{$this->tenant->codigo}",
|
||||||
|
'transport' => 'smtp',
|
||||||
|
'scheme' => $data['MAIL_SCHEME'] ?? null,
|
||||||
|
'host' => $data['MAIL_HOST'],
|
||||||
|
'port' => (int) $data['MAIL_PORT'],
|
||||||
|
'username' => $data['MAIL_USERNAME'],
|
||||||
|
'password' => $data['MAIL_PASSWORD'],
|
||||||
|
'timeout' => isset($data['MAIL_TIMEOUT']) ? (int) $data['MAIL_TIMEOUT'] : null,
|
||||||
|
'local_domain' => $data['MAIL_EHLO_DOMAIN'] ?? null,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$mailer->alwaysFrom(
|
||||||
|
$data['MAIL_FROM_ADDRESS'],
|
||||||
|
$data['MAIL_FROM_NAME'] ?? $this->tenant->nombre,
|
||||||
|
);
|
||||||
|
|
||||||
|
return $mailer;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
namespace App\Domains\MailTest\Services;
|
namespace App\Domains\MailTest\Services;
|
||||||
|
|
||||||
|
use App\Domains\Mail\Services\MailService;
|
||||||
use App\Domains\MailTest\Mailables\TestMail;
|
use App\Domains\MailTest\Mailables\TestMail;
|
||||||
use App\Domains\Tenant\Models\Tenant;
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
use Illuminate\Support\Facades\Mail;
|
|
||||||
|
|
||||||
class MailTestService
|
class MailTestService
|
||||||
{
|
{
|
||||||
@@ -16,13 +16,14 @@ class MailTestService
|
|||||||
$subject ??= 'Prueba de correo de Shopit';
|
$subject ??= 'Prueba de correo de Shopit';
|
||||||
$message ??= 'Este es un correo de prueba enviado desde Shopit.';
|
$message ??= 'Este es un correo de prueba enviado desde Shopit.';
|
||||||
|
|
||||||
Mail::to($recipient)->send(new TestMail($subject, $message, $tenant));
|
$mailService = new MailService($tenant);
|
||||||
|
$mailService->send($recipient, new TestMail($subject, $message, $tenant));
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'message' => 'Correo de prueba enviado correctamente.',
|
'message' => 'Correo de prueba enviado correctamente.',
|
||||||
'recipient' => $recipient,
|
'recipient' => $recipient,
|
||||||
'tenant_code' => $tenant->codigo,
|
'tenant_code' => $tenant->codigo,
|
||||||
'mailer' => (string) config('mail.default'),
|
'mailer' => $mailService->mailerName(),
|
||||||
'sent_at' => now()->toIso8601String(),
|
'sent_at' => now()->toIso8601String(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ class EmailIntegrationSeeder extends Seeder
|
|||||||
'MAIL_USERNAME' => 'required|email',
|
'MAIL_USERNAME' => 'required|email',
|
||||||
'MAIL_PASSWORD' => 'required|string',
|
'MAIL_PASSWORD' => 'required|string',
|
||||||
'MAIL_FROM_ADDRESS' => 'required|email',
|
'MAIL_FROM_ADDRESS' => 'required|email',
|
||||||
|
'MAIL_FROM_NAME' => 'nullable|string|max:255',
|
||||||
],
|
],
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|||||||
86
tests/Feature/Mail/MailServiceTest.php
Normal file
86
tests/Feature/Mail/MailServiceTest.php
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Mail;
|
||||||
|
|
||||||
|
use App\Domains\Attachable\Enums\AttachmentType;
|
||||||
|
use App\Domains\Attachable\Models\Attachment;
|
||||||
|
use App\Domains\Integration\Models\Integration;
|
||||||
|
use App\Domains\Integration\Models\TenantIntegration;
|
||||||
|
use App\Domains\Mail\Services\MailService;
|
||||||
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Mail\Mailer;
|
||||||
|
use Illuminate\Mail\MailManager;
|
||||||
|
use Mockery;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class MailServiceTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
public function test_it_builds_an_isolated_smtp_mailer_from_the_tenant_integration(): void
|
||||||
|
{
|
||||||
|
$logo = Attachment::create([
|
||||||
|
'path' => 'tenants/logo.png',
|
||||||
|
'filename' => 'logo.png',
|
||||||
|
'type' => AttachmentType::Image,
|
||||||
|
'mime_type' => 'image/png',
|
||||||
|
'extension' => 'png',
|
||||||
|
]);
|
||||||
|
$tenant = Tenant::create([
|
||||||
|
'codigo' => 'acme',
|
||||||
|
'nombre' => 'Acme Store',
|
||||||
|
'dominio' => 'acme.example.com',
|
||||||
|
'primary_color' => '#778899',
|
||||||
|
'secondary_color' => '#64748b',
|
||||||
|
'danger_color' => '#dc2626',
|
||||||
|
'success_color' => '#16a34a',
|
||||||
|
'header_bg_color' => '#112233',
|
||||||
|
'footer_bg_color' => '#445566',
|
||||||
|
'header_logo_id' => $logo->id,
|
||||||
|
'footer_logo_id' => $logo->id,
|
||||||
|
]);
|
||||||
|
Integration::create([
|
||||||
|
'integration_code' => 'email',
|
||||||
|
'name' => 'Email',
|
||||||
|
]);
|
||||||
|
TenantIntegration::create([
|
||||||
|
'tenant_code' => $tenant->codigo,
|
||||||
|
'integration_code' => 'email',
|
||||||
|
'integration_data' => [
|
||||||
|
'MAIL_SCHEME' => 'smtp',
|
||||||
|
'MAIL_HOST' => 'smtp.example.com',
|
||||||
|
'MAIL_PORT' => 587,
|
||||||
|
'MAIL_USERNAME' => 'mailer@example.com',
|
||||||
|
'MAIL_PASSWORD' => 'secret',
|
||||||
|
'MAIL_FROM_ADDRESS' => 'store@example.com',
|
||||||
|
'MAIL_FROM_NAME' => 'Acme Mail',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$mailer = Mockery::mock(Mailer::class);
|
||||||
|
$mailer->shouldReceive('alwaysFrom')
|
||||||
|
->once()
|
||||||
|
->with('store@example.com', 'Acme Mail');
|
||||||
|
|
||||||
|
$manager = Mockery::mock(MailManager::class);
|
||||||
|
$manager->shouldReceive('build')
|
||||||
|
->once()
|
||||||
|
->with(Mockery::on(fn (array $config): bool => $config === [
|
||||||
|
'name' => 'tenant-smtp-acme',
|
||||||
|
'transport' => 'smtp',
|
||||||
|
'scheme' => 'smtp',
|
||||||
|
'host' => 'smtp.example.com',
|
||||||
|
'port' => 587,
|
||||||
|
'username' => 'mailer@example.com',
|
||||||
|
'password' => 'secret',
|
||||||
|
'timeout' => null,
|
||||||
|
'local_domain' => null,
|
||||||
|
]))
|
||||||
|
->andReturn($mailer);
|
||||||
|
|
||||||
|
$service = new MailService($tenant, $manager);
|
||||||
|
|
||||||
|
$this->assertSame('tenant-smtp', $service->mailerName());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,8 @@ namespace Tests\Feature\MailTest;
|
|||||||
|
|
||||||
use App\Domains\Attachable\Enums\AttachmentType;
|
use App\Domains\Attachable\Enums\AttachmentType;
|
||||||
use App\Domains\Attachable\Models\Attachment;
|
use App\Domains\Attachable\Models\Attachment;
|
||||||
|
use App\Domains\Integration\Models\Integration;
|
||||||
|
use App\Domains\Integration\Models\TenantIntegration;
|
||||||
use App\Domains\MailTest\Mailables\TestMail;
|
use App\Domains\MailTest\Mailables\TestMail;
|
||||||
use App\Domains\Tenant\Models\Tenant;
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
@@ -55,6 +57,36 @@ class MailTestControllerTest extends TestCase
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_it_uses_the_tenant_email_integration_when_configured(): void
|
||||||
|
{
|
||||||
|
Mail::fake();
|
||||||
|
$tenant = $this->createTenant();
|
||||||
|
|
||||||
|
Integration::create([
|
||||||
|
'integration_code' => 'email',
|
||||||
|
'name' => 'Email',
|
||||||
|
]);
|
||||||
|
TenantIntegration::create([
|
||||||
|
'tenant_code' => $tenant->codigo,
|
||||||
|
'integration_code' => 'email',
|
||||||
|
'integration_data' => [
|
||||||
|
'MAIL_SCHEME' => 'smtp',
|
||||||
|
'MAIL_HOST' => 'smtp.example.com',
|
||||||
|
'MAIL_PORT' => 587,
|
||||||
|
'MAIL_USERNAME' => 'mailer@example.com',
|
||||||
|
'MAIL_PASSWORD' => 'secret',
|
||||||
|
'MAIL_FROM_ADDRESS' => 'store@example.com',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->postJson('/api/acme/mail-test/send', [
|
||||||
|
'to' => 'recipient@example.com',
|
||||||
|
])->assertOk()
|
||||||
|
->assertJsonPath('mailer', 'tenant-smtp');
|
||||||
|
|
||||||
|
Mail::assertSent(TestMail::class);
|
||||||
|
}
|
||||||
|
|
||||||
public function test_it_validates_the_recipient(): void
|
public function test_it_validates_the_recipient(): void
|
||||||
{
|
{
|
||||||
Mail::fake();
|
Mail::fake();
|
||||||
|
|||||||
Reference in New Issue
Block a user