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 Mail Preview Laravel Package

spatie/laravel-mail-preview

Laravel mail transport to preview sent emails locally. Adds an in-browser overlay with a link to the last sent message, and lets you view rendered mail content in the browser. Ideal for development/testing; avoid in production.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Mail Preview Use Case: The package is a perfect fit for Laravel applications requiring local email testing (e.g., development, staging). It replaces the default SMTP transport with a preview driver, storing emails as .html/.eml files for inspection.
  • Non-Production Focus: Explicitly designed for debug environments (configurable via APP_DEBUG), ensuring no risk of production interference.
  • Event-Driven: Leverages Laravel’s event system (MailStoredEvent) for extensibility (e.g., custom storage, notifications).
  • Testability: Provides SentMails facade for assertion-based testing, addressing Laravel’s Mail::fake() limitations (e.g., content validation).

Integration Feasibility

  • Minimal Overhead: Requires 3 key changes:
    1. Update config/mail.php to use preview transport.
    2. Add middleware to app/Http/Kernel.php.
    3. Register Route::mailPreview() in routes/web.php.
  • Optional Customization: Supports publishing config/views for tailored behavior (e.g., storage paths, popup timeout).
  • Laravel Ecosystem Alignment: Works seamlessly with Laravel’s mail system, Mailable classes, and testing tools.

Technical Risk

  • Low Risk:
    • Backward Compatible: No breaking changes in recent versions (Laravel 11–13 support).
    • Isolated: Preview driver does not send real emails; no external dependencies.
    • Fallback: Can revert to SMTP by switching transports.
  • Potential Pitfalls:
    • Storage Management: Files accumulate in storage_path('email-previews'); cleanup may be needed for long-running tests.
    • Middleware Order: Overlay middleware must run after mail is sent (handled by Spatie’s design).
    • Testing Scope: SentMails assertions require actual mail sending (not Mail::fake()), which may slow tests.

Key Questions

  1. Environment Scope:
    • Will this be used only in development? If yes, the risk is negligible. If used in staging, ensure APP_DEBUG is properly gated.
  2. Storage Strategy:
    • How will stored emails be cleaned up (e.g., cron job, maximum_lifetime_in_seconds)?
  3. Testing Impact:
    • Will tests rely on SentMails assertions? If so, benchmark performance vs. Mail::fake().
  4. Customization Needs:
    • Are default views/config sufficient, or will they need publishing/modification?
  5. Laravel Version:
    • Confirmed compatibility with the target Laravel version (e.g., 10.x, 11.x).

Integration Approach

Stack Fit

  • Laravel Core: Integrates natively with Laravel’s mail system, Mailable classes, and testing utilities.
  • PHP Version: Compatible with PHP 8.1+ (Laravel 10+) and PHP 8.2+ (Laravel 11+).
  • Dependencies:
    • SwiftMailer (for mail handling, already bundled with Laravel).
    • No external APIs: All operations are local (file storage, in-memory events).

Migration Path

  1. Installation:
    composer require spatie/laravel-mail-preview
    
  2. Configuration:
    • Update config/mail.php to use preview transport.
    • Publish config/views if customization is needed:
      php artisan vendor:publish --provider="Spatie\MailPreview\MailPreviewServiceProvider" --tag="mail-preview-config"
      php artisan vendor:publish --provider="Spatie\MailPreview\MailPreviewServiceProvider" --tag="mail-preview-views"
      
  3. Middleware:
    • Add AddMailPreviewOverlayToResponse to web middleware group in app/Http/Kernel.php.
  4. Routing:
    • Register Route::mailPreview() in routes/web.php.
  5. Testing:
    • Replace Mail::fake() assertions with SentMails::assertSent() where content validation is needed.

Compatibility

  • Laravel Versions: Officially supports 10.x–13.x (as of v6.2.x). Test with the target version.
  • Mailables: Works with all Laravel Mailable classes (no restrictions).
  • Third-Party Packages: No known conflicts; isolated to mail system.
  • Custom Mailers: Supports multiple mailers (e.g., smtp, ses) by configuring each separately.

Sequencing

  1. Development First: Deploy in local/dev environments before considering staging.
  2. Test Integration:
    • Verify overlay appears after sending mail.
    • Test SentMails assertions in PHPUnit.
  3. Performance Benchmark:
    • Measure impact on mail-sending speed (negligible for most use cases).
    • Assess storage growth over time.
  4. Rollback Plan:
    • Revert config/mail.php to original transport if issues arise.

Operational Impact

Maintenance

  • Low Effort:
    • No runtime maintenance: Files are auto-generated; no manual intervention required.
    • Config-Driven: Changes to behavior (e.g., storage path, timeout) are via config/mail-preview.php.
  • Cleanup:
    • Automatic: Files expire after maximum_lifetime_in_seconds (default: 60s).
    • Manual: Can delete storage/email-previews or implement a cron job for bulk cleanup.
  • Updates:
    • Minimal: Follow Spatie’s changelog for Laravel version compatibility.

Support

  • Debugging:
    • Overlay Visibility: Ensure middleware is in web group and route is registered.
    • File Generation: Check storage_path('email-previews') permissions.
    • Assertions: Verify SentMails tests are not mixed with Mail::fake().
  • Common Issues:
    • Missing Overlay: Confirm show_link_to_preview is true and APP_DEBUG is true.
    • Storage Permissions: Ensure Laravel can write to storage/email-previews.
  • Documentation: Comprehensive README and Spatie’s support channels.

Scaling

  • Performance:
    • Negligible Impact: Preview driver adds ~50–100ms per email (file I/O).
    • High-Volume Testing: For >1000 emails/hour, consider:
      • Increasing maximum_lifetime_in_seconds.
      • Offloading cleanup to a queue job.
  • Storage:
    • File Size: .html/.eml files are small (typically <100KB each).
    • Volume: Monitor storage/email-previews growth in CI/CD pipelines.

Failure Modes

Failure Scenario Impact Mitigation
Middleware not registered Overlay never appears Verify Kernel.php and web group.
Storage path inaccessible Emails not saved Check permissions (chmod -R 755 storage).
APP_DEBUG = false Preview disabled Set enabled: true in config.
Laravel version incompatibility Package fails to load Pin to compatible version in composer.json.
Test assertions fail Flaky tests Use SentMails only for content checks.

Ramp-Up

  • Developer Onboarding:
    • Time to Use: <15 minutes for basic setup.
    • Key Concepts:
      • Preview driver replaces SMTP locally.
      • Overlay shows a link to /spatie-mail-preview.
      • SentMails facade enables content assertions.
  • Training:
  • Adoption Phases:
    1. Pilot: Use in 1–2 feature branches.
    2. Feedback: Gather input on overlay UX and storage needs.
    3. Full Rollout: Deploy to all dev environments.
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.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
ecotone/kafka
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata