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

Technical Evaluation

Architecture Fit The package excels in multi-tenant Laravel applications where dynamic mail configurations (e.g., tenant-specific SMTP/API keys) are required. It leverages Laravel’s service container and facade pattern, ensuring seamless integration with existing mail systems. The design adheres to Laravel’s convention over configuration philosophy, reducing boilerplate while maintaining flexibility. Key strengths include:

  • Runtime Scoping: Dynamically resolves mail configs per tenant/user via HasMailConfig interface, avoiding global overrides.
  • Facade Extensibility: ScopedMail extends Laravel’s native Mail facade, preserving all existing functionality (e.g., queues, notifications).
  • Laravel 12 Readiness: Explicit support for Laravel 12 (and backward compatibility to v8) aligns with modern Laravel ecosystems.

Integration Feasibility

  • High for Multi-Tenancy: Ideal for SaaS platforms using packages like Stancl/Apart or Spatie/Multitenancy, where tenant-specific email routing is critical.
  • Low Coupling: No modifications to core Laravel files; integrates via service providers and config.
  • Mail Driver Agnostic: Works with SMTP, SES, Mailgun, etc., without vendor lock-in.

Technical Risk

  • Minimal: No breaking changes in v1.2.0. Risks are limited to:
    • Dynamic Scoping Complexity: If tenant resolution logic (e.g., middleware) conflicts with the package’s default auth-based scoping.
    • Config Caching: Scoped configs may not play well with config:cache if not explicitly handled (test in production-like environments).
    • Laravel 12 Dependency: Teams on older versions may face minor adjustments (e.g., helper updates).
  • Edge Cases:
    • Fallback Behavior: Unscoped mails default to global config—ensure this aligns with requirements.
    • Testing: Scoped configs may break existing mail tests (e.g., unit tests assuming global SMTP).

Key Questions

  1. Scoping Strategy:
    • How will tenant/user scopes be resolved? (e.g., middleware, context managers, or custom resolvers?)
    • Does the package’s default auth-based scoping conflict with your multi-tenancy setup?
  2. Performance:
    • For high-volume apps, will dynamic config resolution (e.g., DB queries) introduce latency? Consider caching scopes.
  3. Monitoring:
    • How will you track scoped mail delivery? Replace the removed Insights integration with Laravel Telescope or custom logging.
  4. Rollback Plan:
    • If scoped configs fail, how will you revert to global settings? Test fallback mechanisms.
  5. Testing:
    • Are there existing mail tests that assume global configs? Update to verify scoped behavior in CI.

Integration Approach

Stack Fit

  • Laravel Ecosystem: Native support for Laravel 8–12; no framework-specific hacks.
  • Mail System: Extends Laravel’s Mail facade, preserving all features (queues, notifications, events).
  • Service Provider: Registers cleanly via config/app.php, requiring minimal setup.
  • Configuration: Uses Laravel’s config/mail.php with scoped overrides, enabling gradual adoption.

Migration Path

  1. Pre-Integration:
    • Audit current mail configs for global vs. scoped needs.
    • Backup config/mail.php and related service providers.
    • Identify tenant/user resolution logic (e.g., middleware, tenant resolvers).
  2. Installation:
    composer require lacodix/laravel-scoped-mail-config
    php artisan vendor:publish --provider="Lacodix\ScopedMailConfig\ScopedMailConfigServiceProvider"
    
  3. Configuration:
    • Implement HasMailConfig in your tenant/user model:
      class Tenant implements HasMailConfig {
          public function getMailConfig($name): array { ... }
      }
      
    • Bind the resolver in AppServiceProvider:
      ScopedMail::resolveScopeUsing(fn () => Tenant::getCurrentTenant());
      
    • Update mailable classes to use ScopedMail instead of Mail:
      ScopedMail::to($email)->send(new OrderShipped($order));
      
  4. Post-Integration:
    • Test scoped mail delivery in staging (e.g., verify tenant-specific SMTP routes).
    • Validate compatibility with config:cache and queue workers.

