kirschbaum-development / mail-intercept by Kirschbaum

A test package for intercepting email sent from Laravel
286,431
107
13
Package Data
Maintainer Username: Kirschbaum
Maintainer Contact: brandon@kirschbaumdevelopment.com (Brandon Ferens)
Package Create Date: 2019-12-06
Package Last Update: 2024-10-21
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-09 15:08:39
Package Statistics
Total Downloads: 286,431
Monthly Downloads: 6,589
Daily Downloads: 186
Total Stars: 107
Total Watchers: 13
Total Forks: 7
Total Open Issues: 1

Mail Intercept banner

Laravel Mail Intercept

A testing package for intercepting mail sent from Laravel

Latest Version on Packagist Total Downloads Codacy Badge Actions Status

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

This testing package requires Laravel 5.5 or higher.

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,
        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! Pretty simple, right?!

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.

| Assertions | Parameters | |:---------------------------------------------------------- |:--------------------------------------------------| | $this->assertMailSentTo($to, $mail); | $to string, array$mail Swift_Message | | $this->assertMailNotSentTo($to, $mail); | $to string, array$mail Swift_Message | | $this->assertMailSentFrom($from, $mail); | $from string, array$mail Swift_Message | | $this->assertMailNotSentFrom($from, $mail); | $from string, array$mail Swift_Message | | $this->assertMailSubject($subject, $mail); | $subject string$mail Swift_Message | | $this->assertMailNotSubject($subject, $mail); | $subject string$mail Swift_Message | | $this->assertMailBodyContainsString($content, $mail); | $content string$mail Swift_Message | | $this->assertMailBodyNotContainsString($content, $mail); | $content string$mail Swift_Message | | $this->assertMailRepliesTo($reply, $mail); | $reply string, array$mail Swift_Message | | $this->assertMailNotRepliesTo($reply, $mail); | $reply string, array$mail Swift_Message | | $this->assertMailCc($cc, $mail); | $cc string, array$mail Swift_Message | | $this->assertMailNotCc($cc, $mail); | $cc string, array$mail Swift_Message | | $this->assertMailBcc($cc, $mail); | $bcc string, array$mail Swift_Message | | $this->assertMailNotBcc($cc, $mail); | $bcc string, array$mail Swift_Message | | $this->assertMailSender($sender, $mail); | $sender string, array$mail Swift_Message | | $this->assertMailNotSender($sender, $mail); | $sender string, array$mail Swift_Message | | $this->assertMailIsPlain($mail); | $mail Swift_Message | | $this->assertMailIsNotPlain($mail); | $mail Swift_Message | | $this->assertMailIsHtml($mail); | $mail Swift_Message | | $this->assertMailIsNotHtml($mail); | $mail Swift_Message |

| Header Assertions | Parameters | |:------------------------------------------------------- |:---------------------------------------------------------------| | $this->assertMailHasHeader($header, $mail); | $header string$mail Swift_Message | | $this->assertMailMissingHeader($header, $mail); | $header string$mail Swift_Message | | $this->assertMailHeaderIs($header, $value, $mail); | $header string$value string$mail Swift_Message | | $this->assertMailHeaderIsNot($header, $value, $mail); | $header string$value string$mail Swift_Message |

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 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.

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.