- How do I install and set up spatie/laravel-mail-preview in my Laravel project?
- Run `composer require spatie/laravel-mail-preview`, then update your `config/mail.php` to use the `preview` transport under your SMTP mailer. Finally, add the `AddMailPreviewOverlayToResponse` middleware to your `web` middleware group in `app/Http/Kernel.php`. The package is ready for use after these steps.
- Can I use this package in production? What are the risks?
- No, this package is explicitly designed for non-production environments. Enabling it in production could expose sensitive email content or preview links. The package includes a hardcoded check for `APP_DEBUG` to prevent accidental use in production.
- How does the browser overlay work, and can I customize it?
- The overlay appears as a small notification in the browser when an email is sent, containing a link to the rendered email. You can disable it via configuration or customize its appearance by overriding the package’s JavaScript/CSS files. The overlay is optional and won’t affect email functionality.
- Where are the preview emails stored, and how do I access them?
- Emails are stored as `.html` and `.eml` files in a configurable directory (default: `storage/mail-preview`). You can access them directly via the browser link in the overlay or by navigating to the storage path. The files are named with timestamps for easy identification.
- Does this package work with Laravel’s Mail::fake() for testing?
- Yes, the package includes a `SentMails` facade that complements Laravel’s `Mail::fake()`. It allows you to assert the content of sent emails in tests, such as checking HTML or text body. However, it doesn’t replace `Mail::fake()` entirely—use it alongside Laravel’s native testing tools for full coverage.
- How can I test emails in CI/CD pipelines using this package?
- Configure the storage path to a temporary directory (e.g., `/tmp`) in your CI environment. Use the `SentMails` facade to assert email content in tests, and ensure the preview transport is only enabled in non-production environments. Clean up files after tests to avoid disk bloat.
- Will this package slow down my application if I send many emails?
- The package adds minimal overhead for generating `.html` and `.eml` files, but frequent email sends could impact performance due to disk I/O. For high-volume environments, consider using a RAM disk for storage or disabling the package in performance-critical tests.
- Are there alternatives to spatie/laravel-mail-preview for previewing emails?
- Alternatives include manually logging emails to a database or using Laravel’s `Mail::fake()` with custom assertions. However, this package stands out for its seamless integration with Laravel’s mail system, real-time browser overlay, and ease of use during local development.
- How do I handle attachments or embedded images in previewed emails?
- The package automatically includes attachments and embedded images in the generated `.eml` and `.html` files. You can view them directly via the preview link, and the `SentMails` facade supports assertions for attachments, including filenames and content types.
- Can I extend the package to add custom functionality, like logging or analytics?
- Yes, the package emits a `MailStoredEvent` whenever an email is saved, allowing you to hook into this event for custom logic. You can listen to this event in a service provider or observer to log emails, trigger analytics, or integrate with other tools.