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>
84 lines
2.3 KiB
PHP
84 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Enums\EmailLogStatus;
|
|
use App\Enums\EmailTemplateType;
|
|
use App\Mail\TransactionalMail;
|
|
use App\Models\EmailLog;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Support\Str;
|
|
|
|
final class SendTransactionalEmail implements ShouldQueue
|
|
{
|
|
use Dispatchable;
|
|
use InteractsWithQueue;
|
|
use Queueable;
|
|
use SerializesModels;
|
|
|
|
public int $tries = 3;
|
|
|
|
/** @var list<int> */
|
|
public array $backoff = [30, 120, 300];
|
|
|
|
public function __construct(
|
|
public readonly string $emailLogId,
|
|
public readonly EmailTemplateType $type,
|
|
public readonly string $recipientEmail,
|
|
public readonly string $recipientName,
|
|
public readonly array $template,
|
|
public readonly array $branding,
|
|
public readonly ?string $actionUrl = null,
|
|
) {
|
|
$this->onQueue('emails');
|
|
}
|
|
|
|
public function handle(): void
|
|
{
|
|
$emailLog = EmailLog::find($this->emailLogId);
|
|
|
|
// Idempotency: don't re-send if already sent or missing
|
|
if (! $emailLog || $emailLog->status === EmailLogStatus::SENT) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$mailable = new TransactionalMail(
|
|
template: $this->template,
|
|
branding: $this->branding,
|
|
actionUrl: $this->actionUrl,
|
|
);
|
|
|
|
if ($this->branding['reply_to_email']) {
|
|
$mailable->replyTo(
|
|
$this->branding['reply_to_email'],
|
|
$this->branding['reply_to_name'],
|
|
);
|
|
}
|
|
|
|
Mail::to($this->recipientEmail, $this->recipientName)
|
|
->send($mailable);
|
|
|
|
$emailLog->update([
|
|
'status' => EmailLogStatus::SENT->value,
|
|
'sent_at' => now(),
|
|
]);
|
|
} catch (\Exception $e) {
|
|
$emailLog->update([
|
|
'status' => EmailLogStatus::FAILED->value,
|
|
'failed_at' => now(),
|
|
'error_message' => Str::limit($e->getMessage(), 500),
|
|
]);
|
|
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|