Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Smiffy's avatar

Testing that an attachment is sent with Email

I'm writing a test to verify that an attachment is sent along with an email. One solution to verify that the mail has an attachment came from Laracasts at https://laracasts.com/discuss/channels/testing/testing-if-a-sent-mailable-has-an-attachment but this doesn't seem to work for me. I have manually tested the functionality and the attachment is present however the test fails. My test is:

...
Mail::fake();

$mailable = new OrderConfirmation($order);
Mail::to($user)->send($mailable);

Mail::assertSent(OrderConfirmation::class, function($mail) {
    $mail = $mail->build();
    return (count($mail->attachments) == 1);
});

The build method of OrderConfirmation is:

Class OrderConfirmation extends Mailable
{
    ...
    public function build()
    {
        return $this ->view('email::orderconfirmation')
            ->with('order', $this->order)
            ->attachFromStorageDisk('public', '/media/my-attachment.pdf');
    }
}

Any help appreciated!

0 likes
1 reply
Smiffy's avatar
Smiffy
OP
Best Answer
Level 4

Hope this helps someone. I found that by inspecting $mail after it was built, diskAttachments was populated. The test should be:

Mail::assertSent(OrderConfirmation::class, function($mail) {
    $mail = $mail->build();
    return (count($mail->diskAttachments) == 1);
});

Please or to participate in this conversation.