Easily Test Email with MailThief

Published on by

Easily Test Email with MailThief image

MailThief is a new package by Tighten Co. that provides a fake mailer for your Laravel application. This makes it easy to test email without actually sending any.

To better explain how this package works take a look at the following example from the project.

First, pretend you have a registration route that sends the user a welcome email:

Route::post('register', function () {
// <snip> Validation, create account, etc. </snip>
 
Mail::send('emails.welcome', [], function ($m) {
$email = request('email');
$m->to($email),
$m->subject('Welcome to my app!');
$m->from('noreply@example.com');
$m->bcc('notifications@example.com');
});
 
// <snip> Return response </snip>
});

Typically testing this would be pretty difficult, but with MailThief it’s simple:

use MailThief\Facades\MailThief;
 
class RegistrationTest extends TestCase
{
public function test_new_users_are_sent_a_welcome_email()
{
// Block and intercept outgoing mail, important!
MailThief::hijack();
 
$this->post('register', [
'name' => 'John Doe',
'email' => 'john@example.com',
'password' => 'secret',
]);
 
// Check that an email was sent to this email address
$this->assertTrue(MailThief::hasMessageFor('john@example.com'));
 
// BCC addresses are included too
$this->assertTrue(MailThief::hasMessageFor('notifications@example.com'));
 
// Make sure the email has the correct subject
$this->assertEquals('Welcome to my app!', MailThief::lastMessage()->subject);
 
// Make sure the email was sent from the correct address
// (`from` can be a list, so we return it as a collection)
$this->assertEquals('noreply@example.com', MailThief::lastMessage()->from->first());
}
}

You can grab the package from Github and if you’d like to learn more about the thought process behind building MailThief check out this post and video by Adam Wathan.

Eric L. Barnes photo

Eric is the creator of Laravel News and has been covering Laravel since 2012.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Forge
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
LoadForge logo

LoadForge

Easy, affordable load testing and stress tests for websites, APIs and databases.

LoadForge
Paragraph logo

Paragraph

Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Paragraph
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
Bacancy - Staff Augmentation logo

Bacancy - Staff Augmentation

Leave your web app development hustles to the leading IT Staff Augmentation Service Providers. Choose from an extensive pool of 1050+ developers and give yourself the sigh of success you deserve with Bacancy. Get In Touch Today!

Bacancy - Staff Augmentation
DocuWriter.ai logo

DocuWriter.ai

Save hours of manually writing Code Documentation, Comments & DocBlocks, Test suites and Refactoring.

DocuWriter.ai
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
Add Architecture Tests to Saloon API Integrations with Lawman image

Add Architecture Tests to Saloon API Integrations with Lawman

Read article
Protect Routes with JWT Tokens Using This Package for Laravel image

Protect Routes with JWT Tokens Using This Package for Laravel

Read article
Increment a Rate Limiter by a Custom Amount in Laravel 10.46 image

Increment a Rate Limiter by a Custom Amount in Laravel 10.46

Read article
Add Kanban Boards to Your Laravel App in Seconds image

Add Kanban Boards to Your Laravel App in Seconds

Read article
October CMS v3.6 Ships Today, Full of New Features image

October CMS v3.6 Ships Today, Full of New Features

Read article
Laracon EU Videos are now out image

Laracon EU Videos are now out

Read article