1 00:00:00,480 --> 00:00:05,590 In the last video we finished up the creation of a new survey inside of a handler. 2 00:00:05,640 --> 00:00:07,940 So here's our new instance of a survey. 3 00:00:07,980 --> 00:00:12,690 Remember that when we first create the survey right here this does not automatically save it to our 4 00:00:12,690 --> 00:00:13,910 Mongo database. 5 00:00:13,980 --> 00:00:19,590 We have to actually call save on the survey to make sure that it actually gets persisted. 6 00:00:19,620 --> 00:00:24,840 Now before we go any further on this route handler I want to mention that the rat handler we are working 7 00:00:24,840 --> 00:00:29,350 on is going to eventually contain a pretty good amount of logic inside of it. 8 00:00:29,670 --> 00:00:34,350 Now before we start to just write out all that logic I want to give you a little bit of a preview on 9 00:00:34,350 --> 00:00:36,460 exactly what we have to do inside of here. 10 00:00:36,600 --> 00:00:41,790 Outside of just creating the survey and I want to make sure that you kind of understand the entire flow 11 00:00:41,820 --> 00:00:43,840 of this e-mail sending process. 12 00:00:44,040 --> 00:00:49,530 So in this video and the next one we're going to expand a whole bunch on exactly how we take this survey 13 00:00:49,530 --> 00:00:54,930 object and how we use it to create and send a brand new e-mail to all the different recipients that 14 00:00:54,930 --> 00:00:57,230 are listed inside of it. 15 00:00:57,240 --> 00:01:03,690 Now I first want to remind you back that this diagram right here that we're currently working on this 16 00:01:03,690 --> 00:01:07,770 post request route handler where we are creating a new survey. 17 00:01:08,220 --> 00:01:11,460 We probably could have put in a slightly better description right here. 18 00:01:11,460 --> 00:01:19,740 In truth this is being used to create a new survey and send out a big email. 19 00:01:19,920 --> 00:01:22,170 So at the same time we created the survey. 20 00:01:22,170 --> 00:01:28,440 We also create the email and send out send it to every single one of the recipients because remember 21 00:01:28,500 --> 00:01:33,300 at the end of the day we want to make sure that every single recipient gets something that looks like 22 00:01:33,300 --> 00:01:33,870 this. 23 00:01:33,980 --> 00:01:38,030 That says hey click on a link to give us a little bit of feedback. 24 00:01:38,480 --> 00:01:38,980 OK. 25 00:01:39,120 --> 00:01:40,970 So that was all just a little bit a reminder. 26 00:01:41,040 --> 00:01:46,520 I now want to dive into making sure you understand how we are going to create and send this e-mail. 27 00:01:46,680 --> 00:01:49,660 I want to make sure you understand what we're going to do in that role händler. 28 00:01:49,930 --> 00:01:54,510 And I also want to make sure that you understand exactly how we are going to make sure that whenever 29 00:01:54,510 --> 00:02:01,380 a user clicks on one of these buttons we somehow understand exactly whether or not they clicked on yes 30 00:02:01,410 --> 00:02:02,010 or no. 31 00:02:02,160 --> 00:02:07,170 And make sure that we attribute some feedback to the survey that this email belongs to. 32 00:02:07,630 --> 00:02:07,910 OK. 33 00:02:07,950 --> 00:02:08,940 So let's get to it. 34 00:02:10,500 --> 00:02:15,830 So this is what we're going to do inside of the handler that we are currently working on at this point. 35 00:02:15,960 --> 00:02:19,320 We have created a new instance of a survey. 36 00:02:19,650 --> 00:02:24,970 Very shortly we're going to add in some code that is going to attempt to create and send off an e-mail. 37 00:02:25,140 --> 00:02:30,000 And remember this is going to be an e-mail that is sent to every single recipient that is listed inside 38 00:02:30,000 --> 00:02:34,220 the survey after we attempt to send out the e-mail. 39 00:02:34,230 --> 00:02:38,340 We're going to say OK did we make the e-mail do we send it off successfully. 40 00:02:38,700 --> 00:02:44,970 And if we did do all that stuff successfully we will then save this survey to our database. 41 00:02:44,970 --> 00:02:48,690 And so this is kind of a important order of operations right here. 42 00:02:48,720 --> 00:02:53,910 Note that we are not going to save this survey to the database until we are really sure that the e-mail 43 00:02:53,910 --> 00:02:59,220 has actually been sent out successfully because obviously if we can't send the e-mail for some crazy 44 00:02:59,220 --> 00:03:01,930 reason we probably don't want to save the survey. 45 00:03:01,980 --> 00:03:05,140 We want to tell the user who is attempting to make this new survey. 46 00:03:05,250 --> 00:03:10,600 Hey something went wrong while we were attempting to create the survey so sorry but it was not created. 47 00:03:10,620 --> 00:03:12,120 Try again later. 48 00:03:12,150 --> 00:03:16,220 So only after we send off the e-mail will we actually save the survey. 49 00:03:16,650 --> 00:03:19,410 So after the survey gets saved that's it. 50 00:03:19,410 --> 00:03:21,100 Survey out Candler complete. 51 00:03:21,150 --> 00:03:21,510 That's it. 52 00:03:21,510 --> 00:03:23,870 That's all we have to be aware of. 53 00:03:23,870 --> 00:03:24,340 OK. 54 00:03:24,660 --> 00:03:27,630 So that's essentially what's going on inside this raw handler. 55 00:03:27,750 --> 00:03:31,860 So obviously we need to know a little bit more about how we create and send an e-mail. 56 00:03:31,950 --> 00:03:34,160 So let's talk a little bit more about that right now. 57 00:03:35,630 --> 00:03:36,090 OK. 58 00:03:36,140 --> 00:03:39,530 So a couple of different moving pieces of the puzzle here. 59 00:03:39,530 --> 00:03:43,680 At the very top we've got the science survey instance that we just created. 60 00:03:43,760 --> 00:03:48,640 So this survey instance kind of describes a little bit about the email we're trying to send out. 61 00:03:48,650 --> 00:03:51,140 It records the subject line of the e-mail. 62 00:03:51,260 --> 00:03:55,970 The body text and the list of recipients who should receive this e-mail. 63 00:03:56,270 --> 00:04:01,370 That's just the data layer though that's just the kind of stuff that says Here is the content of the 64 00:04:01,370 --> 00:04:01,940 e-mail. 65 00:04:02,000 --> 00:04:03,700 And here's what should be sent to. 66 00:04:03,740 --> 00:04:09,260 But remember we still need some code that describes where we need to show these two buttons at the bottom 67 00:04:09,260 --> 00:04:09,910 of the screen. 68 00:04:10,010 --> 00:04:13,730 All right we need to show the yes and no buttons down here and we need to make sure that they are links 69 00:04:14,120 --> 00:04:19,820 and maybe we want to also make sure that our e-mail has some really neat styling attached to it as well. 70 00:04:19,910 --> 00:04:24,150 That's going to be the purpose of this e-mail template thing over here. 71 00:04:24,170 --> 00:04:30,080 So this survey instance we have communicates kind of the data that should be contained inside that e-mail. 72 00:04:30,350 --> 00:04:35,570 The e-mail template they were going to create communicates the structure or the look or the design of 73 00:04:35,570 --> 00:04:36,890 the e-mail. 74 00:04:36,890 --> 00:04:42,950 In practice this e-mail template is essentially going to be a blob of e-mail that describes what the 75 00:04:42,950 --> 00:04:44,900 body of that e-mail should look like. 76 00:04:44,990 --> 00:04:51,630 So we can make use of this e-mail template to customize the look and the feel of the body of the email. 77 00:04:52,150 --> 00:04:55,930 Now once we create both these things and obviously we've already created the survey instance. 78 00:04:55,930 --> 00:05:01,210 So once we also create the e-mail template as well we're going to merge the two of them together inside 79 00:05:01,210 --> 00:05:05,500 of an object that we will refer to as the Meilleur object. 80 00:05:05,500 --> 00:05:12,610 This mailer object thing represents one single e-mail that we want to send out to a list of possible 81 00:05:12,610 --> 00:05:16,410 people either one person or a thousand people. 82 00:05:16,420 --> 00:05:22,320 So in mailer you can think of as being a helper that helps us generate an email and actually get it 83 00:05:22,360 --> 00:05:27,840 sent off to some number of people after we create this mailer thing. 84 00:05:27,840 --> 00:05:32,700 We will then take that mailer and we will send it off to our e-mail provider. 85 00:05:32,700 --> 00:05:38,070 Remember you and I are not going to be interfacing with some type of e-mail server directly. 86 00:05:38,070 --> 00:05:43,680 Instead we're going to make use of an API that will help us automatically send out these e-mails to 87 00:05:43,710 --> 00:05:47,640 all the different people who are indicated inside the recipient list. 88 00:05:47,640 --> 00:05:53,550 So again you and I take the kind of contents of the e-mail or kind of the data layer or the e-mail. 89 00:05:53,700 --> 00:05:56,620 We're going to make a sort of view layer for the e-mail. 90 00:05:56,700 --> 00:06:01,980 We're going to merge the two together inside of this mailer object then we will send this mailer object 91 00:06:02,040 --> 00:06:10,770 over an H TTP request to our outside third party e-mail provider the e-mail provider will then be responsible 92 00:06:10,830 --> 00:06:14,510 for taking that e-mail and sending it off to all the different recipients. 93 00:06:14,640 --> 00:06:21,690 And so eventually each recipient should see something inside their inbox looking like this right here. 94 00:06:21,690 --> 00:06:26,850 Now obviously this only describes the very high level of the mailer itself and it doesn't actually describe 95 00:06:26,850 --> 00:06:31,590 hey how do we really truly handle e-mail inside of an express application. 96 00:06:31,620 --> 00:06:36,030 And of course that's going to be some of the stuff that we dive into as we start to actually implement 97 00:06:36,030 --> 00:06:37,160 the mailer. 98 00:06:37,890 --> 00:06:45,480 Now the mailer itself and how we communicate the mailer over to this e-mail provider is a rather complicated 99 00:06:45,480 --> 00:06:46,510 subject. 100 00:06:46,530 --> 00:06:49,120 So at this point I want to take a quick break. 101 00:06:49,140 --> 00:06:54,930 When we come back we're going to dive in great detail into exactly how we take this mailer object right 102 00:06:54,930 --> 00:06:55,330 here. 103 00:06:55,350 --> 00:07:01,140 And remember the mailer represents an e-mail that we want to send inside of our codebase how we take 104 00:07:01,140 --> 00:07:08,080 that mailer and somehow make a request with it off to this outside e-mail provider. 105 00:07:08,100 --> 00:07:08,910 So a quick break. 106 00:07:08,910 --> 00:07:15,120 We'll come back and we'll discuss why this relationship right here is going to be so challenging particularly 107 00:07:15,120 --> 00:07:16,620 for our application. 108 00:07:16,620 --> 00:07:18,400 So I'll see you in just a minute.