Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Scoped Mail Config Laravel Package

lacodix/laravel-scoped-mail-config

Send emails with dynamic, per-scope mailer settings in Laravel. Provide SMTP/from config via any model or class implementing HasMailConfig—ideal for multi-tenancy (e.g., Spatie) or user/team-specific mail configurations.

View on GitHub
Deep Wiki
Context7
## Getting Started
Install via Composer:
```bash
composer require lacodix/laravel-scoped-mail-config

Publish the package config (optional):

php artisan vendor:publish --provider="Lacodix\ScopedMailConfig\ScopedMailConfigServiceProvider" --tag="config"

First Use Case: Define scoped mail configurations in config/mail.php under the scopes key:

'scopes' => [
    'newsletter' => [
        'driver' => 'ses',
        'from' => ['address' => 'newsletter@example.com', 'name' => 'Newsletter'],
    ],
    'support' => [
        'driver' => 'mailgun',
        'from' => ['address' => 'support@example.com', 'name' => 'Support Team'],
    ],
],

Use the facade to dynamically switch scopes:

use Lacodix\ScopedMailConfig\Facades\ScopedMailConfig;

// Set scope for current request
ScopedMailConfig::setScope('newsletter');

// Mail will now use the 'newsletter' configuration
Mail::to('user@example.com')->send(new Newsletter());

Implementation Patterns

Dynamic Mail Scope Resolution

Leverage middleware to auto-set scopes based on request context (e.g., route, user role):

// app/Http/Middleware/SetMailScope.php
public function handle(Request $request, Closure $next) {
    if ($request->is('newsletter/*')) {
        ScopedMailConfig::setScope('newsletter');
    }
    return $next($request);
}

Register in app/Http/Kernel.php:

protected $middlewareGroups = [
    'web' => [
        // ...
        \App\Http\Middleware\SetMailScope::class,
    ],
];

Conditional Scope Switching

Use closures for runtime logic:

ScopedMailConfig::setScope(function (Request $request) {
    return $request->user()->isPremium() ? 'premium' : 'standard';
});

Fallback Scopes

Define a default scope in config/mail.php:

'scopes' => [
    'default' => [...],
    // ...
],

Then use:

ScopedMailConfig::setScope('default'); // Fallback if scope not found

Laravel 12 Support

For Laravel 12 projects, the package now drops Laravel Insights compatibility (removed in v1.2.0). Ensure your composer.json includes:

"require": {
    "laravel/framework": "^12.0"
}

Gotchas and Tips

Scope Not Found

If a scope isn’t defined, the package throws InvalidArgumentException. Validate scopes exist before use:

if (ScopedMailConfig::hasScope('newsletter')) {
    ScopedMailConfig::setScope('newsletter');
}

Configuration Overrides

Scopes do not merge with global mail.php settings. Explicitly define all required keys (e.g., driver, from) in each scope.

Testing Scopes

Mock the facade in tests:

$mock = Mockery::mock('overload', Lacodix\ScopedMailConfig\Facades\ScopedMailConfig::class);
$mock->shouldReceive('getScope')->andReturn('test-scope');

Performance Note

Avoid setting scopes in every request if unnecessary. Cache the scope for static contexts (e.g., CLI commands):

// In a command
ScopedMailConfig::setScope('cli-notifications');

Laravel 12 Breaking Change

  • Removed Insights: If using Laravel Insights, downgrade to v1.1.0 or refactor Insights-related logic.
  • No Backward Compatibility: Test thoroughly if upgrading from Laravel 10/11.

Extension Points

  • Custom Scope Resolvers: Extend Lacodix\ScopedMailConfig\Contracts\ScopeResolver for advanced logic (e.g., database-backed scopes).
  • Event Hooks: Listen to ScopedMailConfig.ScopeSet events for side effects (e.g., logging).

---
**NO_UPDATE_NEEDED** for prior assessments (this is a new package). The above is the **full updated assessment** for `v1.2.0`.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope