So let's see if we can get our mike or service to send an email with an attachment, and since I want
to test this, I'm actually going to recommend this stuff out here and I mean, create and send invoice
and invoice handlers and uncommented this and Rijad, the colon here, which I should have thought of
last time.
But there you go.
So now we want to create a mail and we want to send email with an attachment, which we can actually
do in one step.
But in order to do that, of course, we need a helper function to send email.
Now, we have a function over here in our API, Maler Dargo, that allows us to send mail.
But of course, we can't call that since it's in the main package of a different application using the
same code base.
We can copy that.
So let's select All in Maila, go from the API folder and create a new file in our invoice folder and
we'll call it Mealer.
Don't go.
Now, we do need to make a change to this.
I'll paste it in there.
The change, of course, is that we need to be able to send attachments and we actually don't have that
functionality yet, but it's easy enough to add the first thing I'll do is I'll go to the top of this
function and I will add a parameter when we call it, and I'll call it attachments, because we might
have more than one at some point and I'll make it a slice of string and that will be the path name to
the attachments.
Now, the second thing we need to do, of course, is we actually don't have a templates folder inside
of this, but we'll get to that in a minute.
So let's go down here to where we actually get ready to send the mail and we'll go right near the bottom
of this function right here after we add our alternative message format.
Let's just check to see if the length of attachments is greater than zero.
If it is, we have some things we need to attach.
So we'll just say for and ignore the first index.
We don't care about that, but we do care about the message.
And I'll just call it X and we'll range through attachments and for every attachment we find will add
it as an attachment to the email message, email dot.
And this is built right into our mailing mailer package and attachment right there.
And we'll just add X, which is the path to the attachment and that's all we have to do for that function.
But back up here, of course, it we're telling it to embed everything it finds inside the templates
folder.
And I'm actually going to change that.
I'll call it email templates just because I can.
And of course, that means we need to create a folder inside of here called New Folder Email Attachments.
And inside of that, we're going to have two files.
One will be called new file invoice, dot, html, dot temple and a new file called invoice playing
dot temple.
Now, the contents of those are pretty simple and they'll actually be pretty similar to what we have
here in our password reset.
So let's copy those as a starting point.
So I've copied password reset HTML the temple and I'll paste that into invoice dot dot temple.
We're not going to have a link, so I'll just delete all of this.
Just make a really simple hello.
Please find your invoice attached and that's enough for that one.
So let's open the password, reset plain and do the same thing.
We'll copy all of this, paste it into invoice plain and delete this stuff and just say,
please find your invoice attached.
So now we have templates and that's a good start.
So let's go back to Maila logo and see what's going on here.
So it doesn't have go embed email templates.
And I called it email attachments.
That's a bit of a problem.
Rename that.
Email templates.
OK, so that problem goes away.
So now we have a means of actually sending attachments.
So let's go back to invoice handlers.
And let's create our e-mail attachments, that's what we're going to do here, so we'll say attachments
is a sign the value of a slice of string.
And in there we just put format as print of the name of our attachment, which is dots, invoices,
percent, the PDF and we substitute ordered.
So now we have our attachments and to send the mail.
Now it's as simple as this error equals Aptos sendmail.
And we're going to send it from, say, info at widgets, dot com,
and the next thing is the two address, which is just ordered on email.
And on the subject, your voice, then the name of the template, and since we called ours invoice dot,
html, dot and invoice planed up tempo, we can just say invoice.
Now we have our attachments and finally, we're not passing any data.
So let's check for an error and I'll just copy this error checking code right here
and paste it into that, and that should actually send the email.
So let's give this a try.
We'll open our terminal, make stop make start invoice.
And we have an error on Handler's line fifty one.
Where is that 51?
When you come try that again, make start invoice.
And it's running, so let's go to our Web browser and here I have two terminal windows or two windows
open or two tabs.
The first one is the one we use last time around, and the second one is mail truck mail.
So let's go to demo inbox and see what's in there.
We have nothing in the inbox, so let's reload this and see what happens.
Template pattern matches no file templates invoice.
OK, so let's try that again.
Let's see if we have it.
So we do have an invoice invoices.
OK, let's go back to our Maila left something out.
I changed the name of the folder here and it's actually emailed templates, which means I need to put
email here and of course, the same thing for the plane and oversight on my part.
So let's mix it up and then we'll make start invoice.
It's running, let's go back to our Web browser this time it should work, reload.
Invoice created and sent to me out here, dotcom, so let's go to mail, Trepp.
And we have an invoice and there's our email and our attachments actually show up up here and there
it is, one hundred PDF.
So now we have a means of actually calling this myCar service before we can do it.
Of course, we need to go back to our code and go back to our invoice handlers.
Get rid of this entirely, because I know it works now uncommented this get rid of this column and go
to the roots.
So invoice roots and again, make this a post.
So the last step is to find the appropriate place on our front end or our back end and actually call
this micro service and we'll take care of that in the next lecture.