DEV Community

Peter Fox
Peter Fox

Posted on • Originally published at Medium on

Laravel Tip: Testing your emails contents

Photo by CDC on Unsplash

One of the things I originally found difficult in the Laravel framework is testing emails. As someone who’s worked on a package to perform unsubscribes in Laravel I’ve had to deal with testing quite a few emails for their final rendered content and it’s not as obvious as it seems. You might be thinking, wait a minute there’s a Mail fake and Notifications fake, and these work? But actually they do not allow you to test the final rendered email because they only allow you to test if a notification or mailable has been ‘sent’ but this happens before the email is rendered. This can be difficult if you need to test that something was actually rendered in your email. For instance, when I made my unsubscribe package I needed to test that the links for unsubscribing made it into the email body and that particular headers were added to the email. It’s not immediately obvious how to test that from the Laravel documentation so I thought I’d share how I’ve accomplished this.

Test email contents using the Event Fake

If you didn’t know, the Event fake is actually pretty useful. This is because no matter what way you’re sending an email, either by notification or by mailable, events are fired giving you the final email content as a \Swift_Message object.

To do this first you need to use the Event fake() method of the facade to catch the MessageSending event. What this means is every time an email is attempted to be sent, the event will be caught by the Event fake.

Once we have this in a test we can trigger our code to send an email and perform an assertion on the event.

Below is an example of what one of these completed tests might look like. The beauty of this system is you can also test for things like the to, cc and bcc addresses are correct or that a special header you’ve added has been applied to the message instance.

If you’d like to take this even further you could use Laravel Mojito. By installing the package you can pass the HTML content to a new ViewAssertion instance which then lets you make more structured assertions against the email content.


Not a lot more to say to this one. It you find yourself needing to test that your blade views are rendering correctly into HTML emails etc then this is a fairly quick and easy way to test it. Good luck!

I’m Peter Fox, a software developer in the UK who works with Laravel among other things. Thank you for reading my article. I’ve got several more on both medium and that. If you want to know more about me, head over to Also feel free to follow me @SlyFireFox on twitter for more Laravel tips and tutorials in the future.

Top comments (0)