Laravel mail fake

In Laravel, you can use the Mail::fake() method to fake the sending of emails. This method allows you to test your email sending logic without actually sending emails.

Here's an example of how to use Mail::fake():

use Illuminate\Support\Facades\Mail;

// Fake the sending of emails
Mail::fake();

// Send an email using the Mail facade
Mail::to('[email protected]')->send(new MyEmail());

// Verify that the email was sent
Mail::assertSent(function ($mail) {
    return $mail->hasTo('[email protected]');
});

In this example, we use Mail::fake() to fake the sending of emails. We then send an email using the Mail facade, and finally, we use Mail::assertSent() to verify that the email was sent to the correct recipient.

You can also use Mail::fake() to fake the sending of emails for a specific route or controller action:

Route::get('/send-email', function () {
    Mail::fake();

    Mail::to('[email protected]')->send(new MyEmail());

    Mail::assertSent(function ($mail) {
        return $mail->hasTo('[email protected]');
    });
});

In this example, we use Mail::fake() to fake the sending of emails for the /send-email route. We then send an email using the Mail facade, and finally, we use Mail::assertSent() to verify that the email was sent to the correct recipient.

Note that when you use Mail::fake(), Laravel will not actually send the email. Instead, it will store the email in memory, and you can use Mail::assertSent() to verify that the email was sent.

You can also use Mail::fake() to fake the sending of emails for a specific mail driver. For example:

Mail::fake(['driver' => 'smtp']);

Mail::to('[email protected]')->send(new MyEmail());

Mail::assertSent(function ($mail) {
    return $mail->hasTo('[email protected]');
});

In this example, we use Mail::fake() to fake the sending of emails using the SMTP mail driver. We then send an email using the Mail facade, and finally, we use Mail::assertSent() to verify that the email was sent to the correct recipient.