In the last video we started putting together our mailer class now between the last video and this one.
I was just sitting here for a little bit and I realized that it might be a little bit challenging to
understand exactly what the mailer does.
If we just dive face first into it and start putting together it's internal implementation.
So I'm thinking that maybe we need to first change gears just a little bit.
Maybe we need to first kind of see where the mailer would be used inside of our application and see
what type of data we would pass into it once we see what data we would pass in.
Once we see some of exactly how we would make use of the mailer I think that what we do inside the mailer
ourselves like the code we write in here will make a lot more sense.
So right now I'm thinking we'll put a very temporary break or a quick pause on working on the mailer
for now.
We will imagine for just a second that it is 100 percent complete.
Like let's just imagine that we sat down and finished all the code inside this thing.
So let's imagine that it's all done and we now want to use it somewhere inside of our codebase.
So let's go through that kind of process.
And I think as we do it you'll get a better idea of exactly what we have to accomplish inside the mailer.
So before we pretend that's going that it's going to be used anywhere.
I do want to put an export statement at the bottom of this file so that we can actually import it into
a different file and really truly pretend.
Exactly how are we going to make use of it.
So my first question to you is where do we want to use this mailer.
Like we're inside of our project.
Do we want to create and send a new mail.
Well probably in the rat handler that we are just working on right inside of the routes survey redstart
G-S file.
We've got our route handler right here.
Here's where it Kirt where we created our survey and we had said that right after we created our survey
we would create an attempt to send an email and then immediately after that after the email was successfully
sent we would save that survey to our database.
So I think that we should require the mailer into this file and get a very solid a very clear example
of how we might actually make use of it.
So at the top of this file underneath our existing require statements I'm going to require in that mail
mailer helper or the mailer class.
So we'll say Konst Mailer is require dot dot slash services slash mailer like so.
So now we'll scroll back down towards the bottom of our handler.
Here's where we create our survey and we're going to imagine that right after we create our survey.
Maybe right here would be a great place to send an e-mail OK.
So the first thing I want to think about is exactly how do we send an email how do we create that mailer
how do we use a class to somehow make something here and send off an email.
Well anytime we use a class inside of javascript to create a new instance of a class or to create an
object that represents an email that we want to send off to send grid.
We always use the new keyword.
So maybe right here we would say something like Konst Meilleur equals new Meyler like so.
And then to customize exactly how the mailer gets set up to customize what content it shows to the user
to customize who or what email addresses we're sending it to.
We need to make sure that we pass it some amount of configuration as an argument.
So remember we haven't wired this stuff up yet.
I'm just kind of imagining to myself exactly how I would want to use this mailer object.
So I'm going to assume that as a first argument to the mailer when I create a new instance of it I'm
going to pass in some data that describes like the title of the email or maybe the subject line maybe
I'll pass on something that specifies who the e-mail is from and so on.
So to the mailer I'm going to pass in the entire survey object right now because that contains a lot
of data that kind of specifies exactly what data or what e-mail we want to create and who we want to
send it to.
So let's just imagine for a second that we pass in the entire survey.
Now the other thing we need to be aware of inside the survey is we need to or soon excuse me the mailer.
The thing we need to be aware of.
When you think about what content we actually want to sort of the user like what actual each T.M. should
be displayed inside the email for that we need to think back to this diagram over here.
So I've updated just a little bit to add on a couple extra or next extra step here to represent the
template.
So the template is going to be the thing that produces the actual body of the email the actual age T.M.
that shows the user some amount of content when they open the email and sign their email client.
So we're going to centralize all of this logic inside of a function that we're going to call the template
we will receive some data into the template like maybe the body from the survey because remember we've
been saying all along that the survey model contains the body of the email.
So we said yes the survey will receive this body thing and that was give me the text to show inside
the survey.
So we're going to pass in survey body to his template function thing and then we're going to assume
that the template should return some Each T.M. to assign to the body property of the mailer which will
eventually be the actual body of the e-mail itself.
So how about let's put together a template thing and get a slightly better idea of how that's going
to work as well.
So just to before we start that really quickly I want to mention we're going to assume that we're going
to pass in the template or the H.T. male to use for the body of this e-mail as the second argument to
it.
So I'm going to pass in as the second argument.
We're going to say template like so.
So let's now create this template thing and get a better sense of exactly what it's going to look like
since this is closer closely related to emails and closely related to my mailer.
I'm going to make a new folder inside the services directory and I'm going to call it email templates
so we all imagine that if we eventually have many different email templates inside of our application
we'll place them all inside of this directory right here.
Now inside this file folder I'm going to make a new file that's going to serve as the template for any
survey email I send out.
I'm going to call this thing survey template.
Yes.
Now inside of here we're going to define an export function.
And whenever you call this function and we return some e-mail to be used as the body of any survey email
that our application sends out.
So at the very top we'll define and return an arrow function and and this thing is going to contain
all the actual H.T. mail that will be displayed inside of any email we send out.
So right now I think that we can start off pretty easy pretty straightforward.
And once we get a better sense of how all this email stuff works we'll come back to the template and
really kind of enhance its functionality over time so we'll add in a lot more markup over time.
For right now I think that we can just return a string that will function as the body of the email.
So the actual HVM of the email.
So let's give it like just a div.
How about that and we'll say hello there.
Lexa OK.
So that's a good place to start.
Now we have said several times however that we want to make sure that we communicate some data from
the survey model itself over to the template.
So maybe we should kind of forge this link right here and assume that the template function will be
called with a survey.
So OK let's say that the survey is going to be passed in here and we'll say that we'll take the body
property off the survey and show it inside the template.
So I'm going to assume that we're going to receive the survey as an argument remember that our survey
model has a body property and that is the actual body text that our user wants to show inside the email.
So we'll take that property and we'll show it inside this div right here.
So rather than saying hello there let's show a string and we'll just do some simple string concatenation
and say serve a dot body like so OK.
Now this would definitely be a good place to use a little bit of E.S. six string interpolation if you
want to.
We will come back later and refactor this to use that.
But right now I'm thinking let's just keep things straightforward and easy.
OK.
So we've now got a function that takes a survey and returns a string that represents the actual age
email that we want to show in the body of our email.
So I think this is looking good.
Let's now close out that file will import that survey template helper into our survey route's file at
the very top and we'll make sure that we pass that along to our mailer so the top will now require in
survey template it will require that from dot dot slash services email templates serve a template like
so
Snel survey template downside the mailer.
So here's the mailer right here.
We're going to say that this is not going to be this template we're going to say this is going to be
specifically serve a template.
We said that it was a function and we expect that function to be called with the survey object like
so or the survey model.
OK.
So I know that this has been kind of a very huge leap of faith at this point.
It's been a huge leap of faith but I wanted to make sure before we start to started to fill out any
of the internal implementation side the mailer that you got a better sense of exactly how it would be
used inside of our application.
So at this point we are assuming that any time in our application that we want to send out an e-mail
we're going to pass in some object that contains a subject property and a body property or something
just a subject property really that's what we care about the subject property and the recipient's property.
So the subject line to use for the e-mail and the people that send it to and then as a second argument
will pass in the content of the actual e-mail like the body of the e-mail that each T.M. to show inside
the the e-mail itself.
So we now have a very definite method signature here for whenever we create a new mailer.
First argument is going to be an object with subject and recipients.
Second argument will be the age to e-mail to use inside the body of the e-mail.
So we've now got a much better idea of what the mailer itself is going to look like.
So let's take a quick break.
We'll continue in the next section and we'll start to fill out the actual implementation of the mailer
itself.
So I'll see you in just a minute.