Compatibility

  • Laravel Versions: Explicit support for v8–v12; drop support for v7.
  • PHP Versions: Requires PHP 8.0+ (for Laravel 12).
  • Dependencies: No external extensions; uses Laravel’s core mail system.
  • Multi-Tenancy: Works with Stancl/Apart, Spatie/Multitenancy, or custom tenant resolvers.

Sequencing

  1. Phase 1 (Low Risk):
    • Implement in a single tenant/user scope (e.g., admin override).
    • Test with non-critical mailables (e.g., password resets).
  2. Phase 2 (Medium Risk):
    • Roll out to production with feature flags or gradual scope expansion.
    • Monitor mail delivery logs for scoped configs.
  3. Phase 3 (High Risk):
    • Enable dynamic scoping for all tenants/users.
    • Load-test with high-volume mail queues.

Operational Impact

Maintenance

  • Low Effort: Follows Laravel conventions, reducing maintenance overhead.
  • Updates: Monitor for Laravel version compatibility (e.g., v12.x updates).
  • Customizations:
    • Extend via service provider bindings (e.g., custom scope resolvers).
    • Override configs in config/scoped-mail.php.
  • Deprecations: No known deprecations; package is actively maintained (v1.2.0 released in 2026).

Support

  • Documentation: Basic but sufficient for core use cases. New FUNDING.yml suggests community support.
  • Debugging Tools:
    • Use ScopedMail::getScopes() to inspect active configs.
    • Check Laravel logs for mail driver initialization errors.
    • Leverage ScopedMail::fake() for test isolation.
  • Fallback: Maintain a global mail.php backup for rollback.

Scaling

  • Performance:
    • Static Scopes: Negligible overhead (config loaded once).
    • Dynamic Scopes: May require DB indexing (e.g., tenant ID) for high-throughput apps.
  • Horizontal Scaling: No distributed locks; safe for queued mail jobs (e.g., Laravel Queues).
  • Cold Starts: For serverless (e.g., Vapor), pre-load or cache scoped configs to avoid runtime resolution delays.
  • Queue Workers: Test with queue:work to ensure scoped configs persist across workers.

Failure Modes

Scenario Impact Mitigation Strategy
Missing scope Falls back to global config Validate scopes in middleware; log warnings.
Database timeout (dynamic scopes) Mail delivery fails Implement retry logic; cache scopes.
Config caching conflicts Scoped configs not applied Exclude scoped-mail.php from config:cache or use runtime overrides.
Tenant resolution failure No scope resolved Add fallback to global config in resolver.
SMTP/API key errors Mail delivery fails Use Laravel’s Mail::fail() events for alerts.
Queue worker crashes Pending mails lost Monitor queue jobs; implement dead-letter queues.

Ramp-Up

  • Developer Onboarding:
    • Document scoping logic (e.g., "How to add HasMailConfig to a model").
    • Provide examples for common use cases (e.g., tenant-specific "from" addresses).
  • Testing:
    • Add scoped mail tests to CI (e.g., verify tenant-specific SMTP routes).
    • Test edge cases: missing scopes, invalid configs, and fallback behavior.
  • Monitoring:
    • Track scoped mail metrics (e.g., delivery success rates per tenant).
    • Set up alerts for failed SMTP/API key validations.
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.
calmfox/watch-sylius
damienfern/grpc-symfony-bundle
atoolo/index-bundle
atoolo/genai-bundle
coprotoai/laravel-ticket
davidjln/llm-carbon-bundle
cryonighter/valid-request-bundle
coolms/taxonomy-bundle
coolms/field-bundle
articulate-orm/symfony
aaix/laravel-tall-architect
ephoto/akeneo-connector
emmanuelballery/eb-plantumlbundle
emielburgman/symfony-visitor-beacon
emielburgman/symfony-visit-storage
emielburgman/symfony-security-headers
emielburgman/symfony-log-viewer
emarref/xdebug-bundle
emarref/pubnub-bundle
elriseio/finance-money-bundle