feat: complete email infrastructure with queue, templates, logging, and API

Adds the full transactional email system:
- Redis queue (QUEUE_CONNECTION=redis), SES config in .env.example
- 3 migrations: organisation_email_settings, organisation_email_templates, email_logs
- EmailTemplateType and EmailLogStatus enums with Dutch defaults
- EmailService as central entry point for all email sending
- SendTransactionalEmail queued job with retries and idempotency
- TransactionalMail mailable with responsive HTML + plain text templates
- Organisation-level branding (colors, logo, footer, reply-to)
- Per-type template overrides with {variable} substitution
- Email log with filtering by status, type, date range, recipient
- Preview and send-test endpoints for template management
- API endpoints: email-settings, email-templates (CRUD), email-logs (read-only)
- Integrated into existing flows: invitations, password reset, email
  verification, registration approval/rejection
- 37 new tests across 4 test files, all existing tests updated

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-15 20:12:21 +02:00
parent c64875b6ef
commit 65978104d8
42 changed files with 2420 additions and 48 deletions

View File

@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
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('organisation_email_settings', function (Blueprint $table) {
$table->ulid('id')->primary();
$table->ulid('organisation_id');
$table->string('logo_url', 500)->nullable();
$table->string('primary_color', 7)->default('#6366F1');
$table->string('secondary_color', 7)->default('#4F46E5');
$table->string('footer_text', 200)->nullable();
$table->string('reply_to_email')->nullable();
$table->string('reply_to_name', 100)->nullable();
$table->timestamps();
$table->foreign('organisation_id')->references('id')->on('organisations')
->cascadeOnDelete();
$table->unique('organisation_id');
});
}
public function down(): void
{
Schema::dropIfExists('organisation_email_settings');
}
};

View File

@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
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('organisation_email_templates', function (Blueprint $table) {
$table->ulid('id')->primary();
$table->ulid('organisation_id');
$table->string('type', 50);
$table->string('subject', 200);
$table->string('heading', 200)->nullable();
$table->text('body_text');
$table->string('button_text', 100)->nullable();
$table->timestamps();
$table->foreign('organisation_id')->references('id')->on('organisations')
->cascadeOnDelete();
$table->unique(['organisation_id', 'type']);
});
}
public function down(): void
{
Schema::dropIfExists('organisation_email_templates');
}
};

View File

@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
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('email_logs', function (Blueprint $table) {
$table->ulid('id')->primary();
$table->ulid('organisation_id')->nullable();
$table->ulid('event_id')->nullable();
$table->ulid('person_id')->nullable();
$table->ulid('user_id')->nullable();
$table->string('recipient_email');
$table->string('recipient_name')->nullable();
$table->string('mailable_class');
$table->string('template_type', 50);
$table->string('subject');
$table->string('status', 20)->default('queued');
$table->text('error_message')->nullable();
$table->timestamp('queued_at');
$table->timestamp('sent_at')->nullable();
$table->timestamp('failed_at')->nullable();
$table->ulid('triggered_by_user_id')->nullable();
$table->timestamps();
$table->foreign('organisation_id')->references('id')->on('organisations')
->nullOnDelete();
$table->foreign('event_id')->references('id')->on('events')
->nullOnDelete();
$table->index(['organisation_id', 'created_at']);
$table->index(['recipient_email', 'created_at']);
$table->index(['template_type', 'status']);
$table->index(['event_id']);
$table->index(['person_id']);
});
}
public function down(): void
{
Schema::dropIfExists('email_logs');
}
};