A utility for viewing emails in your browser as you develop with Laravel. This package allows you to preview your mailables without actually sending emails, making it easier to develop and test your email templates.
// Make any mailable previewable
class WelcomeEmail extends Mailable implements Previewable
{
public static function toPreview(): self
{
return new self(User::factory()->make());
}
}Then, view it instantly at 👀 http://your-app.test/mail/welcome-email
Think of it like Factories for your emails.
To make a mailable previewable, implement the Previewable interface and add the toPreview() method:
class WelcomeEmail extends Mailable implements Previewable
{
public static function toPreview(): self
{
return new self(
User::factory(['name' => 'Chris Arter'])->make()
);
}
}Once you've made your mailable previewable, you can view it in your browser at:
http://your-app.test/mail/welcome-email
The URL slug is automatically generated from your class name:
WelcomeEmail→welcome-emailOrderConfirmationMail→order-confirmation-mailTestMailable→test-mailable
You can also define a custom preview slug by adding a static property to your mailable:
class WelcomeEmail extends Mailable implements Previewable
{
public static string $previewSlug = 'welcome';
// ... rest of your mailable code
}Now you can access it at: http://your-app.test/mail/welcome
You can install the package via Composer:
composer require dynamik-dev/laravel-mail-preview --devThe package will automatically register itself with Laravel.
- PHP 8.4+
- Laravel 10.x, 11.x, or 12.x
Publish the configuration file:
php artisan vendor:publish --provider="DynamikDev\MailPreview\MailPreviewServiceProvider"This will create a config/mail-preview.php file with the following options:
return [
// Enable or disable the mail preview
'enabled' => env('MAIL_PREVIEW_ENABLED', false),
// The route prefix for the mail preview
'route_prefix' => env('MAIL_PREVIEW_ROUTE_PREFIX', 'mail'),
// The path to discover mailables in
'discover_path' => env('MAIL_PREVIEW_DISCOVER_PATH', base_path('app')),
];To enable the mail preview, set MAIL_PREVIEW_ENABLED to true in your .env file.
These are the available options:
MAIL_PREVIEW_ENABLED=true
MAIL_PREVIEW_ROUTE_PREFIX=mail
MAIL_PREVIEW_DISCOVER_PATH=/path/to/your/appImportant: This package should only be enabled in development environments. The mail preview routes expose your email templates and could potentially leak sensitive information.
Make sure to:
- Set
MAIL_PREVIEW_ENABLED=falsein production - Add the preview routes to your middleware if needed
- Consider using authentication middleware for the preview routes
Run the test suite:
composer testPlease see CONTRIBUTING.md for details.
The MIT License (MIT). Please see License File for more information.
