WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.056 --> 00:00:06.246 align:middle
I bet that most, if not every email your app
sends will be from the same email address,

00:00:06.596 --> 00:00:10.166 align:middle
something clever like
hal9000@universal-travel.com

00:00:10.576 --> 00:00:14.666 align:middle
or the tried-and-true but
sleepier info@universal-travel.com.

00:00:15.326 --> 00:00:17.926 align:middle
Because every email will
have the same from address,

00:00:18.056 --> 00:00:20.046 align:middle
there's no point to set it in every email.

00:00:20.386 --> 00:00:22.036 align:middle
Instead, let's set it globally.

00:00:22.676 --> 00:00:25.466 align:middle
Oddly, there isn't any tiny
config option for this.

00:00:25.716 --> 00:00:29.166 align:middle
But that's great for us: it gives
us a chance to learn about events!

00:00:29.456 --> 00:00:31.366 align:middle
Very powerful, very nerdy.

00:00:31.916 --> 00:00:35.406 align:middle
Before an email is sent, Mailer
dispatches a MessageEvent.

00:00:35.816 --> 00:00:38.286 align:middle
To listen to this, find your terminal and run:

00:00:38.286 --> 00:00:43.936 align:middle
symfony console make:listener
Call it GlobalFromEmailListener.

00:00:44.686 --> 00:00:46.866 align:middle
The gives us a list of events we can listen to.

00:00:47.296 --> 00:00:49.386 align:middle
We want the first one: MessageEvent.

00:00:49.386 --> 00:00:53.006 align:middle
Start typing Symfony and
it's autocompleted for us.

00:00:53.386 --> 00:00:53.936 align:middle
Hit enter.

00:00:54.586 --> 00:00:55.776 align:middle
Listener created!

00:00:56.236 --> 00:01:00.276 align:middle
To be extra cool, let's set our
global from address as a parameter.

00:01:00.806 --> 00:01:06.536 align:middle
In config/services.yaml, under parameters,
add a new one: global_from_email.

00:01:07.156 --> 00:01:13.276 align:middle
This will be a string, but check this out: set
it to Universal Travel , then in angle brackets,

00:01:13.276 --> 00:01:19.376 align:middle
put the email: &lt;info@universal-travel.com&gt;:
When Symfony Mailer sees a string that looks

00:01:19.376 --> 00:01:23.546 align:middle
like this as an email address, it'll
create the proper Address object

00:01:23.596 --> 00:01:25.476 align:middle
with both a name and email set.

00:01:25.726 --> 00:01:33.486 align:middle
Sweet! Open the new class
src/EventListener/GlobalFromEmailListener.php.

00:01:33.486 --> 00:01:43.316 align:middle
Add a constructor with a private string
$fromEmail argument and an #[Autowire] attribute

00:01:43.316 --> 00:01:48.696 align:middle
with our parameter name:
%global_from_email%: Down here,

00:01:48.876 --> 00:01:53.076 align:middle
the #[AsEventListener] attribute is what
marks this method as an event listener.

00:01:53.616 --> 00:01:55.666 align:middle
We can actually remove this event argument -

00:01:55.856 --> 00:02:00.736 align:middle
it'll be inferred from the method
argument's type-hint: MessageEvent: Inside,

00:02:00.736 --> 00:02:05.636 align:middle
first grab the message from the event:
$message = $event-&gt;getMessage():

00:02:06.416 --> 00:02:09.196 align:middle
Jump into the getMessage()
method to see what it returns.

00:02:09.756 --> 00:02:11.086 align:middle
RawMessage...

00:02:11.456 --> 00:02:13.786 align:middle
jump into this and look at
what classes extend it.

00:02:15.416 --> 00:02:16.396 align:middle
TemplatedEmail!

00:02:16.616 --> 00:02:24.266 align:middle
Perfect! Back in our listener, write if
(!$message instanceof TemplatedEmail),

00:02:24.716 --> 00:02:28.486 align:middle
and inside, return;: This
will likely never be the case,

00:02:28.586 --> 00:02:30.296 align:middle
but it's good practice to double-check.

00:02:30.676 --> 00:02:34.806 align:middle
Plus, it helps our IDE know that
$message is a TemplatedEmail now.

00:02:35.426 --> 00:02:39.046 align:middle
It's possible that an email might
still set its own from address.

00:02:39.416 --> 00:02:41.306 align:middle
In this case, we don't want to override it.

00:02:41.306 --> 00:02:47.956 align:middle
So, add a guard clause: if
($message-&gt;getFrom()), return;: Now,

00:02:47.956 --> 00:02:54.706 align:middle
we can set the global from:
$message-&gt;from($this-&gt;fromEmail): Perfect!

00:02:55.476 --> 00:02:59.136 align:middle
Back in TripController::show(),
remove the -&gt;from() for the email.

00:02:59.926 --> 00:03:00.996 align:middle
Time to test this!

00:03:01.256 --> 00:03:04.346 align:middle
In our app, book a trip and
check Mailtrap for the email.

00:03:09.766 --> 00:03:10.596 align:middle
Drumroll...

00:03:11.286 --> 00:03:12.666 align:middle
the from is set correctly!

00:03:12.856 --> 00:03:14.016 align:middle
Our listener works!

00:03:14.276 --> 00:03:15.956 align:middle
I never doubted us.

00:03:15.956 --> 00:03:19.686 align:middle
One more detail to make this completely
airtight (like most of our ships).

00:03:20.226 --> 00:03:24.726 align:middle
Imagine a contact form where the user
fills their name, email, and a message.

00:03:25.076 --> 00:03:28.096 align:middle
This fires off an email with these
details to your support team.

00:03:28.646 --> 00:03:33.006 align:middle
In their email clients, it'd be nice if,
when they hit reply, it goes to the email

00:03:33.006 --> 00:03:35.236 align:middle
from the form - not your "global from".

00:03:35.926 --> 00:03:39.206 align:middle
You might think that you should set
the from address to the user's email.

00:03:39.516 --> 00:03:44.186 align:middle
But that won't work, as we're not authorized
to send emails on behalf of that user.

00:03:44.696 --> 00:03:46.246 align:middle
More on email security soon.

00:03:46.246 --> 00:03:51.766 align:middle
Fortunately, there's a special email header
called Reply-To for just this scenario.

00:03:52.186 --> 00:03:57.566 align:middle
When building your email, set it with
-&gt;replyTo() and pass the user's email address.

00:03:57.566 --> 00:04:01.506 align:middle
Strap in because the booster tanks
are full and ready for launch!

00:04:01.836 --> 00:04:04.266 align:middle
Time to send real emails in production!

00:04:04.616 --> 00:04:05.256 align:middle
That's next.

