kirschbaum-development / mail-intercept
A test package for intercepting email sent from Laravel
Installs: 295 537
Dependents: 1
Suggesters: 0
Security: 0
Stars: 107
Watchers: 13
Forks: 7
Open Issues: 1
Requires
- php: ^8.1|^8.2
- illuminate/mail: ^10.0|^11.0
Requires (Dev)
- laravel/pint: ^1.18
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^2.36|^3.4
- phpunit/phpunit: ^10.1|^11.0
- spatie/ray: ^1.41.1
README
Laravel Mail Intercept
A testing package for intercepting mail sent from Laravel
This testing suite intercepts Laravel Mail just before they are sent out, allowing all kinds of assertions to be made on the actual emails themselves.
Mail isn't faked here. You get to inspect the actual mail ensuring you are sending exactly what you want!
Requirements
Please note: If you are using v0.2.x
, please refer to that version's documentation.
Installation
composer require kirschbaum-development/mail-intercept --dev
Usage
Next you can use the KirschbaumDevelopment\MailIntercept\WithMailInterceptor
trait in your test class:
namespace Tests; use App\Mail\TestMail; use Illuminate\Support\Facades\Mail; use Illuminate\Foundation\Testing\WithFaker; use KirschbaumDevelopment\MailIntercept\WithMailInterceptor; class MailTest extends TestCase { use WithFaker; use WithMailInterceptor; public function testMail() { $this->interceptMail(); $email = $this->faker->email; Mail::to($email)->send(new TestMail()); $interceptedMail = $this->interceptedMail()->first(); $interceptedMail->assertSentTo($email); } }
That's it! Pretty simple, right?!
There are two ways of accessing the assertions. First is the fluent syntax directly on each intercepted email.
$interceptedMail->assertSentTo($email);
The other way (the older way) is to use the assertions methods made available from the WithMailInterceptor
trait. Using these methods are fine, but aren't as clean to write.
$this->assertMailSentTo($email, $interceptedEmail);
Both of these assertions do the exact same thing, the fluent one is just much cleaner. See all the available assertion methods below!
Testing API
$this->interceptMail()
This method MUST be called first, similar to how Mail::fake()
works. But unlike the mail fake, mail is not faked, it is intercepted.
$this->interceptedMail()
This should be called after Mail
has been sent, but before your assertions, otherwise you won't have any emails to work with. It returns a Collection
of emails so you are free to use any of the methods available to a collection.
Fluent Assertion Methods
Assertion Methods
You should use each item of the interceptedMail()
collection as the mail object for all assertions.
If you are injecting your own headers or need access to other headers in the email, use this assertion to verify they exist and are set properly. These assertions require the header name and the compiled email.
Other assertions
Since $this->interceptedMail()
returns a collection of AssertableMessage
objects. You are free to dissect and look into those objects using any methods available to Symfony's Email API. Head over to the Symfony Email Docs for more detailed info.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email brandon@kirschbaumdevelopment.com or nathan@kirschbaumdevelopment.com instead of using the issue tracker.
Credits
Sponsorship
Development of this package is sponsored by Kirschbaum Development Group, a developer driven company focused on problem solving, team building, and community. Learn more about us or join us!
License
The MIT License (MIT). Please see License File for more information.