1 00:00:00,360 --> 00:00:05,360 In the last video we got a much better sense of exactly how the mailer would be put together. 2 00:00:05,360 --> 00:00:10,670 We said that the first argument would be an object that contains the subject of the email in the list 3 00:00:10,670 --> 00:00:16,160 of all the different people to send it to the recipients and then the second argument would be the actual 4 00:00:16,160 --> 00:00:19,520 body or the actual content of the e-mail itself. 5 00:00:19,520 --> 00:00:25,360 So we're now ready to change back over to our mailer J.S. file and start to implement the real mailer. 6 00:00:25,520 --> 00:00:29,720 So I got to change back over to my services mailer dot s file. 7 00:00:29,750 --> 00:00:35,230 So here's our mailer class and we're going to start to add to its implementation now. 8 00:00:35,540 --> 00:00:41,270 When we used the mailer inside of that survey route's file you may have noticed that we used a very 9 00:00:41,270 --> 00:00:44,720 specific signature to create an instance of the mailer. 10 00:00:44,750 --> 00:00:49,060 We said new mailer when we passed in some number of arguments right here. 11 00:00:49,100 --> 00:00:50,250 Right. 12 00:00:50,420 --> 00:00:55,460 Whenever we have a javascript class like this right here just as a reminder this is going but this is 13 00:00:55,460 --> 00:00:56,690 not anything having to do with email. 14 00:00:56,690 --> 00:00:57,380 Nothing like that. 15 00:00:57,380 --> 00:00:59,840 This is just plain as 2015. 16 00:00:59,900 --> 00:01:04,940 Class syntax whenever we call new on a class like this. 17 00:01:05,030 --> 00:01:11,540 The first function that is automatically executed inside the class is the constructor function. 18 00:01:11,630 --> 00:01:13,540 So do triple check your spelling here. 19 00:01:13,550 --> 00:01:16,490 We want very specifically constructor. 20 00:01:16,490 --> 00:01:20,930 So the constructor is called automatically whenever you use the new keyword like this. 21 00:01:20,930 --> 00:01:27,770 And it gives us an opportunity to do some amount of setup or initialization inside of the class instance. 22 00:01:27,770 --> 00:01:33,590 Now I'm going to delete the new statement down here because I just wanted to put it in as a for instance. 23 00:01:33,590 --> 00:01:38,720 So the constructor function is going to be called automatically for us any arguments that we use when 24 00:01:38,720 --> 00:01:43,280 we use the new keyword will be provided as arguments to the constructor function. 25 00:01:43,580 --> 00:01:49,610 So for us we can imagine that this thing is going to receive the survey and the actual template or body 26 00:01:49,610 --> 00:01:51,530 of the email. 27 00:01:51,650 --> 00:01:57,290 Now we're going to write our mailer in such a way that it's not necessarily strictly coupled to the 28 00:01:57,290 --> 00:02:03,260 survey model we're going to assume that this mailer can be used for other e-mails in the future as well. 29 00:02:03,350 --> 00:02:07,940 And that's why I've been putting a lot of emphasis to say that the first argument is just an object 30 00:02:08,240 --> 00:02:11,450 that has a subject and the recipient's property. 31 00:02:11,450 --> 00:02:17,690 So this does not necessarily have to be a survey model instance it just has to be an object with a subject 32 00:02:17,900 --> 00:02:18,950 and recipient property. 33 00:02:18,950 --> 00:02:20,980 That's all we really care about. 34 00:02:20,990 --> 00:02:27,370 So as the first argument to the constructor function will receive those two arguments with some destructuring 35 00:02:28,050 --> 00:02:30,160 will say here is our curly braces. 36 00:02:30,230 --> 00:02:38,240 We expect this first argument to be an object that contains a subject and recipients like so and then 37 00:02:38,240 --> 00:02:44,390 the second argument to a constructor function overall will be the content or the body of the email and 38 00:02:44,390 --> 00:02:48,740 we will receive that as an argument of Will does it content. 39 00:02:48,740 --> 00:02:55,310 So this will be the T.M. string that we got from our survey template right here. 40 00:02:55,620 --> 00:02:59,420 Look I think that this stuff hopefully is starting to come together a little bit better. 41 00:02:59,420 --> 00:03:03,810 So hopefully you're now starting to see how we might be able to reuse the Meyler in the future. 42 00:03:04,190 --> 00:03:08,390 We can call the miller in the future with a new subject a new list of recipients and some different 43 00:03:08,390 --> 00:03:12,390 content and boom we'll have an email put together and ready to go. 44 00:03:12,770 --> 00:03:15,690 So now it's time to do the actual set up inside of here. 45 00:03:15,800 --> 00:03:19,910 So we're going to start by implementing the constructor function. 46 00:03:20,090 --> 00:03:24,530 Now again as we start to do some of this stuff some of the code is going to look a little bit weird 47 00:03:24,650 --> 00:03:30,260 and I'm just going to remind you that my answer to just about any question you might have is remember 48 00:03:31,100 --> 00:03:35,170 this is we're doing it this way because this is what send grid expects us to do. 49 00:03:35,210 --> 00:03:35,440 All right. 50 00:03:35,450 --> 00:03:36,620 That's the answer. 51 00:03:37,140 --> 00:03:37,380 OK. 52 00:03:37,400 --> 00:03:39,030 So first thing it's out of the constructor. 53 00:03:39,260 --> 00:03:44,990 We are extending the male class so we're going to make sure that any constructor that is defined on 54 00:03:44,990 --> 00:03:49,180 the male class gets executed by calling the super function. 55 00:03:49,400 --> 00:03:51,800 So super right here this is not central not e-mail. 56 00:03:51,800 --> 00:03:52,480 This is yes. 57 00:03:52,530 --> 00:03:54,280 2015 classes. 58 00:03:54,410 --> 00:03:58,190 If you're not familiar with super feel free to read up on some documentation. 59 00:03:58,750 --> 00:03:59,010 OK. 60 00:03:59,030 --> 00:04:04,190 So now inside of here we're going to do some of the like send grids specific set up send the stuff where 61 00:04:04,190 --> 00:04:06,090 he said yeah this is just what we're doing. 62 00:04:06,130 --> 00:04:08,570 It's just how it how and how it has to happen. 63 00:04:08,610 --> 00:04:09,800 That's pretty much it. 64 00:04:10,200 --> 00:04:10,550 OK. 65 00:04:10,580 --> 00:04:16,050 So we're going to first start off by defining a property called from e-mail. 66 00:04:16,140 --> 00:04:18,290 Sorry to say this stop from e-mail. 67 00:04:18,440 --> 00:04:25,220 So whenever we make an instance of the mailer that instance we'll have a very specific from e-mail property 68 00:04:25,580 --> 00:04:30,440 and we're assigning that this property to it by referencing this link. 69 00:04:30,470 --> 00:04:35,750 So the e-mail is going to be who this e-mail appears to be sent from. 70 00:04:35,810 --> 00:04:41,780 Now for our application we're going to just assume that the front property will always be UMT like e-mail 71 00:04:41,820 --> 00:04:45,990 dot com us the people who are sending out all the different surveys. 72 00:04:46,070 --> 00:04:50,570 So here's where some of the interesting stuff comes into play. 73 00:04:50,570 --> 00:04:54,830 We're going to say new helper dot e-mail. 74 00:04:54,890 --> 00:05:00,980 We're going to pass in an e-mail of how about like no Dasch reply it's e-mail. 75 00:05:01,100 --> 00:05:02,080 Whoops. 76 00:05:02,430 --> 00:05:03,070 E-mail. 77 00:05:03,100 --> 00:05:03,960 DOT. 78 00:05:04,430 --> 00:05:05,360 Like so. 79 00:05:05,930 --> 00:05:07,190 So this just makes it really clear. 80 00:05:07,190 --> 00:05:11,750 If anyone tries to reply to the survey e-mails it's not going to get a reply. 81 00:05:11,810 --> 00:05:14,260 Sort of saying hey look don't bother e-mailing us. 82 00:05:14,270 --> 00:05:19,560 We're not going to reply to the next property we're going to define is the subject line. 83 00:05:19,570 --> 00:05:22,000 The e-mails will say this subject. 84 00:05:22,040 --> 00:05:27,050 And remember we are accepting that directly as an argument to the mailer itself so we can just bring 85 00:05:27,050 --> 00:05:32,150 in subject like so next is going to be the body of the email. 86 00:05:32,150 --> 00:05:34,270 So the actual HTL content. 87 00:05:34,400 --> 00:05:41,420 So we'll say this body is going to be new helper dot capitals see content. 88 00:05:41,690 --> 00:05:47,660 We're going to give it a first argument of text slash e-mail which indicates that this is going to be 89 00:05:48,050 --> 00:05:50,820 some H.T. mail that we want to show on the body of the e-mail. 90 00:05:51,020 --> 00:05:56,600 And in the second argument we'll be the output of our e-mail template that we passed in as this second 91 00:05:56,600 --> 00:05:58,160 argument to the constructor. 92 00:05:58,550 --> 00:06:01,180 So pass an content like so. 93 00:06:01,460 --> 00:06:05,490 Now one thing I want to point you back to on the content argument right here. 94 00:06:05,540 --> 00:06:11,630 Notice that we are only destructuring subject and recipients of the first argument to the constructor. 95 00:06:11,630 --> 00:06:13,490 So here is the opening curly brace. 96 00:06:13,550 --> 00:06:19,160 Here's the closing curly race content is the entire second argument to the constructor. 97 00:06:19,310 --> 00:06:19,990 It's a string. 98 00:06:20,000 --> 00:06:22,230 We're not destructuring it from anything. 99 00:06:22,250 --> 00:06:24,710 We're not structuring anything off of it. 100 00:06:24,740 --> 00:06:28,390 And so content does not have any curly braces around it. 101 00:06:29,110 --> 00:06:34,100 OK so this defines the body of our email and now comes the really really fun one. 102 00:06:34,130 --> 00:06:36,790 And as you might imagine it's not actually that fun. 103 00:06:36,890 --> 00:06:42,580 So the list of recipients this is the people that we are going to send our e-mail to. 104 00:06:42,580 --> 00:06:45,870 So we'll say this dot recipients. 105 00:06:46,180 --> 00:06:50,840 And for this we're going to send a set up a little helper function because we're going to have to do 106 00:06:50,840 --> 00:06:56,580 a pretty good amount of logic to specify the recipients correctly. 107 00:06:56,720 --> 00:07:02,440 So we're going to make a helper function called this Daut format addresses. 108 00:07:02,720 --> 00:07:07,780 And we're going to pass in the list of recipients that was passed into the mailer itself. 109 00:07:07,970 --> 00:07:09,880 So recipients. 110 00:07:10,540 --> 00:07:11,040 OK. 111 00:07:11,240 --> 00:07:15,160 Now at this point I think we're at a good place to take a break before we start to tackle this helper 112 00:07:15,170 --> 00:07:20,820 function because this is a lot of code that really just seems like it's essentially coming out of nowhere. 113 00:07:20,870 --> 00:07:21,470 Right. 114 00:07:21,770 --> 00:07:27,440 So I do want to do a review on what we've least accomplished so far before we end this video though. 115 00:07:27,470 --> 00:07:34,250 I want to really point out this word to you a format addresses especially if you are a non-native English 116 00:07:34,250 --> 00:07:35,040 speaker. 117 00:07:35,300 --> 00:07:43,350 The word address in English has to Deitz a very common typo even for native English speakers. 118 00:07:43,460 --> 00:07:48,410 Even for me in particular I do this type all the time very common to only put in one do you like that 119 00:07:48,500 --> 00:07:49,540 which is a typo. 120 00:07:49,880 --> 00:07:53,210 So address has two Ds inside of it. 121 00:07:53,210 --> 00:07:57,570 We are saying that there are going to be possibly multiple addresses or multiple people we are sending 122 00:07:57,570 --> 00:07:58,780 this email to. 123 00:07:58,800 --> 00:08:02,040 So we're going to make sure that we specify like we name it plural. 124 00:08:02,130 --> 00:08:05,100 So addresses so to us is. 125 00:08:05,190 --> 00:08:09,810 So basically just triple check your spelling here because otherwise you're going to see an error message 126 00:08:09,810 --> 00:08:16,190 when we try to run our code that says something like unknown function or this dot format addresses is 127 00:08:16,200 --> 00:08:20,010 not a function or undefined is not a function is probably the message you're going to see. 128 00:08:20,360 --> 00:08:20,620 OK. 129 00:08:20,640 --> 00:08:22,780 So at this point let's take a quick break. 130 00:08:22,800 --> 00:08:26,790 When we come back in the next section we're going to talk about some of the code we just wrote and we'll 131 00:08:26,790 --> 00:08:29,100 continue working on our mailer class. 132 00:08:29,250 --> 00:08:31,200 So I'll see you in just a minute.