lacodix/laravel-scoped-mail-config

A package that enables user or tenant specific mail driver configuration.

v1.1.0 2024-03-12 17:20 UTC

This package is auto-updated.

Last update: 2024-05-13 12:07:00 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package makes it an ease to send mails with dynamic mail configuration. The mail configuration can be provided by any model or class, that implements our HasMailConfig Interface.

With this in mind it is the perfect fit for multi tenancy packages like Laravel-multitenancy by spatie.

It also offers the possibility to keep mail configs in user or team models or even if you just want to be able to offer your admin-user to set a dynamic mail configuration.

Documentation

You can find the entire documentation for this package on our documentation site

Installation

composer require lacodix/laravel-scoped-mail-config

Basic Usage

Just add our interface to your scope model

use Illuminate\Database\Eloquent\Model;
use Lacodix\LaravelScopedMailConfig\Concerns\HasMailConfig;

class Tenant extends Model implements HasMailConfig
{
    public function getMailConfig($name): array {
        return [
            'transport' => 'smtp',
            'host' => 'my.smtp.server',
            'port' => 587,
            'encryption' => null,
            'username' => 'my@email.login',
            'password' => 'mypassword',
            'timeout' => 60,
            'local_domain' => null,
            'from' => [
                'address' => 'my@email.login',
                'name' => 'myname',
            ],
        ];
    }
}

In your AppServiceProvider you now just need to inform the package how it shall resolve the mail configuration. By default the Package resolves the current authenticated user. So if you want to use user specific mail configuration then just skip this step.

use Lacodix\LaravelScopedMailConfig\Facades\ScopedMail;

public function register(): void
{
    ScopedMail::resolveScopeUsing(fn () => Tenant::getCurrentTenant());
}

Then you can send mails with this dynamic configuration just by using our facade like laravel Mail facade:

ScopedMail::to('my@email.de')->send($mailable);

ScopedMail is just extending standard Mail facade, so there are all functionalities, that you already know from laravel Mail

Finally it is also possible to use this facade in tests

ScopedMail::fake();

Testing

composer test

Contributing

Please run the following commands and solve potential problems before committing and think about adding tests for new functionality.

composer rector:test
composer insights
composer csfixer:test
composer phpstan:test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.