WEBVTT 00:01.470 --> 00:06.720 So let's keep working on the reset your password functionality for our application, and so far what 00:06.720 --> 00:07.470 we have is this. 00:07.770 --> 00:11.410 When you go to the login page, you see the forgot password link. 00:11.430 --> 00:13.200 And as you can see, I misspelled that. 00:13.200 --> 00:14.610 So let's go fix that right now. 00:15.210 --> 00:20.640 Back to our I.D. and find out where that word is forgot password. 00:20.820 --> 00:22.290 Let's add an S in there. 00:22.410 --> 00:22.830 OK. 00:24.120 --> 00:29.520 So when someone clicks on that link, they go to the forgot password form. 00:31.420 --> 00:37.390 And they enter their email address and then that calls a back end route that doesn't exist yet and sends 00:37.390 --> 00:38.090 it some Jason. 00:38.110 --> 00:39.780 So let's go work on the back end route. 00:40.240 --> 00:48.970 So back to the ID and let's open up routes dush aiport go and I'm going to put the password reset link 00:49.450 --> 00:50.920 as a non protected route. 00:51.040 --> 00:51.910 So I'll put it right here. 00:53.200 --> 01:03.250 Muxtape up post because we are posting from our JavaScript and we'll make the url api dash slash forgot 01:05.500 --> 01:11.170 password, which I believe is what we called it in the JavaScript and we'll have the call handler that 01:11.170 --> 01:17.430 doesn't exist yet which we'll call send password reset email. 01:18.220 --> 01:21.850 So let's go create that handler or at least the start for that handler. 01:21.860 --> 01:24.060 So in Handler start a dash API. 01:24.460 --> 01:34.090 Let's go to the very bottom and I'll create a new function with the receiver of the application and 01:34.090 --> 01:39.100 it's going to be called Send Password, Reset Email and take the standard two parameters. 01:39.130 --> 01:40.120 A response writer. 01:43.340 --> 01:44.780 And a pointer to a request. 01:47.540 --> 01:52.300 OK, so this is getting some Jason, and we need to read that and that's really easy. 01:52.310 --> 01:54.950 Now we'll just find a variable with local payload. 01:55.460 --> 02:00.500 And it's a structure and it has only one field email, which is a string. 02:01.130 --> 02:03.230 And in JSON we call that email 02:06.080 --> 02:13.700 and now we try to read our JSON so we can just call Appletree Jason and give it the response rate or 02:13.820 --> 02:14.570 the request. 02:14.930 --> 02:20.330 And we're going to read the result into this payload variable, which is defined and we check for an 02:20.330 --> 02:20.570 error. 02:21.680 --> 02:22.850 Error is not equal to nil. 02:24.470 --> 02:33.440 We'll just send back that request and that requires w are coming here and we return because we don't 02:33.440 --> 02:34.190 want to go any further. 02:35.870 --> 02:42.110 But assuming that actually works, we want to send them an email with a link to the password request. 02:42.500 --> 02:47.330 We'll just get started with some dummy data and we'll take care of building a signed link in a lecture 02:47.330 --> 02:47.870 or two. 02:48.410 --> 02:54.350 So we're going to be sending some data to a standard template that will be turned into email and sent 02:54.350 --> 02:54.630 out. 02:54.710 --> 03:04.820 OK, so the variable we're going to use to pass this data to our template will be a simple struct that 03:04.820 --> 03:06.410 has linking it. 03:06.950 --> 03:10.760 And that link will be a string that I can put whatever I want for a link in there. 03:10.760 --> 03:15.380 So I'll just say data link is equal to somewhere else, so I'll make it 03:18.680 --> 03:19.820 a local university. 03:19.850 --> 03:22.840 You can put whatever you want and there will be changing that before too long. 03:23.550 --> 03:24.620 At this point. 03:25.460 --> 03:27.620 I want to send some mail now. 03:27.650 --> 03:32.930 I can do all of that logic right here in the handler, but since I'm probably going to be sending more 03:32.930 --> 03:38.720 than one email in, more than one kind of email from the back end API, which is what we're working 03:38.720 --> 03:44.920 on right now, maybe it makes sense to create a helper function that actually sends email for us. 03:45.110 --> 03:47.900 Now, of course, we can do that using the standard library. 03:47.930 --> 03:52.250 All the functionality we need to send email from the standard library exists. 03:52.550 --> 03:56.840 But there are already several very good packages out there that already solve this problem and make 03:56.840 --> 03:58.760 sending email much, much simpler. 03:58.940 --> 04:07.190 So you'll find on the course resources that this lecture a link to this package and it's called Goda 04:07.280 --> 04:10.340 Simple Dash Mail from Exit and it's on GitHub. 04:10.700 --> 04:14.110 And what I'm going to do is actually use that particular package. 04:14.120 --> 04:15.410 It's very easy to use. 04:15.450 --> 04:18.320 OK, so you can scroll down here and read all about it. 04:18.320 --> 04:19.790 It has all kinds of things. 04:20.120 --> 04:21.980 Here's the part that I want right now. 04:22.670 --> 04:25.250 Let's go get that particular package. 04:25.430 --> 04:31.340 OK, so back to my I'd open a terminal, clear the screen and paste in. 04:32.060 --> 04:37.700 Go get GitHub dotcom slash exit slash go dush simple dash mail slash v2. 04:38.210 --> 04:43.550 And if there's a V three out, by the time you take this course, use the V to just follow along with 04:43.550 --> 04:44.990 the exact version we're using. 04:44.990 --> 04:48.940 And then after the fact, if you want to upgrade to version three, go ahead and do that. 04:50.360 --> 04:53.240 So I go and get it and now it's available to me. 04:53.240 --> 04:56.180 It's in my go mod file and it's been downloaded and I can use it. 04:56.540 --> 05:02.570 So what I'm going to do is create a function to send mail and I'm going to do that specifically for 05:02.570 --> 05:03.380 the back end. 05:03.380 --> 05:05.620 We might be sending email from the front end as well. 05:05.780 --> 05:10.880 For right now, I'm going to assume that the mail that sent from the front end might be different than 05:10.880 --> 05:12.260 the mail that is sent from the back end. 05:12.260 --> 05:15.380 So I'm actually going to duplicate sending email functionality. 05:15.380 --> 05:22.690 And what I'll do is go to my API folder and I'll create a new file, which I'll call Maila dot go. 05:23.360 --> 05:26.600 OK, and of course that's in package main. 05:30.010 --> 05:35.530 And the first thing I'll do is assume that I'm going to store my templates because we'll be using templates 05:35.530 --> 05:36.220 to send mail. 05:36.640 --> 05:37.030 Right. 05:37.070 --> 05:41.200 In this directory API, in a folder called Templates. 05:41.590 --> 05:48.970 So I'll use go ahead, go colon embed templates folder. 05:48.970 --> 05:50.890 That doesn't exist yet, but it will very shortly. 05:51.160 --> 05:53.800 And then I'll create a variable right underneath that. 05:53.800 --> 05:56.740 And there can't be a space or this won't work there. 05:56.740 --> 06:06.820 And I'll call it email templates, email template FS for file system and some type embed dot FS and 06:06.820 --> 06:08.440 that should have done the import for me and it did. 06:08.620 --> 06:10.900 Now it's warning me that that folder doesn't exist. 06:11.500 --> 06:17.320 So let's create that folder right in the API directory, a new folder called Templates. 06:19.090 --> 06:22.930 OK, and it still telling me there's an error because it's an empty folder, but that's OK. 06:22.940 --> 06:23.980 We'll take care of that shortly. 06:25.300 --> 06:32.440 So now let's declare a function func with a receiver appointed application, and I'm going to call this 06:32.440 --> 06:35.410 simply send mail like that. 06:36.600 --> 06:39.200 And it doesn't need to be exported, but it doesn't hurt to do it either. 06:39.210 --> 06:43.470 So I'm going to leave it with a capital s for now and I'm going to take certain parameters in here. 06:43.500 --> 06:50.670 Now, when you're sending an email, it's from somebody and it's to somebody and it has a subject and 06:51.060 --> 06:54.320 it needs to use some kind of template, which I'll call Tampoe. 06:54.480 --> 06:55.800 All of those are strings. 06:56.110 --> 07:01.980 OK, and also I'm going to be passing some kind of data to this template in our case right now with 07:01.980 --> 07:06.510 password resets, it will be the link the user clicks on to reset his or her password. 07:06.720 --> 07:10.260 So I'm just going to call this data and I don't know what I'm really putting in there for any particular 07:10.260 --> 07:10.770 template. 07:11.250 --> 07:17.760 So I'll just make it an interface like that and we're going to potentially return an error. 07:19.260 --> 07:24.040 And for right now, I'm just going to put a return nil in there. 07:24.150 --> 07:26.010 So this gets rid of an error. 07:27.030 --> 07:30.180 And I know I'm going to be sending some email using templates. 07:30.180 --> 07:31.020 So let's create one. 07:31.620 --> 07:37.680 So create a new file in my templates folder and I'll call it password dash reset. 07:38.250 --> 07:44.160 And I'm going to give it the DOT html and then a template where you can use go HDMI or whatever you 07:44.160 --> 07:44.430 want. 07:44.430 --> 07:50.460 But I'm going to use template and every time I send email, I'm going to send one email that includes 07:50.460 --> 07:56.040 an HTML version, but it also includes a plain text version. 07:56.040 --> 08:03.570 So Password Reset is the name of my second file in the templates folder and I'll call this plain for 08:03.570 --> 08:05.600 plain text dot temple. 08:06.090 --> 08:10.960 OK, we'll populate that with content before too long, but let's not worry about that right now. 08:10.980 --> 08:17.490 So back in Mulago, OK, presumably that error is going to go away once we actually compile this program 08:17.490 --> 08:18.780 for right now, let's ignore it. 08:19.860 --> 08:27.930 So I'm getting from to subject the template and any data I want to pass to this email message I'm going 08:27.930 --> 08:28.330 to send. 08:28.380 --> 08:35.550 So the very first thing I'm going to do is to calculate the name of the file I want to use based upon 08:35.580 --> 08:38.370 the value template that we receive as a parameter. 08:38.910 --> 08:45.000 So I know I'm going to be executing two templates or pursing two templates, one for HTML and one for 08:45.000 --> 08:45.740 plain text. 08:46.320 --> 08:51.600 So the first thing I'll do is build the necessary string and I'm going to create a variable called template 08:52.110 --> 09:00.450 to render, and that's going to be assigned the value of using the format package as print F and the 09:00.450 --> 09:01.500 path to the template. 09:01.500 --> 09:06.270 I want to render for the first one and I'm going to do the HTML template first is going to be templates 09:06.300 --> 09:12.510 slash and then %s for the placeholder dot html dot temple. 09:13.400 --> 09:19.620 OK, so that gives me the path name using the embedded file system to the template I want to render. 09:21.970 --> 09:28.180 Now, let's try parsing that template, so storing the result in the variable T for template and checking 09:28.180 --> 09:35.410 for an error, those come from template dot new and we're going to let the auto import take care of 09:35.410 --> 09:36.070 that for us. 09:37.900 --> 09:42.250 I need you to give this template some kind of name, so I'll call it emailed HTML. 09:42.910 --> 09:44.530 OK, Dot. 09:45.100 --> 09:53.590 And this time I'm going to pass s and the file system I want to use is my email template EFAs. 09:54.560 --> 10:00.350 And now I put in a template to render and we check for an error, if error is not equal to nil. 10:01.000 --> 10:08.360 I'll print it out after error log print line error and I'll return the error. 10:09.870 --> 10:14.160 Now, seemingly a passed that successfully, successfully passed this template, but we still have to 10:14.160 --> 10:18.330 execute it and put some data in there, in our case it will be the link. 10:19.050 --> 10:22.880 So I'll create a variable TPL and it's a type of buffer. 10:23.910 --> 10:28.770 And that's where I'm going to store the template that I execute right here. 10:29.430 --> 10:33.810 If error equals t dot execute template. 10:35.830 --> 10:42.730 And we're putting the results into our Bitstamp buffer and we're going to execute something called body, 10:42.850 --> 10:46.630 which will be in the template we create in a minute, and we're going to put the data in there. 10:46.870 --> 10:49.760 And again, we check for an error if error is not equal to nil. 10:51.640 --> 10:56.140 Then again, we'll just after error log dot, print, line 11:00.490 --> 11:02.620 the error and return here. 11:06.580 --> 11:11.830 And if we get past that, then we can take from that bites dog buffer. 11:12.040 --> 11:20.610 The value of the final formatted message formatted message is a sign the value of T.L. dot string. 11:22.420 --> 11:27.250 So that variable formatted message contains the HTML version of our message. 11:27.400 --> 11:33.270 OK, now we need to do the plain text, which is almost the same logic template to render in this time. 11:33.520 --> 11:39.730 Now it's equal to formatter as print f again, it's in templates. 11:40.360 --> 11:44.740 Again, we're going to change the first part of the file we're looking for, but now we're looking for 11:44.740 --> 11:52.040 plain dot tempo and we substitute template the parameter we get up here. 11:52.570 --> 11:55.330 So now the path to the template we want to work with next. 11:55.330 --> 11:57.430 It ends in plain dot temple. 11:58.930 --> 12:04.060 So again, t an error or equal to template dot new. 12:05.650 --> 12:14.350 And this time I'll call the template emailed plan, which is purely for my purposes from part of the 12:14.350 --> 12:15.040 file system. 12:16.980 --> 12:22.950 And we're going to pass email template EFS, and now we're using template to render, which has a new 12:22.950 --> 12:26.000 value now and we check for an error. 12:26.010 --> 12:27.750 So I'll just copy and paste the stuff. 12:28.990 --> 12:29.620 Right here. 12:33.460 --> 12:34.320 Pasted in there. 12:36.900 --> 12:44.070 And we execute that template, so again, very simple, if error is equal to t execute template. 12:46.500 --> 12:53.460 Again, we're using TPO, we're putting it into TPO, we're still looking for body. 12:55.080 --> 12:57.780 We're still passing in data and we check for the error. 13:03.190 --> 13:12.800 And again, same logic here, so let's just copy that, paste it into here now, at this point, I can 13:12.800 --> 13:20.330 create another variable called play message and that's a sign the value of TPO dot string. 13:21.920 --> 13:27.440 So now I have two variables formatted message, which is the HTML version of the email and plain message, 13:27.440 --> 13:29.340 which is the plain text version of the email. 13:29.930 --> 13:34.340 So now it's time to start using that mail package we just included a little while ago. 13:35.240 --> 13:38.600 But before we do that, if you go back and look at the documentation for this. 13:41.950 --> 13:48.910 We actually have to specify things like the SMTP server, the port that we're going to use, the username, 13:48.910 --> 13:54.820 the password, all of these things have to be set up and there's a fair bit of configuration to take 13:54.820 --> 13:57.550 place before we can actually send an email. 13:58.030 --> 14:03.640 So in the next lecture, we're going to go set up a dummy email account on mail, trapped audio, and 14:03.640 --> 14:07.740 we'll start formatting and sending the email to our end user. 14:07.810 --> 14:09.670 And we're not going to send the actual link right now. 14:09.670 --> 14:13.720 As I said a while ago, we just want to get to the point where we can send an email.