{"id":26269470,"url":"https://github.com/martinpetricko/laravel-database-mail","last_synced_at":"2026-02-22T23:16:03.574Z","repository":{"id":280821595,"uuid":"943291817","full_name":"MartinPetricko/laravel-database-mail","owner":"MartinPetricko","description":"Laravel Database Mail lets you store email templates in your database, link them to events, and automatically send them when those events are dispatched.","archived":false,"fork":false,"pushed_at":"2025-12-11T20:53:01.000Z","size":70,"stargazers_count":5,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-13T01:26:26.517Z","etag":null,"topics":["database","laravel","mail"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MartinPetricko.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"MartinPetricko","custom":"https://filamentphp.com/plugins/martin-petricko-database-mail"}},"created_at":"2025-03-05T13:25:13.000Z","updated_at":"2025-12-11T20:52:23.000Z","dependencies_parsed_at":"2025-03-05T14:26:42.300Z","dependency_job_id":"b993838c-a484-4674-ad62-3c6213de0424","html_url":"https://github.com/MartinPetricko/laravel-database-mail","commit_stats":null,"previous_names":["martinpetricko/laravel-database-mail"],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/MartinPetricko/laravel-database-mail","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MartinPetricko%2Flaravel-database-mail","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MartinPetricko%2Flaravel-database-mail/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MartinPetricko%2Flaravel-database-mail/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MartinPetricko%2Flaravel-database-mail/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MartinPetricko","download_url":"https://codeload.github.com/MartinPetricko/laravel-database-mail/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MartinPetricko%2Flaravel-database-mail/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29730458,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-22T20:09:16.275Z","status":"ssl_error","status_checked_at":"2026-02-22T20:09:13.750Z","response_time":110,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","laravel","mail"],"created_at":"2025-03-14T05:14:29.885Z","updated_at":"2026-02-22T23:16:03.235Z","avatar_url":"https://github.com/MartinPetricko.png","language":"PHP","funding_links":["https://github.com/sponsors/MartinPetricko","https://filamentphp.com/plugins/martin-petricko-database-mail"],"categories":[],"sub_categories":[],"readme":"# Laravel Database Mail\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/martinpetricko/laravel-database-mail.svg?style=flat-square)](https://packagist.org/packages/martinpetricko/laravel-database-mail)\n[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/martinpetricko/laravel-database-mail/run-tests.yml?branch=main\u0026label=tests\u0026style=flat-square)](https://github.com/martinpetricko/laravel-database-mail/actions?query=workflow%3Arun-tests+branch%3Amain)\n[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/martinpetricko/laravel-database-mail/fix-php-code-style-issues.yml?branch=main\u0026label=code%20style\u0026style=flat-square)](https://github.com/martinpetricko/laravel-database-mail/actions?query=workflow%3A\"Fix+PHP+code+style+issues\"+branch%3Amain)\n[![Total Downloads](https://img.shields.io/packagist/dt/martinpetricko/laravel-database-mail.svg?style=flat-square)](https://packagist.org/packages/martinpetricko/laravel-database-mail)\n\nLaravel Database Mail lets you store email templates in your database, link them to events, and automatically send them\nwhen those events are dispatched.\n\nFor implementation of this package check\nout [FilamentPHP implementation](https://github.com/MartinPetricko/filament-database-mail-docs).\n\n## Support me\n\nYou can support me\nby [buying FilamentPHP implementation of this package](https://filamentphp.com/plugins/martin-petricko-database-mail).\n\n## Upgrading\n\n### 1.x to 2.x\n\nNew migration was added that needs to be published and migrated\n\n```bash\nphp artisan vendor:publish --tag=database-mail-migrations\n```\n\n```bash\nphp artisan migrate\n```\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require martinpetricko/laravel-database-mail\n```\n\nPublish and run the migrations with:\n\n```bash\nphp artisan vendor:publish --tag=\"database-mail-migrations\"\nphp artisan migrate\n```\n\nPublish the config file with:\n\n```bash\nphp artisan vendor:publish --tag=\"database-mail-config\"\n```\n\nThese are the contents of the published config file:\n\n```php\nreturn [\n    /**\n     * Register event listener for all TriggersDatabaseMail events,\n     * that sends mails associated with the event.\n     */\n    'register_event_listener' =\u003e true,\n\n    /**\n     * Period of time when mail exceptions are pruned.\n     */\n    'prune_exceptions_period' =\u003e now()-\u003esubMonth(),\n\n    /**\n     * Models that are used by Laravel Database Mail.\n     */\n    'models' =\u003e [\n        'mail_exception' =\u003e \\MartinPetricko\\LaravelDatabaseMail\\Models\\MailException::class,\n        'mail_template' =\u003e \\MartinPetricko\\LaravelDatabaseMail\\Models\\MailTemplate::class,\n    ],\n\n    /**\n     * Mailable that is used to send the mail from database.\n     */\n    'event_mail' =\u003e \\MartinPetricko\\LaravelDatabaseMail\\Mail\\EventMail::class,\n\n    /**\n     * Resolvers are used to automatically resolve properties of the event.\n     * These property definitions can be later shown to user as available\n     * variables that can be used in the mail template.\n     */\n    'resolvers' =\u003e [\n        \\MartinPetricko\\LaravelDatabaseMail\\Properties\\Resolvers\\EloquentResolver::class,\n        \\MartinPetricko\\LaravelDatabaseMail\\Properties\\Resolvers\\BooleanResolver::class,\n        \\MartinPetricko\\LaravelDatabaseMail\\Properties\\Resolvers\\StringResolver::class,\n        \\MartinPetricko\\LaravelDatabaseMail\\Properties\\Resolvers\\ListResolver::class,\n    ],\n\n    /**\n     * Register events that implement TriggersDatabaseMail interface.\n     * Events will be used to trigger the mail and this list\n     * of events can be shown to user as available events.\n     */\n    'events' =\u003e [\n        // \\App\\Events\\YourEvent::class\n    ],\n];\n```\n\nRegister exceptions reporting in `bootstrap/app.php`:\n\n```php\nuse Illuminate\\Foundation\\Configuration\\Exceptions;\nuse MartinPetricko\\LaravelDatabaseMail\\Exceptions\\DatabaseMailException;\nuse MartinPetricko\\LaravelDatabaseMail\\Facades\\LaravelDatabaseMail;\n\n//...\n\n-\u003ewithExceptions(function (Exceptions $exceptions) {\n    $exceptions-\u003ereport(function (DatabaseMailException $e) {\n        LaravelDatabaseMail::logException($e);\n    });\n})\n```\n\nEnable exceptions table pruning:\n\n```php\nSchedule::command('model:prune', [\n    '--model' =\u003e [\n        MartinPetricko\\LaravelDatabaseMail\\Models\\MailException::class,\n    ],\n])-\u003edaily();\n```\n\n## Usage\n\n### Create Events\n\nAdd `TriggersDatabaseMail` interface and `CanTriggerDatabaseMail` trait to your standard [laravel events](https://laravel.com/docs/master/events).\n\n```php\nnamespace App\\Events;\n\nuse App\\Models\\User;\nuse Illuminate\\Foundation\\Events\\Dispatchable;\nuse Illuminate\\Queue\\SerializesModels;\nuse MartinPetricko\\LaravelDatabaseMail\\Attachments\\Attachment;\nuse MartinPetricko\\LaravelDatabaseMail\\Events\\Concerns\\CanTriggerDatabaseMail;\nuse MartinPetricko\\LaravelDatabaseMail\\Events\\Contracts\\TriggersDatabaseMail;\nuse MartinPetricko\\LaravelDatabaseMail\\Recipients\\Recipient;\n\nclass Registered implements TriggersDatabaseMail\n{\n    use Dispatchable;\n    use SerializesModels;\n    use CanTriggerDatabaseMail;\n    \n    /**\n     * All public properties of the event will be passed \n     * to the mail template body and subject.\n     * \n     * @param array\u003cUser\u003e $users\n     */\n    public function __construct(public User $user, public array $users, public string $emailVerificationUrl)\n    {\n        //\n    }\n\n    /**\n     * Name of the event that can be used in the UI.\n     */\n    public static function getName(): string\n    {\n        return 'User Registered';\n    }\n\n    /**\n     * Description of the event that can be used in the UI.\n     */\n    public static function getDescription(): ?string\n    {\n        return 'Fires when a user is registered';\n    }\n\n    /**\n     * List of possible recipients that can receive the email.\n     * MailTemplate stores recipient keys that will\n     * receive the email when event is triggered.\n     *\n     * @return Recipient\u003cRegistered\u003e[]\n     */\n    public static function getRecipients(): array\n    {\n        return [\n            'user' =\u003e new Recipient('Registered User', fn (Registered $event) =\u003e [\n                $event-\u003euser,\n            ]),\n        ];\n    }\n\n    /**\n     * List of possible attachments that can be attached to the email.\n     * MailTemplate stores attachment keys that will be attached \n     * to the email when the event is triggered.\n     *\n     * @return Attachment\u003cRegistered\u003e[]\n     */\n    public static function getAttachments(): array\n    {\n        return [\n            'terms-of-service' =\u003e new Attachment('Terms of Services', fn (Registered $event) =\u003e [\n                \\Illuminate\\Mail\\Attachment::fromUrl('https://my-project.com/tos')-\u003eas('tos.pdf'),\n            ]),\n        ];\n    }\n}\n```\n\n### Register Events\n\nAdd a list of events to your published `config/database-mail.php` file:\n\n```php\n'events' =\u003e [\n    \\App\\Events\\Registered::class,\n],\n```\n\n### Create Mail Template\n\n```php\nuse \\MartinPetricko\\LaravelDatabaseMail\\Models\\MailTemplate;\n\n$mailTemplate = MailTemplate::make();\n\n// Internal name of the mail\n$mailTemplate-\u003ename = 'Verify Email Address';\n\n// The event that triggers the mail\n$mailTemplate-\u003eevent = \\App\\Events\\Registered::class;\n\n// The subject of the email, rendered with blade\n$mailTemplate-\u003esubject = 'Welcome {{ $user-\u003ename }}';\n\n// The body of the email, rendered with blade\n$mailTemplate-\u003ebody = \u003c\u003c\u003cHTML\n    \u003ch1\u003eWelcome {{ \\$user-\u003ename }}\u003c/h1\u003e\n    \u003cp\u003ePlease verify your email address by clicking \u003ca href=\"{{ \\$emailVerificationUrl }}\"\u003eHere\u003c/a\u003e\u003c/p\u003e\nHTML;\n\n// Keys of the recipients, defined in the event, that will receive the email\n$mailTemplate-\u003erecipients = ['user'];\n\n// Keys of the attachments, defined in the event, that will be attached to the email\n$mailTemplate-\u003eattachments = ['terms-of-service'];\n\n// Optionally, you can set a delay for how long the mail should be sent after the event is fired\n$mailTemplate-\u003edelay = '1 day 5 hours';\n\n// Determines if mail is sent when event is fired\n$mailTemplate-\u003eis_active = true;\n\n$mailTemplate-\u003esave();\n```\n\n### Dispatch Event\n\nDispatch the event as you would any other [Laravel event](https://laravel.com/docs/master/events#dispatching-events) with its parameters.\n\n```php\nuse App\\Events\\Registered;\n\n// ... your bussiness logic\n\nRegistered::dispatch($registeredUser, $registeredUserEmailVerificationUrl);\n```\n\n### List Events\n\nYou can get all events that are registered in `config/database-mail.php`.\n\n```php\nuse MartinPetricko\\LaravelDatabaseMail\\Facades\\LaravelDatabaseMail;\n\nLaravelDatabaseMail::getEvents();\n```\n\nYou can get array of property definitions that can be shown to user as available variables that can be used in mail\ntemplate.\n\n```php\nuse MartinPetricko\\LaravelDatabaseMail\\Facades\\LaravelDatabaseMail;\n\nLaravelDatabaseMail::getEventAttributes(\\App\\Events\\Registered::class);\n```\n\n### Import/Export Mail Templates\n\nYou can prepare your mail templates before deploying your application to production and then import them in your\nseeders.\n\n#### Export Mail Templates\n\n```bash\nphp artisan mail:export\n```\n\n#### Import Mail Templates\n\n```bash\nphp artisan mail:import\n```\n\n#### Seeder Setup\n\n```php\nuse Illuminate\\Support\\Facades\\Artisan;\n\npublic function run(): void\n{\n    /**\n     * Import all mail templates from json files and replace localhost url with production url. \n     */\n    Artisan::call('mail:import', [\n        '--all' =\u003e true,\n        '--search' =\u003e 'http:\\/\\/localhost',\n        '--replace' =\u003e config('app.url'),\n    ]);\n}\n```\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Martin Petricko](https://github.com/MartinPetricko)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartinpetricko%2Flaravel-database-mail","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmartinpetricko%2Flaravel-database-mail","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartinpetricko%2Flaravel-database-mail/lists"}