So now that we have an account on male trapped IO, it's time to keep working on sending email from
the back end.
And before we go too much further, just a couple of things I needed to clean up.
No one here in Mulago online 14, I left out the second argument for formatter as print if I added it
right here.
So this part needs to be added.
And I'm sure you noticed that.
OK.
And also on the password reset page itself, forgot password, the one that allows us to initiate the
process.
This said log in and I've changed that back to send password reset link.
So little sloppy on my part and certainly not critical for this part.
But the first one is we wouldn't have been able to render a template for email.
So let's keep going.
So back in our Mulago, now that we have our credentials, we actually need to start sending email.
So I added this line right here just so I could compile and run it and make sure that everything works
because we weren't using formatted message or plain message.
And as you know, God doesn't like unused variables inside functions, but that's just temporary and
we'll be getting rid of that before too long.
So now we actually want to send the mail, send the message, OK, in order to do that, of course,
if we go back to mail trapped oh, right here, we need to use these credentials.
And I don't want to hard code these into my application.
Instead, I want them to be a command line parameters or environment variables or something like that.
So I can actually work in development and not have to change the actual source code before I go live.
So to make that happen.
Let's go back to our Idy and let's open up Apptio, which is the main entry point for our application.
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,
not surprisingly, as A.P. And it's going to be a struct get rid of that space.
And the members for that struck will consist of the host, which is a string, and the port, which
is an end, and the user name for the service for our e-mail server, which is a string, and the password,
which is also a string.
And this is sufficient for our purposes right now.
So let's format this.
OK, now down here, we can actually read that information and I'll just read it from the command line
is command line parameters.
So right here I'll just duplicate this line because the first one I want is a string.
I'm not going to read it into CFG DB DSN.
I'm going to read it into CFT or CFG SMTP post and I'll call the command line parameter, the SMTP host
and the value
we got right from our mail page.
And that was a TPG mail trap.
OK.
And we call this a S.P. host for the Help text, and the next thing I want is a port.
So I have here flag infa, so a copy and paste pasted here.
And so, again, I'm not reading into config port and reading into CFG as in TP Port, and we'll call
the command line Fleg.
The S.P. port will make a default to one of the values available to us five eighty seven.
That's one of the ones that I got from the mail truck configuration information.
And this will be the.
To people.
OK, and we also want the username and password.
So those are both strings, so I'll just duplicate that line twice and the first one will be user name
and we'll call it a SMTP username or just some TV user.
Make a short.
And mine are copied already, so I will paste that in here.
And again, this one won't work, so don't try using this, because I'll have reset my credentials before
this course goes live.
So SMTP user and the SMTP password.
It was SMTP password, and we'll call this a SMTP pass and I'll just paste in the one that I copied
from my credential, SPREP.
There is so this is a S.P. password, so now I have all of these things available to me, the next time
I start and stop and start my application so I can go back to Maila, don't go and I can start configuring
this.
So let me give yourselves a little bit of room here.
The first thing I want to do.
Is to create a server and some type client, so I'll call that server and it's mailed out and the mail
comes from the mail package that we're going to import here.
And hopefully it will import it for me.
New s.M TPE client right there.
And that requires no parameters, so that creates a server variable for me when I start populating its
various fields.
So server host.
Do that important for me.
Yep.
Server host equals APTA, config SMTP, dot host server dot port is equal to abdon config SMPTE dot
port.
Server username.
Is equal to abduct, configure, SMTP, dot username
server, dot password.
Is equal to after configures SMTP password.
And we need a few more things as well, so I'm using male trapped oil, so I will specify RTLS encryption
server encryption.
Is equal to male dot encryption code, again, encryption should tell us.
And I'm going to keep alive defaults because I don't want to keep the connection to the SMTP server
open server to keep alive
Zippel false and I'll put a couple of timeouts server that connect timeout is equal to ten seconds.
So 10 times time dot second and server dot send the time out is equal to the same thing 10 times time
dot second.
OK, so we've configured the server now we have to connect to it and we're going to create an SMTP client
and potentially there are assigned the value of server dot connect.
And this is straight from the example on the GitHub repository for Ghost Simple mail.
We'll check for an error.
If error is not equal to no, we'll just return the return error, otherwise we can start sending our
email.
So to do that, some client requires an email message.
So we'll create what I'll call email.
And that's a sign the value of mailed out new MSG requires no parameters and we'll set some values email
dot set from and I'll make that equal to from which we receive as a parameter to this.
I'll put a dot here and go on the next line.
Add to it sending it to the value of two.
Again, that's a parameter passed to this function dot and then a subject and that will be equal to
subject, which will serve as a parameter, this function.
Now we need to set the body and set the alternative body so the body in our case will be the HTML formatted
message.
So we'll say email set body and that's going to be equal to the type of mail dot text HTML.
Which is a constant built into the mail package and it's are formatted message.
Now we'll do the alternative, which will be plain text.
So email dot set alternative.
It's a type mail text playing again, a constant built into our email package, and it's our plain message.
So at this point, our email is ready to go.
So what we can do is check for an hour at the equal sign and it's emailed us send using the SMTP clamp.
And we check for an error, if error is not equal to no, I'll print it out after error log, print,
line the error and I'll return the error.
Otherwise, I'll write to our info log here so we can see what's happening after info log print line
sent email and we'll return.
I think I already have.
I do.
OK, so this is almost ready to go.
The next step is to go into our package or our templates and put some content in our password reset
HTML and our password reset plan.
Now I do see that I have some errors indicated here up there.
Let's get rid of this one.
It imported it twice for me.
For some reason we want this to be so that should take care of that error.
And if we go down here, it says, here's another one.
It's not settled, it's an alternative that's better.
OK?
Been using too many mail packages and they all have different directives, so this is now error free.
So in the next lecture, we'll set up our templates and we'll try sending a message.