WEBVTT 00:01.040 --> 00:06.950 So now that we have an account on male trapped IO, it's time to keep working on sending email from 00:06.950 --> 00:07.450 the back end. 00:07.940 --> 00:11.620 And before we go too much further, just a couple of things I needed to clean up. 00:11.630 --> 00:20.150 No one here in Mulago online 14, I left out the second argument for formatter as print if I added it 00:20.150 --> 00:20.680 right here. 00:20.690 --> 00:22.580 So this part needs to be added. 00:22.580 --> 00:23.910 And I'm sure you noticed that. 00:23.970 --> 00:24.380 OK. 00:25.190 --> 00:32.660 And also on the password reset page itself, forgot password, the one that allows us to initiate the 00:32.660 --> 00:33.260 process. 00:33.830 --> 00:38.570 This said log in and I've changed that back to send password reset link. 00:38.930 --> 00:42.170 So little sloppy on my part and certainly not critical for this part. 00:42.170 --> 00:45.550 But the first one is we wouldn't have been able to render a template for email. 00:45.560 --> 00:46.430 So let's keep going. 00:47.790 --> 00:54.410 So back in our Mulago, now that we have our credentials, we actually need to start sending email. 00:54.420 --> 00:59.100 So I added this line right here just so I could compile and run it and make sure that everything works 00:59.100 --> 01:01.830 because we weren't using formatted message or plain message. 01:01.840 --> 01:06.960 And as you know, God doesn't like unused variables inside functions, but that's just temporary and 01:06.960 --> 01:08.460 we'll be getting rid of that before too long. 01:08.820 --> 01:16.200 So now we actually want to send the mail, send the message, OK, in order to do that, of course, 01:16.200 --> 01:23.010 if we go back to mail trapped oh, right here, we need to use these credentials. 01:23.160 --> 01:26.640 And I don't want to hard code these into my application. 01:26.970 --> 01:31.500 Instead, I want them to be a command line parameters or environment variables or something like that. 01:31.500 --> 01:37.140 So I can actually work in development and not have to change the actual source code before I go live. 01:38.220 --> 01:39.200 So to make that happen. 01:41.040 --> 01:48.870 Let's go back to our Idy and let's open up Apptio, which is the main entry point for our application. 01:49.620 --> 01:57.060 And what I'm going to do is I'm going to add some values to my config type for NMT, and I'll call it, 01:57.060 --> 02:04.440 not surprisingly, as A.P. And it's going to be a struct get rid of that space. 02:06.540 --> 02:13.020 And the members for that struck will consist of the host, which is a string, and the port, which 02:13.020 --> 02:21.120 is an end, and the user name for the service for our e-mail server, which is a string, and the password, 02:21.660 --> 02:22.640 which is also a string. 02:22.950 --> 02:25.020 And this is sufficient for our purposes right now. 02:25.030 --> 02:26.070 So let's format this. 02:26.320 --> 02:33.660 OK, now down here, we can actually read that information and I'll just read it from the command line 02:33.660 --> 02:34.860 is command line parameters. 02:35.940 --> 02:40.050 So right here I'll just duplicate this line because the first one I want is a string. 02:40.050 --> 02:43.740 I'm not going to read it into CFG DB DSN. 02:43.980 --> 02:53.640 I'm going to read it into CFT or CFG SMTP post and I'll call the command line parameter, the SMTP host 02:54.630 --> 02:55.980 and the value 02:58.890 --> 03:00.630 we got right from our mail page. 03:00.630 --> 03:03.720 And that was a TPG mail trap. 03:04.380 --> 03:04.680 OK. 03:06.800 --> 03:12.460 And we call this a S.P. host for the Help text, and the next thing I want is a port. 03:12.470 --> 03:18.190 So I have here flag infa, so a copy and paste pasted here. 03:19.040 --> 03:27.530 And so, again, I'm not reading into config port and reading into CFG as in TP Port, and we'll call 03:27.530 --> 03:29.190 the command line Fleg. 03:29.240 --> 03:35.420 The S.P. port will make a default to one of the values available to us five eighty seven. 03:35.450 --> 03:39.800 That's one of the ones that I got from the mail truck configuration information. 03:40.610 --> 03:41.900 And this will be the. 03:43.510 --> 03:44.230 To people. 03:45.360 --> 03:48.820 OK, and we also want the username and password. 03:48.840 --> 03:55.740 So those are both strings, so I'll just duplicate that line twice and the first one will be user name 03:56.490 --> 04:01.640 and we'll call it a SMTP username or just some TV user. 04:01.660 --> 04:02.250 Make a short. 04:04.070 --> 04:08.890 And mine are copied already, so I will paste that in here. 04:10.250 --> 04:15.740 And again, this one won't work, so don't try using this, because I'll have reset my credentials before 04:16.370 --> 04:17.390 this course goes live. 04:17.390 --> 04:21.590 So SMTP user and the SMTP password. 04:23.930 --> 04:32.960 It was SMTP password, and we'll call this a SMTP pass and I'll just paste in the one that I copied 04:32.960 --> 04:34.610 from my credential, SPREP. 04:35.910 --> 04:43.050 There is so this is a S.P. password, so now I have all of these things available to me, the next time 04:43.050 --> 04:49.980 I start and stop and start my application so I can go back to Maila, don't go and I can start configuring 04:49.980 --> 04:50.330 this. 04:50.850 --> 04:52.970 So let me give yourselves a little bit of room here. 04:54.180 --> 04:55.320 The first thing I want to do. 04:56.610 --> 05:03.420 Is to create a server and some type client, so I'll call that server and it's mailed out and the mail 05:03.420 --> 05:07.290 comes from the mail package that we're going to import here. 05:07.290 --> 05:08.730 And hopefully it will import it for me. 05:09.300 --> 05:14.400 New s.M TPE client right there. 05:16.520 --> 05:22.400 And that requires no parameters, so that creates a server variable for me when I start populating its 05:22.400 --> 05:23.190 various fields. 05:23.210 --> 05:24.710 So server host. 05:27.110 --> 05:28.090 Do that important for me. 05:30.290 --> 05:30.650 Yep. 05:37.290 --> 05:51.820 Server host equals APTA, config SMTP, dot host server dot port is equal to abdon config SMPTE dot 05:51.840 --> 05:52.260 port. 05:56.120 --> 05:57.440 Server username. 06:00.450 --> 06:05.100 Is equal to abduct, configure, SMTP, dot username 06:07.740 --> 06:09.060 server, dot password. 06:12.850 --> 06:16.560 Is equal to after configures SMTP password. 06:18.100 --> 06:24.400 And we need a few more things as well, so I'm using male trapped oil, so I will specify RTLS encryption 06:24.910 --> 06:26.200 server encryption. 06:29.910 --> 06:38.850 Is equal to male dot encryption code, again, encryption should tell us. 06:41.570 --> 06:45.710 And I'm going to keep alive defaults because I don't want to keep the connection to the SMTP server 06:45.710 --> 06:48.260 open server to keep alive 06:50.960 --> 07:01.250 Zippel false and I'll put a couple of timeouts server that connect timeout is equal to ten seconds. 07:01.260 --> 07:12.500 So 10 times time dot second and server dot send the time out is equal to the same thing 10 times time 07:12.500 --> 07:13.280 dot second. 07:14.820 --> 07:21.770 OK, so we've configured the server now we have to connect to it and we're going to create an SMTP client 07:22.760 --> 07:27.460 and potentially there are assigned the value of server dot connect. 07:28.280 --> 07:33.070 And this is straight from the example on the GitHub repository for Ghost Simple mail. 07:34.070 --> 07:34.850 We'll check for an error. 07:35.060 --> 07:43.280 If error is not equal to no, we'll just return the return error, otherwise we can start sending our 07:43.280 --> 07:43.780 email. 07:43.910 --> 07:47.630 So to do that, some client requires an email message. 07:47.630 --> 07:49.280 So we'll create what I'll call email. 07:49.610 --> 07:59.060 And that's a sign the value of mailed out new MSG requires no parameters and we'll set some values email 07:59.060 --> 08:05.680 dot set from and I'll make that equal to from which we receive as a parameter to this. 08:05.720 --> 08:07.760 I'll put a dot here and go on the next line. 08:09.760 --> 08:14.130 Add to it sending it to the value of two. 08:14.320 --> 08:22.780 Again, that's a parameter passed to this function dot and then a subject and that will be equal to 08:22.800 --> 08:25.060 subject, which will serve as a parameter, this function. 08:26.260 --> 08:33.880 Now we need to set the body and set the alternative body so the body in our case will be the HTML formatted 08:33.880 --> 08:34.380 message. 08:34.390 --> 08:43.810 So we'll say email set body and that's going to be equal to the type of mail dot text HTML. 08:45.440 --> 08:49.800 Which is a constant built into the mail package and it's are formatted message. 08:52.100 --> 08:54.770 Now we'll do the alternative, which will be plain text. 08:54.770 --> 08:57.340 So email dot set alternative. 09:03.770 --> 09:10.350 It's a type mail text playing again, a constant built into our email package, and it's our plain message. 09:12.530 --> 09:15.350 So at this point, our email is ready to go. 09:16.850 --> 09:26.310 So what we can do is check for an hour at the equal sign and it's emailed us send using the SMTP clamp. 09:27.840 --> 09:35.970 And we check for an error, if error is not equal to no, I'll print it out after error log, print, 09:35.970 --> 09:40.830 line the error and I'll return the error. 09:42.180 --> 09:50.310 Otherwise, I'll write to our info log here so we can see what's happening after info log print line 09:52.800 --> 09:56.070 sent email and we'll return. 09:56.520 --> 09:57.390 I think I already have. 09:57.420 --> 09:57.960 I do. 09:59.040 --> 10:01.170 OK, so this is almost ready to go. 10:01.260 --> 10:08.970 The next step is to go into our package or our templates and put some content in our password reset 10:09.210 --> 10:11.100 HTML and our password reset plan. 10:11.610 --> 10:15.000 Now I do see that I have some errors indicated here up there. 10:15.370 --> 10:16.200 Let's get rid of this one. 10:18.270 --> 10:19.680 It imported it twice for me. 10:19.680 --> 10:23.430 For some reason we want this to be so that should take care of that error. 10:23.430 --> 10:25.470 And if we go down here, it says, here's another one. 10:27.330 --> 10:31.320 It's not settled, it's an alternative that's better. 10:31.650 --> 10:32.040 OK? 10:33.780 --> 10:38.910 Been using too many mail packages and they all have different directives, so this is now error free. 10:39.000 --> 10:42.720 So in the next lecture, we'll set up our templates and we'll try sending a message.