How martinpetricko/laravel-database-mail works
Project type and purpose
• This is an open-source Laravel package (“Laravel Database Mail”) that lets you store email templates in your database, link them to Laravel events, and automatically send them when those events fire. It integrates deeply with the Laravel framework by providing service providers, facades, migrations, models, event contracts/traits, listeners, mailables, and Artisan commands.
High-level file-structure analysis
• Top-level files: composer.json, README, config and database directories for publishing configuration/migrations, example code, and a full test suite.
• src/ directory subdivides into:
• Attachments (Attachment class)
• Commands (ExportMailTemplates, ImportMailTemplates)
• Events (contracts, concerns/traits for “TriggersDatabaseMail”)
• Listeners (SendDatabaseEmails)
• Mail (EventMail mailable)
• Models (MailTemplate, MailException)
• Properties (generic Property and Resolver implementations)
• Recipients (Recipient class)
• Exceptions (DatabaseMailException)
• Facades and ServiceProviders
• dir “example” shows user-side event and model usage.
• config/database-mail.php driving behavior (enabled events, resolvers, models, mailable).
Key architecture and interaction points from README
• ServiceProvider registers event listener for any event implementing the TriggersDatabaseMail interface.
• Events use the CanTriggerDatabaseMail trait and a static list of recipients/attachments.
• SendDatabaseEmails listener:
1) Fetches matching MailTemplate(s) for the event class
2) Resolves event properties via Resolvers (Eloquent, Boolean, String)
3) Instantiates EventMail mailable
4) Dispatches via Laravel’s Mailer to resolved Recipients, attaches resolved Attachments
5) Catches DatabaseMailException and logs via LaravelDatabaseMail facade into the mail_exceptions table.
• Artisan commands for importing/exporting MailTemplate JSON files.
• Migrations create two tables: mail_templates and mail_exceptions.
Components
a. Laravel Application (host)
• Service Provider (LaravelDatabaseMailServiceProvider)
• Facade (LaravelDatabaseMail)
• Event Dispatcher & Queue
• Mailer subsystem (Laravel\Mail\Mailer)
• Scheduler (for exception pruning/commands)
b. Package Core Components
• Config (database-mail.php)
• Migrations → Database (mail_templates, mail_exceptions)
• Models → Eloquent ORM
• Events (Contracts + CanTriggerDatabaseMail trait)
• Listener (SendDatabaseEmails)
• Resolvers (EloquentResolver, BooleanResolver, StringResolver)
• Mailable (EventMail)
• Attachments & Recipients
• Exceptions (DatabaseMailException)
• Commands (ExportMailTemplates, ImportMailTemplates)
c. External Services
• Database (MySQL/Postgres/etc.)
• SMTP/External Mail Transport
Relationships and data‐flow
1) Developer dispatches a Laravel Event (implements TriggersDatabaseMail).
2) Laravel Event Dispatcher invokes SendDatabaseEmails listener.
3) Listener queries MailTemplate model(s) by event class from the database.
4) Listener uses Resolvers to map event public properties → template variables.
5) Listener builds EventMail instance (Blade-rendered subject & body).
6) Listener calls Mailer→to(Recipients)…send(EventMail).
7) On error, DatabaseMailException is thrown & caught; Listener calls Facade→logException → persisted in mail_exceptions table.
8) Separate flow: Artisan commands read/write MailTemplate JSON files ↔ MailTemplate model(s).
Architectural patterns & design principles
• Laravel Service Provider & Facade pattern for integration
• Observer pattern (Events & Listeners)
• Command pattern (Artisan Commands)
• MVC/ActiveRecord via Eloquent Models
• Strategy pattern via pluggable Resolvers & Recipient/Attachment callbacks
• POPO (Plain Old PHP Object) Contracts & Traits for extensibility