In the last video we got a much better sense of exactly how the mailer would be put together.
We said that the first argument would be an object that contains the subject of the email in the list
of all the different people to send it to the recipients and then the second argument would be the actual
body or the actual content of the e-mail itself.
So we're now ready to change back over to our mailer J.S. file and start to implement the real mailer.
So I got to change back over to my services mailer dot s file.
So here's our mailer class and we're going to start to add to its implementation now.
When we used the mailer inside of that survey route's file you may have noticed that we used a very
specific signature to create an instance of the mailer.
We said new mailer when we passed in some number of arguments right here.
Right.
Whenever we have a javascript class like this right here just as a reminder this is going but this is
not anything having to do with email.
Nothing like that.
This is just plain as 2015.
Class syntax whenever we call new on a class like this.
The first function that is automatically executed inside the class is the constructor function.
So do triple check your spelling here.
We want very specifically constructor.
So the constructor is called automatically whenever you use the new keyword like this.
And it gives us an opportunity to do some amount of setup or initialization inside of the class instance.
Now I'm going to delete the new statement down here because I just wanted to put it in as a for instance.
So the constructor function is going to be called automatically for us any arguments that we use when
we use the new keyword will be provided as arguments to the constructor function.
So for us we can imagine that this thing is going to receive the survey and the actual template or body
of the email.
Now we're going to write our mailer in such a way that it's not necessarily strictly coupled to the
survey model we're going to assume that this mailer can be used for other e-mails in the future as well.
And that's why I've been putting a lot of emphasis to say that the first argument is just an object
that has a subject and the recipient's property.
So this does not necessarily have to be a survey model instance it just has to be an object with a subject
and recipient property.
That's all we really care about.
So as the first argument to the constructor function will receive those two arguments with some destructuring
will say here is our curly braces.
We expect this first argument to be an object that contains a subject and recipients like so and then
the second argument to a constructor function overall will be the content or the body of the email and
we will receive that as an argument of Will does it content.
So this will be the T.M. string that we got from our survey template right here.
Look I think that this stuff hopefully is starting to come together a little bit better.
So hopefully you're now starting to see how we might be able to reuse the Meyler in the future.
We can call the miller in the future with a new subject a new list of recipients and some different
content and boom we'll have an email put together and ready to go.
So now it's time to do the actual set up inside of here.
So we're going to start by implementing the constructor function.
Now again as we start to do some of this stuff some of the code is going to look a little bit weird
and I'm just going to remind you that my answer to just about any question you might have is remember
this is we're doing it this way because this is what send grid expects us to do.
All right.
That's the answer.
OK.
So first thing it's out of the constructor.
We are extending the male class so we're going to make sure that any constructor that is defined on
the male class gets executed by calling the super function.
So super right here this is not central not e-mail.
This is yes.
2015 classes.
If you're not familiar with super feel free to read up on some documentation.
OK.
So now inside of here we're going to do some of the like send grids specific set up send the stuff where
he said yeah this is just what we're doing.
It's just how it how and how it has to happen.
That's pretty much it.
OK.
So we're going to first start off by defining a property called from e-mail.
Sorry to say this stop from e-mail.
So whenever we make an instance of the mailer that instance we'll have a very specific from e-mail property
and we're assigning that this property to it by referencing this link.
So the e-mail is going to be who this e-mail appears to be sent from.
Now for our application we're going to just assume that the front property will always be UMT like e-mail
dot com us the people who are sending out all the different surveys.
So here's where some of the interesting stuff comes into play.
We're going to say new helper dot e-mail.
We're going to pass in an e-mail of how about like no Dasch reply it's e-mail.
Whoops.
E-mail.
DOT.
Like so.
So this just makes it really clear.
If anyone tries to reply to the survey e-mails it's not going to get a reply.
Sort of saying hey look don't bother e-mailing us.
We're not going to reply to the next property we're going to define is the subject line.
The e-mails will say this subject.
And remember we are accepting that directly as an argument to the mailer itself so we can just bring
in subject like so next is going to be the body of the email.
So the actual HTL content.
So we'll say this body is going to be new helper dot capitals see content.
We're going to give it a first argument of text slash e-mail which indicates that this is going to be
some H.T. mail that we want to show on the body of the e-mail.
And in the second argument we'll be the output of our e-mail template that we passed in as this second
argument to the constructor.
So pass an content like so.
Now one thing I want to point you back to on the content argument right here.
Notice that we are only destructuring subject and recipients of the first argument to the constructor.
So here is the opening curly brace.
Here's the closing curly race content is the entire second argument to the constructor.
It's a string.
We're not destructuring it from anything.
We're not structuring anything off of it.
And so content does not have any curly braces around it.
OK so this defines the body of our email and now comes the really really fun one.
And as you might imagine it's not actually that fun.
So the list of recipients this is the people that we are going to send our e-mail to.
So we'll say this dot recipients.
And for this we're going to send a set up a little helper function because we're going to have to do
a pretty good amount of logic to specify the recipients correctly.
So we're going to make a helper function called this Daut format addresses.
And we're going to pass in the list of recipients that was passed into the mailer itself.
So recipients.
OK.
Now at this point I think we're at a good place to take a break before we start to tackle this helper
function because this is a lot of code that really just seems like it's essentially coming out of nowhere.
Right.
So I do want to do a review on what we've least accomplished so far before we end this video though.
I want to really point out this word to you a format addresses especially if you are a non-native English
speaker.
The word address in English has to Deitz a very common typo even for native English speakers.
Even for me in particular I do this type all the time very common to only put in one do you like that
which is a typo.
So address has two Ds inside of it.
We are saying that there are going to be possibly multiple addresses or multiple people we are sending
this email to.
So we're going to make sure that we specify like we name it plural.
So addresses so to us is.
So basically just triple check your spelling here because otherwise you're going to see an error message
when we try to run our code that says something like unknown function or this dot format addresses is
not a function or undefined is not a function is probably the message you're going to see.
OK.
So at this point let's take a quick break.
When we come back in the next section we're going to talk about some of the code we just wrote and we'll
continue working on our mailer class.
So I'll see you in just a minute.