So this time around, I want to explore how we send emails using Go and Go does have some functionality
built into the standard library that allows us to send email, but it's not ideal.
Nevertheless, you should be aware of it.
So what I'm going to do is right here in my mango file, I'm going to hard code right in this part right
here.
Just after we deferred the closing of our database connection pool, I'm going to write code so that
when the program starts, it sends a test email.
So how do we do that?
Well, it's not that hard.
First of all, you need a from address.
Who's the mail from?
So I'll just create a variable called from and it's going to be assigned the value of me here dot com.
So now I have a firm address.
Now, according to the functionality in the standard library, we must have some means of authenticating
with the mail server.
We have to give our credentials, if any, to email server that will allow us to send messages.
So I have installed and you don't yet, but don't worry about it.
I have installed a test mail server called Mail Hug, unfortunately named but extremely helpful when
you're developing and sending text messages.
And I'm going to create my authentication against that.
So you don't need to do this.
You don't need to do any of this at all because we're not going to be using this this mail functionality.
But I'm just showing you that it exists.
Orcadian authentication called off and it's using a package built into the standard library called SMTP,
which stands for simple mail transfer protocol.
And I'm going to use the plane off and it requires some arguments.
First of all, it requires the identity.
Well, I'm using a test mail server.
There is no identity.
Then it requires a username.
And I'll just use my from email because it just just has to be in the form of an email address.
Then it requires a password for the mail server, if any.
I'll leave that empty because I'm using a test mail server.
Then it requires a server name and I'm going to use localhost, which you will be using once you install
the test mail server.
But don't worry about it for right now.
And that that's all that's required for arguments for the off package.
Now, I will actually create and send an email message in one step, so I'm going to check for an error.
And then I call SMTP dot sendmail and it requires an address.
And in this case, the addresses of four hour server and the server is localhost and I'm listening on
Porten twenty five and you will be as soon as you install the necessary package then it requires the
auth that we just to find off then it requires the from address which we have from then it requires
the recipient's address and for that we can use a slice of strings.
And I'm just building that in one step and I'll send it to you at their dot com.
As long as it's formatted as an email message that will work for our purposes right now.
Finally, we need the actual content of the message itself, which is a slice of bytes.
So in that slice of bytes, I will just put Hello World.
So that sends the message and it's not going to be very pretty and it's not going to have a subject
or anything like that.
But I'm just demonstrating to you that there is functionality in the standard library for sending messages
so we check for error.
If error is not equal to nil, just log the error.
OK, let's run this.
So I run the application, don't run it starts up and hopefully it actually sent the message.
Let's find out.
So I would go to my test mail server, which is on localhost 80, 25.
And there's a message badly formatted, that's what it used for the actual subject, but it doesn't
matter.
We're not going to be using this package.
I just want to show you that it exists.
And there is a message that I sent in plain text, not nicely formatted HTML.
As you can see, there are some limitations in the standard libraries, mail functionality.
So what we're going to do instead is in the next lecture or two will be installing a sample or a good
package for sending and sending emails using go.
And then we'll go through the process of allowing ourselves to send an email from anywhere in our application
at any time to anyone we want.
And that will take a little while.
But we'll do that in the next few lectures.