WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.056 --> 00:00:06.406 align:middle
I think you, me, anyone that's ever received an
email, can agree that our first email stinks.

00:00:06.656 --> 00:00:08.086 align:middle
It doesn't provide any value.

00:00:08.446 --> 00:00:09.246 align:middle
Let's improve it!

00:00:09.246 --> 00:00:11.866 align:middle
First, we can add a name to the email.

00:00:12.216 --> 00:00:14.526 align:middle
This will show up in most email clients instead

00:00:14.526 --> 00:00:17.376 align:middle
of just the email address:
it just looks smoother.

00:00:18.036 --> 00:00:22.336 align:middle
Wrap the from with new Address(),
the one from Symfony\Component\Mime.

00:00:22.946 --> 00:00:28.236 align:middle
The first argument is the email, and the second
is the name - how about Universal Travel:

00:00:29.056 --> 00:00:31.616 align:middle
We can also wrap the to with new Address().

00:00:31.616 --> 00:00:38.186 align:middle
and pass $customer-&gt;getName()
for the name: For the subject,

00:00:38.186 --> 00:00:40.996 align:middle
add the trip name: 'Booking Confirmation for ' .

00:00:41.706 --> 00:00:44.946 align:middle
$trip-&gt;getName(): For the text body.

00:00:45.086 --> 00:00:47.296 align:middle
We could inline all the text right here.

00:00:47.686 --> 00:00:49.906 align:middle
That would get ugly, so let's use Twig!

00:00:50.456 --> 00:00:51.336 align:middle
We need a template.

00:00:51.686 --> 00:00:56.486 align:middle
In templates/, add a new
email/ directory and inside,

00:00:56.486 --> 00:01:02.626 align:middle
create a new file:
booking_confirmation.txt.twig.

00:01:03.226 --> 00:01:06.466 align:middle
Twig can be used for any
text format, not just html.

00:01:07.026 --> 00:01:12.266 align:middle
A good practice is to include the format
- .html or .txt - in the filename.

00:01:12.726 --> 00:01:16.076 align:middle
But Twig doesn't care about the that -
it's just to satisfy our human brains.

00:01:16.556 --> 00:01:18.146 align:middle
We'll return to this file in a second.

00:01:18.666 --> 00:01:24.516 align:middle
Back in TripController::show(), instead of
new Email(), use new TemplatedEmail() (the one

00:01:24.516 --> 00:01:28.016 align:middle
from Symfony\Bridge\Twig): Replace -&gt;text()

00:01:28.086 --> 00:01:35.106 align:middle
with -&gt;textTemplate('email
/booking_confirmation.txt.twig'):

00:01:35.906 --> 00:01:42.396 align:middle
To pass variables to the template, use
-&gt;context() with 'customer' =&gt; $customer,

00:01:43.276 --> 00:01:47.526 align:middle
'trip' =&gt; $trip, 'booking' =&gt; $booking:

00:01:47.526 --> 00:01:51.466 align:middle
Note that we aren't technically
rendering the Twig template here:

00:01:51.826 --> 00:01:54.536 align:middle
Mailer will do that for us
before it sends the email.

00:01:55.036 --> 00:01:57.126 align:middle
This is normal, boring Twig code.

00:01:57.896 --> 00:02:03.946 align:middle
Let's render the user's first name using a
cheap trick, the trip name, the departure date,

00:02:04.106 --> 00:02:05.536 align:middle
and a link to manage the booking.

00:02:05.936 --> 00:02:14.756 align:middle
We need to use absolute URLs in emails -
like https://univeral-travel.com/booking -

00:02:14.756 --> 00:02:19.656 align:middle
so we'll leverage the url()
Twig function instead of path():

00:02:19.656 --> 00:02:24.266 align:middle
{{ url('booking_show', {'uid': booking.uid}) }}.

00:02:24.846 --> 00:02:31.596 align:middle
End politely with, Regards, the
Universal Travel team: Email body done!

00:02:32.126 --> 00:02:32.806 align:middle
Test it out.

00:02:33.316 --> 00:02:41.326 align:middle
Back in your browser, choose a trip,
name: Steve, email: steve@minecraft.com,

00:02:41.806 --> 00:02:44.216 align:middle
any date in the future, and book the trip.

00:02:45.026 --> 00:02:49.136 align:middle
Open the profiler for the last request
and click the Emails tab to see the email.

00:02:50.176 --> 00:02:51.006 align:middle
Much better!

00:02:51.616 --> 00:02:54.386 align:middle
Notice the From and To addresses now have names.

00:02:54.536 --> 00:02:57.236 align:middle
And our text content is definitely
more valuable!

00:02:57.826 --> 00:03:02.276 align:middle
Copy the booking URL and paste it into your
browser to make sure it goes to the right place.

00:03:03.356 --> 00:03:04.876 align:middle
Looks like it, nice!

00:03:05.596 --> 00:03:10.046 align:middle
Next, we'll use Mailtrap's testing
tool for a more robust email preview.

