WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.076 --> 00:00:04.836 align:middle
Alrighty, it's finally time
send real emails in production!

00:00:05.576 --> 00:00:09.396 align:middle
Mailer comes with various ways to
send emails, called "transports".

00:00:09.756 --> 00:00:13.266 align:middle
This smtp one is what we're
using for our Mailtrap testing.

00:00:13.766 --> 00:00:16.676 align:middle
We could set up our own SMTP
server to send emails...

00:00:16.676 --> 00:00:19.666 align:middle
but... that's complex, and
you need to do a lot of things

00:00:19.666 --> 00:00:21.966 align:middle
to make sure your emails
don't get marked as spam.

00:00:22.476 --> 00:00:27.426 align:middle
Boo. I highly, highly recommend
using a 3rd-party email service.

00:00:27.866 --> 00:00:31.766 align:middle
These handle all these complexities
for you and Mailer provides bridges

00:00:31.766 --> 00:00:33.926 align:middle
to many of these to make setup a breeze.

00:00:34.556 --> 00:00:39.526 align:middle
We're using Mailtrap for testing but Mailtrap
also has production sending capabilities!

00:00:39.856 --> 00:00:41.096 align:middle
Fantabulous!

00:00:41.486 --> 00:00:43.096 align:middle
It even has an official bridge!

00:00:43.656 --> 00:00:48.946 align:middle
At your terminal, install it with:
composer require symfony/mailtrap-mailer

00:00:51.136 --> 00:00:53.536 align:middle
After this is installed, check your IDE.

00:00:54.186 --> 00:00:58.066 align:middle
In .env, the recipe added some MAILER_DSN stubs.

00:00:58.436 --> 00:01:02.836 align:middle
We can get the real DSN values from
Mailtrap, but first, we need to do some setup.

00:01:03.346 --> 00:01:06.366 align:middle
Over in Mailtrap, we need to
set up a "sending domain".

00:01:06.796 --> 00:01:12.126 align:middle
This configures a domain you own to allow
Mailtrap to properly send emails on its behalf.

00:01:12.556 --> 00:01:17.266 align:middle
Our lawyers are still negotiating the
purchase of universal-travel.com, so for now,

00:01:17.396 --> 00:01:20.836 align:middle
I'm using a personal domain
I own: zenstruck.com.

00:01:21.306 --> 00:01:22.536 align:middle
Add your domain here.

00:01:23.036 --> 00:01:26.256 align:middle
Once added, you'll be on this
"Domain Verification" page.

00:01:26.616 --> 00:01:29.366 align:middle
This is super important but
Mailtrap makes it easy.

00:01:29.716 --> 00:01:32.866 align:middle
Just follow the instructions until
you get this green checkmark.

00:01:33.456 --> 00:01:37.496 align:middle
Basically, you'll need to add a bunch
of specific DNS records to your domain.

00:01:38.036 --> 00:01:43.966 align:middle
DKIM, which verifies emails sent from your
domain, and SPF, which authorizes Mailtrap

00:01:43.966 --> 00:01:47.606 align:middle
to send emails on your domain's
behalf are the most important.

00:01:48.266 --> 00:01:51.066 align:middle
Mailtrap provides great documentation
on these if you want

00:01:51.066 --> 00:01:53.236 align:middle
to dig deeper on how exactly these work.

00:01:53.616 --> 00:01:55.176 align:middle
But basically, we're telling the world

00:01:55.176 --> 00:01:58.046 align:middle
that Mailtrap is allowed to
send emails on our behalf.

00:01:58.766 --> 00:02:02.906 align:middle
Once you have the green checkmark,
click "Integrations" then "Integrate"

00:02:03.006 --> 00:02:05.206 align:middle
under the "Transaction Stream" section.

00:02:05.736 --> 00:02:08.896 align:middle
We can now decide between using SMTP or API.

00:02:09.666 --> 00:02:11.896 align:middle
I'll use the API, but either works.

00:02:12.346 --> 00:02:18.306 align:middle
And hey! This looks familiar: like with
Mailtrap testing, choose PHP, then Symfony.

00:02:18.966 --> 00:02:20.686 align:middle
This is the MAILER_DSN we need!

00:02:21.146 --> 00:02:22.906 align:middle
Copy it and jump over to your editor.

00:02:23.546 --> 00:02:28.876 align:middle
This is a sensitive environment variable, so add
it to .env.local to avoid committing it to git.

00:02:29.636 --> 00:02:33.096 align:middle
Comment out the Mailtrap
testing DSN and paste below.

00:02:33.656 --> 00:02:36.296 align:middle
I'll remove this comment because
we like to keep life tidy.

00:02:37.006 --> 00:02:37.766 align:middle
Almost ready!

00:02:38.206 --> 00:02:42.806 align:middle
Remember, we can only send emails in
production from the domain we configured.

00:02:43.196 --> 00:02:45.266 align:middle
In my case, zenstruck.com.

00:02:45.836 --> 00:02:51.416 align:middle
Open config/services.yaml and update
the global_from_email to your domain.

00:02:54.186 --> 00:02:55.336 align:middle
Let's see if this works!

00:02:55.746 --> 00:02:57.256 align:middle
In your app, book a trip.

00:02:57.786 --> 00:03:00.126 align:middle
This time use a real email address.

00:03:00.606 --> 00:03:05.926 align:middle
I'll set the name to Kevin and I'll use
my personal email: kevin@symfonycasts.com.

00:03:06.486 --> 00:03:11.336 align:middle
As much as I love you and space travel, put
your own email here to avoid spamming me.

00:03:12.036 --> 00:03:13.356 align:middle
Choose a date and book!

00:03:15.846 --> 00:03:18.766 align:middle
We're on the booking confirmation
page, that's a good sign!

00:03:19.296 --> 00:03:21.036 align:middle
Now, check your personal email.

00:03:21.506 --> 00:03:22.836 align:middle
I'll go to mine and wait...

00:03:23.426 --> 00:03:24.116 align:middle
refresh...

00:03:24.786 --> 00:03:25.466 align:middle
here it is!

00:03:25.966 --> 00:03:28.736 align:middle
If I click it, this is exactly what we expect!

00:03:29.196 --> 00:03:31.766 align:middle
The image, attachment, everything is here!

00:03:32.456 --> 00:03:36.626 align:middle
Next, let's see how we can track sent
emails with Mailtrap plus add tags

00:03:36.626 --> 00:03:39.136 align:middle
and metadata to improve that tracking!

