07
DecLaravel Mail Intercept is a testing suite that intercepts Laravel Mail just before they are sent out, allowing all kinds of assertions to be made on the actual emails themselves. In this package, you aren’t faking email. So you can inspect the actual mail and ensure that you are sending exactly what you want!
Under the hood, it is quite straightforward in that it forces the mail driver to be an array pushing all those emails into memory. We then grab those emails and run assertions on them! That’s it, pretty simple really!
This testing package requires Laravel 5.5
or higher.
You’ve to run this command to install this package via composer.
composer require kirschbaum-development/mail-intercept --dev
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,
WithMailInterceptor;
public function testMail()
{
$this->interceptMail();
$email = $this->faker->email;
Mail::to($email)->send(new TestMail());
$interceptedMail = $this->interceptedMail()->first();
$this->assertMailSentTo($email, $interceptedMail);
}
}
That’s it! very simple, right?!
$this->interceptMail()
This method MUST be called first, similar to how Mail::fake()
works. But unlike mail fake, the 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
.
$this->assertMailSentTo($to, $mail);
$this->assertMailNotSentTo($to, $mail);
$this->assertMailSentFrom($from, $mail);
$this->assertMailNotSentFrom($from, $mail);
$this->assertMailSubject($subject, $mail);
$this->assertMailNotSubject($subject, $mail);
$this->assertMailBodyContainsString($content, $mail);
$this->assertMailBodyNotContainsString($content, $mail);
$this->assertMailHasHeader($header, $mail);
$this->assertMailMissingHeader($header, $mail);
$this->assertMailHeaderIs($header, $value, $mail);
$this->assertMailHeaderIsNot($header, $value, $mail);
$this->assertMailIsNotHtml($mail);
$this->assertMailIsHtml($mail);
$this->assertMailIsPlain($mail);
$this->assertMailIsNotPlain($mail);
$this->assertMailSender($sender, $mail);
$this->assertMailNotSender($sender, $mail);
$this->assertMailNotBcc($cc, $mail);
$this->assertMailBcc($cc, $mail);
$this->assertMailCc($cc, $mail);
$this->assertMailNotCc($cc, $mail);
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.
Since $this->interceptedMail()
returns a collection of Swift_Message
objects, you are free to dissect and look into those objects using any methods available to Swift’s Message API. Head over to the Swift Mail Docs for more detailed info.
You can visit its documentation & source code on Github.
Published at : 07-12-2021
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.
Launch project