Explore and polish your Laravel Emails with Mailbook

smknstd
3 min readDec 29, 2022

--

When developing a web application that sends email, you generally don’t want to actually send emails to live email addresses. Fortunately with Laravel it is easy to setup on your local environment a way to send email messages to a “dummy” mailbox. To do so you could use a saas service like Mailtrap or handle it locally with something like Mailhog.

This approach has the benefit of allowing you to actually inspect visually the content of your sent messages. As you can imagine the system is perfect to try a complex feature that sends multiple emails to different recipients.

But once emails are fired properly, it is not well suited for polishing email templates as every time you change something, you need to fire the message again. Same problem when dealing with multiple languages, you need to fire each message multiple time with appropriate locale. This process is far from ideal.

A modern web application can have tons of email templates and it’s easy to forget about them. As a developer, when bootstrapping a feature, you can mess up some details of an email notification as you focus your energy on wiring everything up. Once you’re happy enough with the result, there is little chances you’ll coming back to refine it later. Your email template now sleeps quietly deep in your code base.

Mailbook

In addition to a tool like mailtrap, you can use another development tool that fill those gaps.

Mailbook is a Laravel package by Xammie, that lets you easily inspect your mails locally without having to actually trigger it in your application.

Here are some of its most interesting features:

  • just reload to update the template
  • left menu lets you explore all of your emails
  • multi locale dropdown
  • handles all Laravel mailables (App\Mails and App\Notifications)

And you can try an online demo.

Variants

Sometimes your email template can handle different scenarios with conditions or an empty state. Mailbook has your back and let you register for a same email different “variants”.

Here is an example of an electronic giftcard that can be sent by email to the client himself or to a different person:

Mailbook::add(GiftcardEmailWithAttachedPdf::class)
->variant('giftcard sent to client himself', function () {
return new GiftcardEmailWithAttachedPdf(
Giftcard::whereNull('target_email')->first()
);
})
->variant('giftcard sent to someone else with a custom message', function () {
return new GiftcardEmailWithAttachedPdf(
Giftcard::whereNotNull('target_email')->first()
);
});

Now you can insect both versions of this same email very easily:

To learn more about Mailbook, you can check out the source code on GitHub at Xammie/mailbook. To learn how to install and use the package, check out the README.

--

--