March 1, 2024

Introducing Laravel Mailbook

It's been more than a year since I've release laravel mailbook. An open source package that has already accumulated over 100k downloads and 400+ stars on GitHub. It even has been featured on laravel news. So... I guess this is a good time to finally write a blog about it!

Mailbook Demo

What is mailbook?

Mailbook is a laravel package which makes it easy to preview, inspect and test your mail templates without triggering them in your application. You are able to see the subject, recipients, attachments and of course the content of the mail.

Getting started with mailbook is very easy. After you follow the installation instructions. You can begin to register your mailables or notifications.

Mailbook::add(VerificationMail::class);
Mailbook::add(function (): InvoiceCreatedNotification {
    $user = User::factory()->create();
    $invoice = Invoice::factory()->create();

    return new InvoiceCreatedNotification($user, $invoice);
});

After that you just need to navigate to /mailbook. You can also view the mailbook demo.

Features

In my opinion there are some very powerful features for applications that deal with a lot of emails. Here is a list of the most important ones:

  • Localisation: Preview mail in every supported language of your application.
  • Database Rollback: Perform any database changes in mailbook without altering your local or production database in any way.
  • Attachments: See the attachments of the mail.
  • Mailable and notification support: Preview both mailables and notifications in mailbook.
  • Send mail: Send a mail to a real email address to do a final compatibility check.

Community

If you are still not convinced, you can read about the experience of other people who have used mailbook.

Why did I create mailbook?

At the time of creating mailbook I was working on a project that had a lot of different emails and complex logic behind when those mails would be sent. When updating styling or content of such a mail, I was bothered by the effort it took to send it to mailhog to test it locally. It could easily take up to 5 minutes to see the result of a change because I had to go through a lot of steps and forms in order to trigger the mail.

My first solution was to put every mail in a custom mail.php route file in which every mail was registered as a route. When you return a mail from a route it will render the contents of the mail. I'm of course not the first person to use this method, in fact, it's actually described in the laravel docs.

Route::get('mails', fn () => view('mails.index'))
Route::get('mails/registration', fn () => new \App\Mail\Registration(...));

I was very happy with how this worked because changing styling and content was now as easy as refreshing the page. After a while I realised this method was missing some important information, like the mail subject and the recipients. This made me eventually to create mailbook.

The name mailbook was inspired by storybook. A tool that is used to preview and test UI components.

Thank you

Thanks for reading and I hope you'll find mailbook as useful as I do. If you have any questions or suggestions feel free to open an issue or discussion.