Laravel Tip: Testing your emails contents
Tags: testing, laravel, email, php
Published: 2020-05-20 09:10:20

---
title: Laravel Tip: Testing your emails contents
published: true
date: 2020-05-19 21:58:07 UTC
tags: testing,laravel,email,php
canonical_url: 
---

![](https://cdn-images-1.medium.com/max/1024/0*i3zMHxrE3O9TBd8N)<figcaption>Photo by <a href="https://unsplash.com/@cdc?utm_source=medium&amp;utm_medium=referral">CDC</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption>

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](https://github.com/ylsideas/subscribable-notifications) 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.

{% gist https://gist.github.com/peterfox/075afaaccfbf137784b4a6f97600dadc %}

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

{% gist https://gist.github.com/peterfox/f16b6f4e8099ce7b99bccd94b6598cd1 %}

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.

{% gist https://gist.github.com/peterfox/3e46d9fc75fea12473af7202af5ae3a3 %}

If you’d like to take this even further you could use [Laravel Mojito](https://github.com/nunomaduro/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.

{% gist https://gist.github.com/peterfox/cd7c1be48aaaebd6bdeea95fdeeb8b2c %}

### Conclusion

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 dev.to that. If you want to know more about me, head over to [https://www.peterfox.me](https://www.peterfox.me/). Also feel free to follow me @[SlyFireFox](https://twitter.com/SlyFireFox) on twitter for more Laravel tips and tutorials in the future.