WEBVTT 00:00.900 --> 00:05.610 So let's see if we can get our mike or service to send an email with an attachment, and since I want 00:05.610 --> 00:12.570 to test this, I'm actually going to recommend this stuff out here and I mean, create and send invoice 00:12.570 --> 00:21.510 and invoice handlers and uncommented this and Rijad, the colon here, which I should have thought of 00:21.510 --> 00:22.020 last time. 00:22.020 --> 00:22.650 But there you go. 00:23.820 --> 00:28.260 So now we want to create a mail and we want to send email with an attachment, which we can actually 00:28.260 --> 00:29.130 do in one step. 00:29.370 --> 00:33.390 But in order to do that, of course, we need a helper function to send email. 00:33.810 --> 00:39.690 Now, we have a function over here in our API, Maler Dargo, that allows us to send mail. 00:39.720 --> 00:44.130 But of course, we can't call that since it's in the main package of a different application using the 00:44.130 --> 00:44.940 same code base. 00:45.930 --> 00:46.780 We can copy that. 00:46.830 --> 00:56.830 So let's select All in Maila, go from the API folder and create a new file in our invoice folder and 00:56.850 --> 00:57.480 we'll call it Mealer. 00:57.480 --> 00:57.960 Don't go. 00:59.880 --> 01:01.600 Now, we do need to make a change to this. 01:01.620 --> 01:02.550 I'll paste it in there. 01:03.910 --> 01:08.260 The change, of course, is that we need to be able to send attachments and we actually don't have that 01:08.260 --> 01:12.820 functionality yet, but it's easy enough to add the first thing I'll do is I'll go to the top of this 01:12.820 --> 01:20.680 function and I will add a parameter when we call it, and I'll call it attachments, because we might 01:20.680 --> 01:26.470 have more than one at some point and I'll make it a slice of string and that will be the path name to 01:26.470 --> 01:27.220 the attachments. 01:28.140 --> 01:32.460 Now, the second thing we need to do, of course, is we actually don't have a templates folder inside 01:32.460 --> 01:33.720 of this, but we'll get to that in a minute. 01:34.140 --> 01:40.140 So let's go down here to where we actually get ready to send the mail and we'll go right near the bottom 01:40.140 --> 01:46.910 of this function right here after we add our alternative message format. 01:46.950 --> 01:53.000 Let's just check to see if the length of attachments is greater than zero. 01:53.010 --> 01:54.880 If it is, we have some things we need to attach. 01:54.900 --> 01:57.690 So we'll just say for and ignore the first index. 01:57.690 --> 01:59.790 We don't care about that, but we do care about the message. 01:59.790 --> 02:08.010 And I'll just call it X and we'll range through attachments and for every attachment we find will add 02:08.010 --> 02:10.820 it as an attachment to the email message, email dot. 02:10.830 --> 02:16.020 And this is built right into our mailing mailer package and attachment right there. 02:17.730 --> 02:22.530 And we'll just add X, which is the path to the attachment and that's all we have to do for that function. 02:23.160 --> 02:28.410 But back up here, of course, it we're telling it to embed everything it finds inside the templates 02:28.410 --> 02:28.760 folder. 02:28.890 --> 02:30.270 And I'm actually going to change that. 02:30.270 --> 02:33.080 I'll call it email templates just because I can. 02:33.780 --> 02:41.940 And of course, that means we need to create a folder inside of here called New Folder Email Attachments. 02:42.810 --> 02:44.940 And inside of that, we're going to have two files. 02:45.450 --> 02:56.840 One will be called new file invoice, dot, html, dot temple and a new file called invoice playing 02:57.150 --> 02:58.310 dot temple. 02:59.880 --> 03:03.420 Now, the contents of those are pretty simple and they'll actually be pretty similar to what we have 03:03.420 --> 03:04.590 here in our password reset. 03:04.820 --> 03:06.610 So let's copy those as a starting point. 03:08.220 --> 03:16.030 So I've copied password reset HTML the temple and I'll paste that into invoice dot dot temple. 03:16.650 --> 03:19.620 We're not going to have a link, so I'll just delete all of this. 03:19.830 --> 03:21.690 Just make a really simple hello. 03:25.380 --> 03:29.970 Please find your invoice attached and that's enough for that one. 03:30.180 --> 03:33.860 So let's open the password, reset plain and do the same thing. 03:34.560 --> 03:42.750 We'll copy all of this, paste it into invoice plain and delete this stuff and just say, 03:46.260 --> 03:49.650 please find your invoice attached. 03:50.670 --> 03:52.840 So now we have templates and that's a good start. 03:53.880 --> 03:56.520 So let's go back to Maila logo and see what's going on here. 03:57.510 --> 04:00.300 So it doesn't have go embed email templates. 04:00.300 --> 04:02.700 And I called it email attachments. 04:02.820 --> 04:03.630 That's a bit of a problem. 04:03.900 --> 04:04.740 Rename that. 04:08.410 --> 04:09.890 Email templates. 04:10.940 --> 04:13.790 OK, so that problem goes away. 04:13.810 --> 04:16.790 So now we have a means of actually sending attachments. 04:18.010 --> 04:20.140 So let's go back to invoice handlers. 04:21.750 --> 04:28.560 And let's create our e-mail attachments, that's what we're going to do here, so we'll say attachments 04:29.670 --> 04:32.850 is a sign the value of a slice of string. 04:34.680 --> 04:41.550 And in there we just put format as print of the name of our attachment, which is dots, invoices, 04:43.140 --> 04:47.080 percent, the PDF and we substitute ordered. 04:48.600 --> 04:50.850 So now we have our attachments and to send the mail. 04:51.030 --> 04:56.400 Now it's as simple as this error equals Aptos sendmail. 04:58.040 --> 05:01.640 And we're going to send it from, say, info at widgets, dot com, 05:04.910 --> 05:07.820 and the next thing is the two address, which is just ordered on email. 05:10.360 --> 05:19.540 And on the subject, your voice, then the name of the template, and since we called ours invoice dot, 05:19.600 --> 05:26.960 html, dot and invoice planed up tempo, we can just say invoice. 05:28.840 --> 05:33.700 Now we have our attachments and finally, we're not passing any data. 05:35.600 --> 05:39.680 So let's check for an error and I'll just copy this error checking code right here 05:42.380 --> 05:45.620 and paste it into that, and that should actually send the email. 05:47.590 --> 05:48.610 So let's give this a try. 05:48.880 --> 05:54.070 We'll open our terminal, make stop make start invoice. 05:56.030 --> 05:59.630 And we have an error on Handler's line fifty one. 06:00.710 --> 06:02.930 Where is that 51? 06:05.120 --> 06:09.860 When you come try that again, make start invoice. 06:11.820 --> 06:17.940 And it's running, so let's go to our Web browser and here I have two terminal windows or two windows 06:17.940 --> 06:19.030 open or two tabs. 06:19.200 --> 06:22.990 The first one is the one we use last time around, and the second one is mail truck mail. 06:23.310 --> 06:25.480 So let's go to demo inbox and see what's in there. 06:25.980 --> 06:30.050 We have nothing in the inbox, so let's reload this and see what happens. 06:32.620 --> 06:35.620 Template pattern matches no file templates invoice. 06:35.650 --> 06:36.970 OK, so let's try that again. 06:39.250 --> 06:40.150 Let's see if we have it. 06:40.150 --> 06:41.470 So we do have an invoice invoices. 06:45.860 --> 06:49.290 OK, let's go back to our Maila left something out. 06:49.310 --> 06:54.830 I changed the name of the folder here and it's actually emailed templates, which means I need to put 06:54.830 --> 07:02.390 email here and of course, the same thing for the plane and oversight on my part. 07:03.350 --> 07:08.780 So let's mix it up and then we'll make start invoice. 07:12.020 --> 07:16.880 It's running, let's go back to our Web browser this time it should work, reload. 07:19.280 --> 07:23.510 Invoice created and sent to me out here, dotcom, so let's go to mail, Trepp. 07:24.370 --> 07:31.720 And we have an invoice and there's our email and our attachments actually show up up here and there 07:31.720 --> 07:34.080 it is, one hundred PDF. 07:35.380 --> 07:39.190 So now we have a means of actually calling this myCar service before we can do it. 07:39.200 --> 07:44.020 Of course, we need to go back to our code and go back to our invoice handlers. 07:45.920 --> 07:56.810 Get rid of this entirely, because I know it works now uncommented this get rid of this column and go 07:56.810 --> 07:57.560 to the roots. 07:57.800 --> 08:01.850 So invoice roots and again, make this a post. 08:03.530 --> 08:09.650 So the last step is to find the appropriate place on our front end or our back end and actually call 08:09.650 --> 08:12.740 this micro service and we'll take care of that in the next lecture.