In the last video we continue to working on our mailer specifically the constructor function which does
some initial set up of any instance of the mailer that we create inside of here.
We've started to define a couple of different properties that are required by the Meilleur to work correctly.
So our Meilleur expects us to have a from email property that contains exactly who is sending the email
and expects to have a subject that will be used as the subject of the e-mail and we'll expects a body
property that it's some H email display inside the email and then also expects a recipient property
which is going to be exactly who the email should be sent off to.
Now in every case here except for the subject one we have obviously had to apply some type of helper
to this thing.
So help or does e-mail help or not content are to helper functions from the censored library that properly
format both the from e-mail and the body of the e-mail to work correctly inside of an actual e-mail
that is being processed by send great.
We're about to do something very similar for sending up the list of recipients for this e-mail.
Now before we do that I want to remind you that at this point the arguments recipients right here is
the recipients property from our survey instance and that recipients property very importantly.
You'll recall was find our diagram for it.
I know we just had this thing up where to go.
Here it is right here.
So we've got our survey for instance the recipient's property is an array of objects where each object
has an email property.
So an email key value pair.
So we need to make sure that we somehow format this sub document collection that we just passed into
this thing to make sure that we extract just the actual e-mail addresses.
OK.
So give me the next challenge that we have to kind of get over.
So we want to make sure we extract each of the these e-mails from these objects inside of the sub document
collection.
So that's going to be what this format addresses function is for.
So we're going to add on this helper function called format addresses.
So format addresses that we receive our list of recipients and then inside of the function we're going
to iterate through the list of recipients.
And for every recipient we will extract just the email and then return it.
We will then return that overall array that we have created.
So I'm going to say return recipients dot map remember that every recipient inside of this function
is an object that contains an email property and we just care about that email here.
So we're going to use some 60 structuring to just pull off that e-mail.
So I'm going to place another set of parentheses and then a set of curly braces and say Give me just
the email like so now very important here.
If you do use the structure right here make sure you get the extra set of parentheses around here.
Otherwise you're going to see an error message because you can't have destructuring with an arrow function
without the parentheses.
You have to have the parentheses to do destructuring with an air of function.
OK.
So now inside of the mapping statement itself we're going to take the e-mail we're going to return it.
But before we do we're going to do a little bit of formatting on the e-mail just like we did up here
as well.
So from the body of the arrow function we'll say return new helper Daut capital E mail and we'll pass
in the e-mail that we just extracted.
OK so here we go again we're taking in the list of recipients which is our sub document collection.
We pass it off to format addresses for every recipient inside that array.
We pull off the e-mail property we format it with the email helper and then we return it.
So now recipients right here start recipients we can imagine is going to be an array of these new help
or don't e-mail things each of which wrap an actual e-mail that we want to send this e-mail off to.
That's a lot of saying e-mail.
I just said e-mail a lot.
I'm not going to say it anymore.
So I'm going to try not to you.
I doubt I can.
But whatever.
OK.
So this now has formatted and assigned all of our recipients to this dot recipients we're now ready
to move on to the next step.
So we set up our for kind of like golden properties here with e-mails from what the subject is the body
of it and the list of who to send it to.
Now there's one or two more steps we have to do.
We're going to do them in order of increasing difficulty.
So the first thing we're going to do is to make sure that the body right here this body we're going
to make sure that that gets added as the actual content to the mailer.
So just assigning the content here to the body property is not enough.
We have to actually kind of register this body with the email with the mailer itself.
So to do so we'll say this dot ad content will pass in this body like so now when you see this you're
going to say OK this is a helper function that we have to put together like we have to make a helper
function called out content.
And we're going to do something inside of there.
Oh no not quite.
Remember that our mailer is extending help or direct mail.
So this mail base class right here it has some built in functionality to it.
This is one of those built in functions.
So the mail class right here has a function called ad content and it expects you to call ad content
with the actual body that you want to use in the mail.
So at this point you can easily imagine that we could have refactored our code here to not assign anything
to this body we could've just said well yeah just you know do the content thing and then call add content
with it just like this.
Right we could just skip this entire body step right here.
Well we definitely could.
But personally I like how the mailer looks when we very clearly assign each of these properties and
then we make use of each of the properties later on down the constructor function.
It just makes it really easy to kind of feel the flow of code inside the mailer.
Again a lot of the code that we're putting together inside of here my best explanation I can give to
you is kind of this is how we have to do it with with grid.
And unfortunately that's kind of the reality here.
OK.
So two more steps here.
Let's take care of the easier one next.
Right underneath our content we're now going to enable click tracking inside of our email.
So remember we had said that any time we put an email or give me a link into one of our emails send
great grade We'll scan the email and automatically replace every one and there's every link with one
of their special own links.
And so that's what this clicked tracking thing that we're about to do is click.
Tracking is going to essentially enable this step right here.
So back inside of our mailer will say this dot ad click tracking.
Now this is going to be in a helper function that you and I are going to define.
So now underneath format addresses we're going to say ad click tracking inside of here.
We're going to create two helper variables and then use them to do a little bit of setup.
So we'll say Konst tracking settings is new helper dot tracking settings like so then Konst click tracking
is new helper Daut click tracking.
And we're going to pass in the arguments of true and true Okay now we're going to make use of these
two variables.
We're going to say trackings settings Daut set.
Click tracking and pass in click tracking.
So essentially take that one variable that we just declared and pass in the second one that we just
defined as well.
So click check or check settings track settings click tracking click tracking and then one last line
and here this dot ad tracking settings tracking settings like so.
OK.
So again I know this is painful stuff to write right here.
I know I feel terrible just writing it and saying hey this is just how centered works but unfortunately
this is basically how some grid works and I don't have a great explanation for you on you know the real
internals of what's going on here.
At best I could say read the documentation but the unfortunate thing is the documentation basically
says just write this code.
And that's pretty much it.
So hey for better for worse this is what we got to do.
All right.
Now there's one last little piece of setup that we need to do starting inside the constructor.
So this is going to be one of the more challenging ones just to kind of understand what's going on.
But the code itself won't be too bad.
So we're going to do the function call first.
We're going to call it this dot add recipients.
And I think at this point we'll take a quick break.
When we come back we're going to implement this.
Recipient's function right here inside of our mailer.
So a quick break and I'll see you in just a minute.