In the last video we finished up the creation of a new survey inside of a handler.
So here's our new instance of a survey.
Remember that when we first create the survey right here this does not automatically save it to our
Mongo database.
We have to actually call save on the survey to make sure that it actually gets persisted.
Now before we go any further on this route handler I want to mention that the rat handler we are working
on is going to eventually contain a pretty good amount of logic inside of it.
Now before we start to just write out all that logic I want to give you a little bit of a preview on
exactly what we have to do inside of here.
Outside of just creating the survey and I want to make sure that you kind of understand the entire flow
of this e-mail sending process.
So in this video and the next one we're going to expand a whole bunch on exactly how we take this survey
object and how we use it to create and send a brand new e-mail to all the different recipients that
are listed inside of it.
Now I first want to remind you back that this diagram right here that we're currently working on this
post request route handler where we are creating a new survey.
We probably could have put in a slightly better description right here.
In truth this is being used to create a new survey and send out a big email.
So at the same time we created the survey.
We also create the email and send out send it to every single one of the recipients because remember
at the end of the day we want to make sure that every single recipient gets something that looks like
this.
That says hey click on a link to give us a little bit of feedback.
OK.
So that was all just a little bit a reminder.
I now want to dive into making sure you understand how we are going to create and send this e-mail.
I want to make sure you understand what we're going to do in that role händler.
And I also want to make sure that you understand exactly how we are going to make sure that whenever
a user clicks on one of these buttons we somehow understand exactly whether or not they clicked on yes
or no.
And make sure that we attribute some feedback to the survey that this email belongs to.
OK.
So let's get to it.
So this is what we're going to do inside of the handler that we are currently working on at this point.
We have created a new instance of a survey.
Very shortly we're going to add in some code that is going to attempt to create and send off an e-mail.
And remember this is going to be an e-mail that is sent to every single recipient that is listed inside
the survey after we attempt to send out the e-mail.
We're going to say OK did we make the e-mail do we send it off successfully.
And if we did do all that stuff successfully we will then save this survey to our database.
And so this is kind of a important order of operations right here.
Note that we are not going to save this survey to the database until we are really sure that the e-mail
has actually been sent out successfully because obviously if we can't send the e-mail for some crazy
reason we probably don't want to save the survey.
We want to tell the user who is attempting to make this new survey.
Hey something went wrong while we were attempting to create the survey so sorry but it was not created.
Try again later.
So only after we send off the e-mail will we actually save the survey.
So after the survey gets saved that's it.
Survey out Candler complete.
That's it.
That's all we have to be aware of.
OK.
So that's essentially what's going on inside this raw handler.
So obviously we need to know a little bit more about how we create and send an e-mail.
So let's talk a little bit more about that right now.
OK.
So a couple of different moving pieces of the puzzle here.
At the very top we've got the science survey instance that we just created.
So this survey instance kind of describes a little bit about the email we're trying to send out.
It records the subject line of the e-mail.
The body text and the list of recipients who should receive this e-mail.
That's just the data layer though that's just the kind of stuff that says Here is the content of the
e-mail.
And here's what should be sent to.
But remember we still need some code that describes where we need to show these two buttons at the bottom
of the screen.
All right we need to show the yes and no buttons down here and we need to make sure that they are links
and maybe we want to also make sure that our e-mail has some really neat styling attached to it as well.
That's going to be the purpose of this e-mail template thing over here.
So this survey instance we have communicates kind of the data that should be contained inside that e-mail.
The e-mail template they were going to create communicates the structure or the look or the design of
the e-mail.
In practice this e-mail template is essentially going to be a blob of e-mail that describes what the
body of that e-mail should look like.
So we can make use of this e-mail template to customize the look and the feel of the body of the email.
Now once we create both these things and obviously we've already created the survey instance.
So once we also create the e-mail template as well we're going to merge the two of them together inside
of an object that we will refer to as the Meilleur object.
This mailer object thing represents one single e-mail that we want to send out to a list of possible
people either one person or a thousand people.
So in mailer you can think of as being a helper that helps us generate an email and actually get it
sent off to some number of people after we create this mailer thing.
We will then take that mailer and we will send it off to our e-mail provider.
Remember you and I are not going to be interfacing with some type of e-mail server directly.
Instead we're going to make use of an API that will help us automatically send out these e-mails to
all the different people who are indicated inside the recipient list.
So again you and I take the kind of contents of the e-mail or kind of the data layer or the e-mail.
We're going to make a sort of view layer for the e-mail.
We're going to merge the two together inside of this mailer object then we will send this mailer object
over an H TTP request to our outside third party e-mail provider the e-mail provider will then be responsible
for taking that e-mail and sending it off to all the different recipients.
And so eventually each recipient should see something inside their inbox looking like this right here.
Now obviously this only describes the very high level of the mailer itself and it doesn't actually describe
hey how do we really truly handle e-mail inside of an express application.
And of course that's going to be some of the stuff that we dive into as we start to actually implement
the mailer.
Now the mailer itself and how we communicate the mailer over to this e-mail provider is a rather complicated
subject.
So at this point I want to take a quick break.
When we come back we're going to dive in great detail into exactly how we take this mailer object right
here.
And remember the mailer represents an e-mail that we want to send inside of our codebase how we take
that mailer and somehow make a request with it off to this outside e-mail provider.
So a quick break.
We'll come back and we'll discuss why this relationship right here is going to be so challenging particularly
for our application.
So I'll see you in just a minute.