In this episode, we're setting up a notification system to send an email whenever the status of an order changes in our application. We'll start off simple with a no-frills email, and we'll make it nicer later on.
First, we sort out sending emails locally using a tool like MailHog. You can pick another tool if you want, but the video guides you through installing MailHog on a Mac via Homebrew. Once it's running, you get a local inbox to capture test emails. Laravel is already set up to use MailHog by default, but we do need to set a mail 'from' address in the .env file.
Next, we generate a new mailable class called OrderStatusUpdated
. Instead of returning a plain view, we use a markdown template, which makes designing the email much easier in Laravel. The markdown template gets a message about the order's status change, and we set the subject accordingly.
After that, we hook into Laravel's order observer so that when an order is updated, the system sends the email to the user tied to that order. We pass the whole order into the mailable, so you can show order-specific bits like the order ID inside the email.
Finally, we walk through testing it out: update an order's status using Tinker, and you should see a new email arrive in MailHog. Now you have a working email notification for order status changes! We'll tweak and enhance the email template in a future episode.