So let's keep working on the reset your password functionality for our application, and so far what
we have is this.
When you go to the login page, you see the forgot password link.
And as you can see, I misspelled that.
So let's go fix that right now.
Back to our I.D. and find out where that word is forgot password.
Let's add an S in there.
OK.
So when someone clicks on that link, they go to the forgot password form.
And they enter their email address and then that calls a back end route that doesn't exist yet and sends
it some Jason.
So let's go work on the back end route.
So back to the ID and let's open up routes dush aiport go and I'm going to put the password reset link
as a non protected route.
So I'll put it right here.
Muxtape up post because we are posting from our JavaScript and we'll make the url api dash slash forgot
password, which I believe is what we called it in the JavaScript and we'll have the call handler that
doesn't exist yet which we'll call send password reset email.
So let's go create that handler or at least the start for that handler.
So in Handler start a dash API.
Let's go to the very bottom and I'll create a new function with the receiver of the application and
it's going to be called Send Password, Reset Email and take the standard two parameters.
A response writer.
And a pointer to a request.
OK, so this is getting some Jason, and we need to read that and that's really easy.
Now we'll just find a variable with local payload.
And it's a structure and it has only one field email, which is a string.
And in JSON we call that email
and now we try to read our JSON so we can just call Appletree Jason and give it the response rate or
the request.
And we're going to read the result into this payload variable, which is defined and we check for an
error.
Error is not equal to nil.
We'll just send back that request and that requires w are coming here and we return because we don't
want to go any further.
But assuming that actually works, we want to send them an email with a link to the password request.
We'll just get started with some dummy data and we'll take care of building a signed link in a lecture
or two.
So we're going to be sending some data to a standard template that will be turned into email and sent
out.
OK, so the variable we're going to use to pass this data to our template will be a simple struct that
has linking it.
And that link will be a string that I can put whatever I want for a link in there.
So I'll just say data link is equal to somewhere else, so I'll make it
a local university.
You can put whatever you want and there will be changing that before too long.
At this point.
I want to send some mail now.
I can do all of that logic right here in the handler, but since I'm probably going to be sending more
than one email in, more than one kind of email from the back end API, which is what we're working
on right now, maybe it makes sense to create a helper function that actually sends email for us.
Now, of course, we can do that using the standard library.
All the functionality we need to send email from the standard library exists.
But there are already several very good packages out there that already solve this problem and make
sending email much, much simpler.
So you'll find on the course resources that this lecture a link to this package and it's called Goda
Simple Dash Mail from Exit and it's on GitHub.
And what I'm going to do is actually use that particular package.
It's very easy to use.
OK, so you can scroll down here and read all about it.
It has all kinds of things.
Here's the part that I want right now.
Let's go get that particular package.
OK, so back to my I'd open a terminal, clear the screen and paste in.
Go get GitHub dotcom slash exit slash go dush simple dash mail slash v2.
And if there's a V three out, by the time you take this course, use the V to just follow along with
the exact version we're using.
And then after the fact, if you want to upgrade to version three, go ahead and do that.
So I go and get it and now it's available to me.
It's in my go mod file and it's been downloaded and I can use it.
So what I'm going to do is create a function to send mail and I'm going to do that specifically for
the back end.
We might be sending email from the front end as well.
For right now, I'm going to assume that the mail that sent from the front end might be different than
the mail that is sent from the back end.
So I'm actually going to duplicate sending email functionality.
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.
OK, and of course that's in package main.
And the first thing I'll do is assume that I'm going to store my templates because we'll be using templates
to send mail.
Right.
In this directory API, in a folder called Templates.
So I'll use go ahead, go colon embed templates folder.
That doesn't exist yet, but it will very shortly.
And then I'll create a variable right underneath that.
And there can't be a space or this won't work there.
And I'll call it email templates, email template FS for file system and some type embed dot FS and
that should have done the import for me and it did.
Now it's warning me that that folder doesn't exist.
So let's create that folder right in the API directory, a new folder called Templates.
OK, and it still telling me there's an error because it's an empty folder, but that's OK.
We'll take care of that shortly.
So now let's declare a function func with a receiver appointed application, and I'm going to call this
simply send mail like that.
And it doesn't need to be exported, but it doesn't hurt to do it either.
So I'm going to leave it with a capital s for now and I'm going to take certain parameters in here.
Now, when you're sending an email, it's from somebody and it's to somebody and it has a subject and
it needs to use some kind of template, which I'll call Tampoe.
All of those are strings.
OK, and also I'm going to be passing some kind of data to this template in our case right now with
password resets, it will be the link the user clicks on to reset his or her password.
So I'm just going to call this data and I don't know what I'm really putting in there for any particular
template.
So I'll just make it an interface like that and we're going to potentially return an error.
And for right now, I'm just going to put a return nil in there.
So this gets rid of an error.
And I know I'm going to be sending some email using templates.
So let's create one.
So create a new file in my templates folder and I'll call it password dash reset.
And I'm going to give it the DOT html and then a template where you can use go HDMI or whatever you
want.
But I'm going to use template and every time I send email, I'm going to send one email that includes
an HTML version, but it also includes a plain text version.
So Password Reset is the name of my second file in the templates folder and I'll call this plain for
plain text dot temple.
OK, we'll populate that with content before too long, but let's not worry about that right now.
So back in Mulago, OK, presumably that error is going to go away once we actually compile this program
for right now, let's ignore it.
So I'm getting from to subject the template and any data I want to pass to this email message I'm going
to send.
So the very first thing I'm going to do is to calculate the name of the file I want to use based upon
the value template that we receive as a parameter.
So I know I'm going to be executing two templates or pursing two templates, one for HTML and one for
plain text.
So the first thing I'll do is build the necessary string and I'm going to create a variable called template
to render, and that's going to be assigned the value of using the format package as print F and the
path to the template.
I want to render for the first one and I'm going to do the HTML template first is going to be templates
slash and then %s for the placeholder dot html dot temple.
OK, so that gives me the path name using the embedded file system to the template I want to render.
Now, let's try parsing that template, so storing the result in the variable T for template and checking
for an error, those come from template dot new and we're going to let the auto import take care of
that for us.
I need you to give this template some kind of name, so I'll call it emailed HTML.
OK, Dot.
And this time I'm going to pass s and the file system I want to use is my email template EFAs.
And now I put in a template to render and we check for an error, if error is not equal to nil.
I'll print it out after error log print line error and I'll return the error.
Now, seemingly a passed that successfully, successfully passed this template, but we still have to
execute it and put some data in there, in our case it will be the link.
So I'll create a variable TPL and it's a type of buffer.
And that's where I'm going to store the template that I execute right here.
If error equals t dot execute template.
And we're putting the results into our Bitstamp buffer and we're going to execute something called body,
which will be in the template we create in a minute, and we're going to put the data in there.
And again, we check for an error if error is not equal to nil.
Then again, we'll just after error log dot, print, line
the error and return here.
And if we get past that, then we can take from that bites dog buffer.
The value of the final formatted message formatted message is a sign the value of T.L. dot string.
So that variable formatted message contains the HTML version of our message.
OK, now we need to do the plain text, which is almost the same logic template to render in this time.
Now it's equal to formatter as print f again, it's in templates.
Again, we're going to change the first part of the file we're looking for, but now we're looking for
plain dot tempo and we substitute template the parameter we get up here.
So now the path to the template we want to work with next.
It ends in plain dot temple.
So again, t an error or equal to template dot new.
And this time I'll call the template emailed plan, which is purely for my purposes from part of the
file system.
And we're going to pass email template EFS, and now we're using template to render, which has a new
value now and we check for an error.
So I'll just copy and paste the stuff.
Right here.
Pasted in there.
And we execute that template, so again, very simple, if error is equal to t execute template.
Again, we're using TPO, we're putting it into TPO, we're still looking for body.
We're still passing in data and we check for the error.
And again, same logic here, so let's just copy that, paste it into here now, at this point, I can
create another variable called play message and that's a sign the value of TPO dot string.
So now I have two variables formatted message, which is the HTML version of the email and plain message,
which is the plain text version of the email.
So now it's time to start using that mail package we just included a little while ago.
But before we do that, if you go back and look at the documentation for this.
We actually have to specify things like the SMTP server, the port that we're going to use, the username,
the password, all of these things have to be set up and there's a fair bit of configuration to take
place before we can actually send an email.
So in the next lecture, we're going to go set up a dummy email account on mail, trapped audio, and
we'll start formatting and sending the email to our end user.
And we're not going to send the actual link right now.
As I said a while ago, we just want to get to the point where we can send an email.