PHPackages                             martinpetricko/laravel-database-mail - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. martinpetricko/laravel-database-mail

ActiveLibrary[Database &amp; ORM](/categories/database)

martinpetricko/laravel-database-mail
====================================

Laravel package that allows sending emails stored in the database based on fired events.

2.0.3(2mo ago)612.3k—2.5%[1 PRs](https://github.com/MartinPetricko/laravel-database-mail/pulls)MITPHPPHP ^8.2CI passing

Since Mar 5Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/MartinPetricko/laravel-database-mail)[ Packagist](https://packagist.org/packages/martinpetricko/laravel-database-mail)[ Docs](https://github.com/martinpetricko/laravel-database-mail)[ Fund](https://filamentphp.com/plugins/martin-petricko-database-mail)[ GitHub Sponsors](https://github.com/MartinPetricko)[ RSS](/packages/martinpetricko-laravel-database-mail/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (11)Versions (19)Used By (0)

Laravel Database Mail
=====================

[](#laravel-database-mail)

[![Latest Version on Packagist](https://camo.githubusercontent.com/170739f1bece644a5a422d9519e3dd7d2fd2e494c921310a98eb14bdf07783af/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d617274696e7065747269636b6f2f6c61726176656c2d64617461626173652d6d61696c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/martinpetricko/laravel-database-mail)[![GitHub Tests Action Status](https://camo.githubusercontent.com/f147c50a2b20d9ac404efc86862a1152d3d2f577047d0e736412ccf744cdc2cc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d617274696e7065747269636b6f2f6c61726176656c2d64617461626173652d6d61696c2f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/martinpetricko/laravel-database-mail/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/5d62f637019df6afa7737f8ffced31c328f2a97320d3568fa3c25c70980b6e59/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d617274696e7065747269636b6f2f6c61726176656c2d64617461626173652d6d61696c2f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/martinpetricko/laravel-database-mail/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/6cff1b6a7d68016f24bf038fd055e0c558aa7b11143d8fcb205a7e08e209caeb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d617274696e7065747269636b6f2f6c61726176656c2d64617461626173652d6d61696c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/martinpetricko/laravel-database-mail)

Laravel Database Mail lets you store email templates in your database, link them to events, and automatically send them when those events are dispatched.

For implementation of this package check out [FilamentPHP implementation](https://github.com/MartinPetricko/filament-database-mail-docs).

Support me
----------

[](#support-me)

You can support me by [buying FilamentPHP implementation of this package](https://filamentphp.com/plugins/martin-petricko-database-mail).

Upgrading
---------

[](#upgrading)

### 1.x to 2.x

[](#1x-to-2x)

New migration was added that needs to be published and migrated

```
php artisan vendor:publish --tag=database-mail-migrations
```

```
php artisan migrate
```

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require martinpetricko/laravel-database-mail
```

Publish and run the migrations with:

```
php artisan vendor:publish --tag="database-mail-migrations"
php artisan migrate
```

Publish the config file with:

```
php artisan vendor:publish --tag="database-mail-config"
```

These are the contents of the published config file:

```
return [
    /**
     * Register event listener for all TriggersDatabaseMail events,
     * that sends mails associated with the event.
     */
    'register_event_listener' => true,

    /**
     * Period of time when mail exceptions are pruned.
     */
    'prune_exceptions_period' => now()->subMonth(),

    /**
     * Models that are used by Laravel Database Mail.
     */
    'models' => [
        'mail_exception' => \MartinPetricko\LaravelDatabaseMail\Models\MailException::class,
        'mail_template' => \MartinPetricko\LaravelDatabaseMail\Models\MailTemplate::class,
    ],

    /**
     * Mailable that is used to send the mail from database.
     */
    'event_mail' => \MartinPetricko\LaravelDatabaseMail\Mail\EventMail::class,

    /**
     * Resolvers are used to automatically resolve properties of the event.
     * These property definitions can be later shown to user as available
     * variables that can be used in the mail template.
     */
    'resolvers' => [
        \MartinPetricko\LaravelDatabaseMail\Properties\Resolvers\EloquentResolver::class,
        \MartinPetricko\LaravelDatabaseMail\Properties\Resolvers\BooleanResolver::class,
        \MartinPetricko\LaravelDatabaseMail\Properties\Resolvers\StringResolver::class,
        \MartinPetricko\LaravelDatabaseMail\Properties\Resolvers\ListResolver::class,
    ],

    /**
     * Register events that implement TriggersDatabaseMail interface.
     * Events will be used to trigger the mail and this list
     * of events can be shown to user as available events.
     */
    'events' => [
        // \App\Events\YourEvent::class
    ],
];
```

Register exceptions reporting in `bootstrap/app.php`:

```
use Illuminate\Foundation\Configuration\Exceptions;
use MartinPetricko\LaravelDatabaseMail\Exceptions\DatabaseMailException;
use MartinPetricko\LaravelDatabaseMail\Facades\LaravelDatabaseMail;

//...

->withExceptions(function (Exceptions $exceptions) {
    $exceptions->report(function (DatabaseMailException $e) {
        LaravelDatabaseMail::logException($e);
    });
})
```

Enable exceptions table pruning:

```
Schedule::command('model:prune', [
    '--model' => [
        MartinPetricko\LaravelDatabaseMail\Models\MailException::class,
    ],
])->daily();
```

Usage
-----

[](#usage)

### Create Events

[](#create-events)

Add `TriggersDatabaseMail` interface and `CanTriggerDatabaseMail` trait to your standard [laravel events](https://laravel.com/docs/master/events).

```
namespace App\Events;

use App\Models\User;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use MartinPetricko\LaravelDatabaseMail\Attachments\Attachment;
use MartinPetricko\LaravelDatabaseMail\Events\Concerns\CanTriggerDatabaseMail;
use MartinPetricko\LaravelDatabaseMail\Events\Contracts\TriggersDatabaseMail;
use MartinPetricko\LaravelDatabaseMail\Recipients\Recipient;

class Registered implements TriggersDatabaseMail
{
    use Dispatchable;
    use SerializesModels;
    use CanTriggerDatabaseMail;

    /**
     * All public properties of the event will be passed
     * to the mail template body and subject.
     *
     * @param array $users
     */
    public function __construct(public User $user, public array $users, public string $emailVerificationUrl)
    {
        //
    }

    /**
     * Name of the event that can be used in the UI.
     */
    public static function getName(): string
    {
        return 'User Registered';
    }

    /**
     * Description of the event that can be used in the UI.
     */
    public static function getDescription(): ?string
    {
        return 'Fires when a user is registered';
    }

    /**
     * List of possible recipients that can receive the email.
     * MailTemplate stores recipient keys that will
     * receive the email when event is triggered.
     *
     * @return Recipient[]
     */
    public static function getRecipients(): array
    {
        return [
            'user' => new Recipient('Registered User', fn (Registered $event) => [
                $event->user,
            ]),
        ];
    }

    /**
     * List of possible attachments that can be attached to the email.
     * MailTemplate stores attachment keys that will be attached
     * to the email when the event is triggered.
     *
     * @return Attachment[]
     */
    public static function getAttachments(): array
    {
        return [
            'terms-of-service' => new Attachment('Terms of Services', fn (Registered $event) => [
                \Illuminate\Mail\Attachment::fromUrl('https://my-project.com/tos')->as('tos.pdf'),
            ]),
        ];
    }
}
```

### Register Events

[](#register-events)

Add a list of events to your published `config/database-mail.php` file:

```
'events' => [
    \App\Events\Registered::class,
],
```

### Create Mail Template

[](#create-mail-template)

```
use \MartinPetricko\LaravelDatabaseMail\Models\MailTemplate;

$mailTemplate = MailTemplate::make();

// Internal name of the mail
$mailTemplate->name = 'Verify Email Address';

// The event that triggers the mail
$mailTemplate->event = \App\Events\Registered::class;

// The subject of the email, rendered with blade
$mailTemplate->subject = 'Welcome {{ $user->name }}';

// The body of the email, rendered with blade
$mailTemplate->body = Here
HTML;

// Keys of the recipients, defined in the event, that will receive the email
$mailTemplate->recipients = ['user'];

// Keys of the attachments, defined in the event, that will be attached to the email
$mailTemplate->attachments = ['terms-of-service'];

// Optionally, you can set a delay for how long the mail should be sent after the event is fired
$mailTemplate->delay = '1 day 5 hours';

// Determines if mail is sent when event is fired
$mailTemplate->is_active = true;

$mailTemplate->save();
```

### Dispatch Event

[](#dispatch-event)

Dispatch the event as you would any other [Laravel event](https://laravel.com/docs/master/events#dispatching-events) with its parameters.

```
use App\Events\Registered;

// ... your bussiness logic

Registered::dispatch($registeredUser, $registeredUserEmailVerificationUrl);
```

### List Events

[](#list-events)

You can get all events that are registered in `config/database-mail.php`.

```
use MartinPetricko\LaravelDatabaseMail\Facades\LaravelDatabaseMail;

LaravelDatabaseMail::getEvents();
```

You can get array of property definitions that can be shown to user as available variables that can be used in mail template.

```
use MartinPetricko\LaravelDatabaseMail\Facades\LaravelDatabaseMail;

LaravelDatabaseMail::getEventAttributes(\App\Events\Registered::class);
```

### Import/Export Mail Templates

[](#importexport-mail-templates)

You can prepare your mail templates before deploying your application to production and then import them in your seeders.

#### Export Mail Templates

[](#export-mail-templates)

```
php artisan mail:export
```

#### Import Mail Templates

[](#import-mail-templates)

```
php artisan mail:import
```

#### Seeder Setup

[](#seeder-setup)

```
use Illuminate\Support\Facades\Artisan;

public function run(): void
{
    /**
     * Import all mail templates from json files and replace localhost url with production url.
     */
    Artisan::call('mail:import', [
        '--all' => true,
        '--search' => 'http:\/\/localhost',
        '--replace' => config('app.url'),
    ]);
}
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Martin Petricko](https://github.com/MartinPetricko)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

50

—

FairBetter than 96% of packages

Maintenance85

Actively maintained with recent releases

Popularity32

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 79.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~29 days

Recently: every ~18 days

Total

13

Last Release

84d ago

Major Versions

1.4.0 → 2.0.02026-02-22

### Community

Maintainers

![](https://www.gravatar.com/avatar/db681d8ae9e45ba4eb11da5407129930a1cfdc321a7f76f3ab5a3a4fb7a2146d?d=identicon)[MartinPetricko](/maintainers/MartinPetricko)

---

Top Contributors

[![MartinPetricko](https://avatars.githubusercontent.com/u/39132866?v=4)](https://github.com/MartinPetricko "MartinPetricko (35 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (6 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (3 commits)")

---

Tags

databaselaravelmaillaraveldatabasemailMartin Petrickolaravel-database-mail

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/martinpetricko-laravel-database-mail/health.svg)

```
[![Health](https://phpackages.com/badges/martinpetricko-laravel-database-mail/health.svg)](https://phpackages.com/packages/martinpetricko-laravel-database-mail)
```

###  Alternatives

[orptech/laravel-migration-partition

Laravel extensions that extends Illuminate to enable partitioned table creation within Laravel migrations.

3426.7k](/packages/orptech-laravel-migration-partition)[wxm/ddoc

基于 laravel 框架, 读取数据库信息生成数据库字典, 通过控制器注释生成API接口文档, 注册 ddoc 路由网页形式呈现

271.0k](/packages/wxm-ddoc)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
