WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.076 --> 00:00:03.636 align:middle
We've done the prep work for
our reminder email feature.

00:00:03.986 --> 00:00:06.766 align:middle
Now, let's actually create and send the emails!

00:00:07.316 --> 00:00:11.296 align:middle
In templates/email, the new email
template will be super similar

00:00:11.296 --> 00:00:13.726 align:middle
to booking_confirmation.html.twig.

00:00:14.246 --> 00:00:19.186 align:middle
Copy that file and name it
booking_reminder.html.twig.

00:00:19.746 --> 00:00:22.326 align:middle
Inside, I don't want to spend
too much time on this,

00:00:22.326 --> 00:00:26.486 align:middle
so just change the accent title
to say "Coming soon!": Ship it!

00:00:26.576 --> 00:00:27.966 align:middle
Accidental space pun!

00:00:28.536 --> 00:00:30.256 align:middle
The logic to send the emails needs

00:00:30.256 --> 00:00:33.496 align:middle
to be something we can schedule
to run every hour or every day.

00:00:33.866 --> 00:00:35.846 align:middle
Perfect job for a CLI command!

00:00:36.256 --> 00:00:37.916 align:middle
At your terminal, run: Bah!

00:00:37.916 --> 00:00:46.346 align:middle
symfony console make:command Call
it: app:send-booking-reminders.

00:00:47.276 --> 00:00:48.016 align:middle
Go check it out!

00:00:48.256 --> 00:00:52.726 align:middle
src/Command/SendBookingRemindersCommand.php.

00:00:52.726 --> 00:00:58.136 align:middle
Change the description to "Send booking
reminder emails": In the constructor,

00:00:59.486 --> 00:01:12.416 align:middle
autowire &amp; set properties for BookingRepository,
EntityManagerInterface and MailerInterface:

00:01:14.326 --> 00:01:16.746 align:middle
This command doesn't need
any arguments or options,

00:01:16.746 --> 00:01:18.866 align:middle
so remove the configure() method entirely.

00:01:19.526 --> 00:01:20.986 align:middle
Clear out the guts of execute().

00:01:20.986 --> 00:01:26.166 align:middle
Start by adding a nice: $io-&gt;title('Sending
booking reminders').

00:01:26.756 --> 00:01:29.086 align:middle
Then, grab the bookings that
need reminders sent,

00:01:29.126 --> 00:01:35.646 align:middle
with $bookings =
$this-&gt;bookingRepo-&gt;findBookingsToRemind().

00:01:36.586 --> 00:01:41.006 align:middle
To be the absolute best, let's show a
progress bar as we loop over the bookings.

00:01:41.476 --> 00:01:44.046 align:middle
The $io object has a trick for this.

00:01:44.476 --> 00:01:50.466 align:middle
Write foreach ($io-&gt;progressIterate($bookings)
as $booking).

00:01:51.116 --> 00:01:54.066 align:middle
This handles all the boring
progress bar logic for us!

00:01:54.656 --> 00:01:56.976 align:middle
Inside, we need to create a new email.

00:01:57.636 --> 00:02:04.406 align:middle
In TripController, copy that email -
including these headers, and paste it here.

00:02:05.366 --> 00:02:08.086 align:middle
But we need to adjust this a
bit: remove the attachment.

00:02:09.666 --> 00:02:13.346 align:middle
And for the subject: replace
"Confirmation" with "Reminder".

00:02:14.126 --> 00:02:16.486 align:middle
Above, add some variables for convenience:

00:02:16.486 --> 00:02:24.676 align:middle
$customer = $booking-&gt;getCustomer()
and $trip = $booking-&gt;getTrip().

00:02:25.416 --> 00:02:30.446 align:middle
Down here, keep the same metadata, but
change the tag to booking_reminder.

00:02:31.046 --> 00:02:33.716 align:middle
This will help us better
distinguish these emails in Mailtrap.

00:02:34.286 --> 00:02:39.036 align:middle
Oh, and of course, change the
template to booking_reminder.html.twig.

00:02:39.136 --> 00:02:47.386 align:middle
Still in the loop, send the email with
$this-&gt;mailer-&gt;send($email) and mark the booking

00:02:47.386 --> 00:02:48.626 align:middle
as having the reminder sent

00:02:48.626 --> 00:02:54.276 align:middle
with $booking-&gt;setReminderSentAt(new
\DateTimeImmutable('now')).

00:02:55.426 --> 00:03:00.496 align:middle
Perfect! Outside the loop,
call $this-&gt;em-&gt;flush()

00:03:00.646 --> 00:03:02.346 align:middle
to save the changes to the database.

00:03:02.346 --> 00:03:04.156 align:middle
Finally, celebrate

00:03:04.156 --> 00:03:12.306 align:middle
with $io-&gt;success(sprintf('Sent %d
booking reminders', count($bookings))).

00:03:13.286 --> 00:03:14.106 align:middle
Testing time!

00:03:14.376 --> 00:03:15.436 align:middle
Pop over to your terminal.

00:03:15.986 --> 00:03:20.116 align:middle
To be sure we have a booking that needs a
reminder sent, reload the fixtures with:

00:03:20.466 --> 00:03:26.986 align:middle
symfony console doctrine:fixture:load
Now, run our new command!

00:03:26.986 --> 00:03:33.976 align:middle
symfony console app:send-booking-reminders
Nice, 1 reminder sent!

00:03:34.256 --> 00:03:36.236 align:middle
And the output will impress our colleagues!

00:03:36.896 --> 00:03:42.106 align:middle
Before we check Mailtrap, run the command
again: "Sent 0 booking reminders".

00:03:42.346 --> 00:03:46.406 align:middle
Perfect! Our logic to mark bookings
as having reminders sent works!

00:03:47.036 --> 00:03:48.246 align:middle
Now check Mailtrap...

00:03:49.006 --> 00:03:49.506 align:middle
here it is!

00:03:49.506 --> 00:03:55.846 align:middle
As expected, it looks super similar to our
confirmation email but, it says "Coming soon!"

00:03:55.846 --> 00:03:57.846 align:middle
here: it's using the new template.

00:03:58.466 --> 00:04:02.606 align:middle
When using "Mailtrap Testing", Mailer
tags and metadata are not converted

00:04:02.606 --> 00:04:06.766 align:middle
to Mailtrap categories and custom variables
like they are when sent in production.

00:04:07.426 --> 00:04:09.206 align:middle
But you can still make sure they're being sent!

00:04:09.656 --> 00:04:12.216 align:middle
Click this "Tech Info" tab
and scroll down a bit.

00:04:12.756 --> 00:04:15.466 align:middle
When Mailer doesn't know how
to convert tags and metadata,

00:04:15.766 --> 00:04:20.376 align:middle
it adds them as these generic custom
headers: X-Tag and X-Metadata.

00:04:20.936 --> 00:04:24.176 align:middle
Sure enough, X-Tag is booking_reminder.

00:04:24.646 --> 00:04:26.386 align:middle
Awesome, that's what we expect too!

00:04:27.106 --> 00:04:28.506 align:middle
Ok, new feature?

00:04:28.606 --> 00:04:30.606 align:middle
Check! Test for the new feature?

00:04:30.856 --> 00:04:31.516 align:middle
That's next!

