Skip to content
Advertisement

How to get messages sent with the `array` mail driver?

Starting from version 5.7 Laravel suggests to use the array driver for Mail during testing:

Unfortunately, the documentation tells nothing about this driver. According to the source code, the driver stores all the messages in memory without actually sending them. How to get the stored “sent” messages during unit testing (in order to check them)?

Advertisement

Answer

Call app()->make('swift.transport')->driver()->messages(). The return value is a collection of Swift_Mime_SimpleMessage objects.

An example of a full PHPUnit test:

public function testEmail()
{
    Mail::to('user@example.com')->send(new MyMail);

    $emails = app()->make('swift.transport')->driver()->messages();
    $this->assertCount(1, $emails);
    $this->assertEquals(['user@example.com'], array_keys($emails[0]->getTo()));
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement