An artisan command to easily test mailables
Most of the Laravel apps we create at Spatie will send mails. This can be a password reset mail, a welcome mail after registration, an order confirmation mail, ... One of the things we do is styling such mails so it has the same look and feel as the site it was sent from. When testing such mails our designers had to request a password reset or go through the entire checkout flow just to receive such an order confirmation mail. To make that testing process a lot easier we've created a package called laravel-mailable-test. This package provides an artisan command that can send a mailable to an mail-address.
To send any mailable issue this artisan command:
php artisan mail:send-test "App\Mail\MyMailable" recipient@mail.com
This will send the given mailable to the given email address. The to-, cc- and bcc-addresses that may be set in the given mailable will be cleared. The mail will only be sent to the email address given in the artisan command.
The package will provide a value for any typehinted argument of the constructor of the mailable. If an argument is a int
, string
or bool
the package will generated a value using Faker. Any argument that typehints an Eloquent model will receive the first record of that model.
Image the constructor of your mailable looks like this:
public function __construct(string $title, Order $order)
{
...
}
That constructor will receive a string generated by the sentence
method of Faker
and the first Order
in your database.
The values that are passed to the constructor of the mailable can be customized using the values
option of the command.
php artisan mail:send-test "App\Mail\MyMailable" recipient@mail.com --values="title:My title,order:5"
Using this command My title
will be passed to $title
and an Order
with id 5 will be passed to $order
.
To learn more about the package head over to the readme on GitHub. Be sure take also take a look at this list of Laravel packages our team has previously made.
What are your thoughts on "An artisan command to easily test mailables"?