xammie/mailbook
Laravel dev tool to preview and inspect Mailables and Notifications without triggering them in your app. Register emails in a generated routes/mailbook.php file (with DI or closures) and browse previews at /mailbook.
Install the package in your Laravel project:
composer require --dev xammie/mailbook
Run the installer to generate the route file:
php artisan mailbook:install
This creates routes/mailbook.php where you register mailables.
Register a mailer in routes/mailbook.php:
use App\Mail\VerificationMail;
use Xammie\Mailbook\Facades\Mailbook;
Mailbook::add(VerificationMail::class);
Access the UI at /mailbook to preview your emails.
Mailable or Notification class./mailbook to see a categorized list of all registered emails.Mailbook::add(App\Mail\WelcomeMail::class);
Mailbook::add(function () {
$user = User::factory()->create();
return new App\Mail\WelcomeMail($user);
});
Mailbook::add(App\Notifications\InvoicePaidNotification::class);
Mailbook::category('User Notifications')
->group(function () {
Mailbook::add(App\Mail\WelcomeMail::class);
Mailbook::add(App\Mail\PasswordResetMail::class);
});
Mailbook::to('test@example.com')
->group(function () {
Mailbook::add(App\Mail\WelcomeMail::class);
Mailbook::add(App\Mail\NewsletterMail::class);
});
Mailbook::add(App\Mail\OrderConfirmationMail::class)
->variant('Single Item', fn () => new App\Mail\OrderConfirmationMail(Order::factory()->withOneItem()->create()))
->variant('Multiple Items', fn () => new App\Mail\OrderConfirmationMail(Order::factory()->withMultipleItems()->create()));
config/mailbook.php:
'locales' => [
'en' => 'English',
'nl' => 'Dutch',
],
config/mailbook.php:
'database_rollback' => true,
Mailbook::add(function () {
$order = Order::factory()->create(); // Changes rolled back after preview
return new App\Mail\OrderShippedMail($order);
});
config/mailbook.php:
'send' => true,
'send_to' => 'test@example.com',
phpunit.xml to preview emails during tests:
<env name="MAILBOOK_ENABLED" value="true"/>
php artisan vendor:publish --tag="mailbook-views"
/mailbook with middleware (e.g., auth) by modifying the route in routes/mailbook.php.Database Rollback Scope:
Mailbook::add().Queueable Mailables:
ShouldQueue may not render correctly if the queue driver isn’t properly mocked.sync for testing).Localization Quirks:
locales is configured in mailbook.php.AppServiceProvider for consistent previews.Attachment Handling:
public_path() or storage_path() isn’t accessible.$mail->embed(file_get_contents(public_path('images/logo.png')), 'logo');
Route Conflicts:
/mailbook route is registered automatically. Avoid naming other routes /mailbook to prevent conflicts.Blank Preview?:
Mailable or Notification instance.storage/logs/laravel.log).Database Rollback Issues:
database_rollback is true in mailbook.php.Send Button Missing:
'send' => true and 'send_to' are set in mailbook.php.log, mailgun) is configured in .env.Customize the UI:
resources/views/vendor/mailbook/:
php artisan vendor:publish --tag="mailbook-views"
Add Metadata:
Mailbook facade to include custom metadata (e.g., tags, descriptions) for each mailer:
Mailbook::add(WelcomeMail::class)
->meta(['priority' => 'high', 'template' => 'welcome_v2']);
Hook into Mailbook Events:
mailbook.mail.preview or mailbook.mail.sent events to log or modify behavior:
Mailbook::add(WelcomeMail::class)
->listener(function ($mail) {
// Log or modify $mail before rendering
});
Dynamic Registration:
if (config('features.newsletter')) {
Mailbook::add(App\Mail\NewsletterMail::class);
}
Testing Utilities:
trait MailbookTestHelper {
public function registerMailForTest($mailable, $user = null) {
if ($user) Mailbook::to($user);
Mailbook::add($mailable);
}
}
How can I help you explore Laravel packages today?