1
00:00:00,660 --> 00:00:08,100
So this time around, I want to explore how we send emails using Go and Go does have some functionality

2
00:00:08,100 --> 00:00:13,140
built into the standard library that allows us to send email, but it's not ideal.

3
00:00:13,170 --> 00:00:14,670
Nevertheless, you should be aware of it.

4
00:00:14,700 --> 00:00:21,150
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

5
00:00:21,150 --> 00:00:21,420
here.

6
00:00:21,420 --> 00:00:26,040
Just after we deferred the closing of our database connection pool, I'm going to write code so that

7
00:00:26,040 --> 00:00:29,230
when the program starts, it sends a test email.

8
00:00:29,610 --> 00:00:31,260
So how do we do that?

9
00:00:31,290 --> 00:00:32,170
Well, it's not that hard.

10
00:00:32,190 --> 00:00:34,020
First of all, you need a from address.

11
00:00:34,020 --> 00:00:34,970
Who's the mail from?

12
00:00:34,980 --> 00:00:40,360
So I'll just create a variable called from and it's going to be assigned the value of me here dot com.

13
00:00:40,610 --> 00:00:41,870
So now I have a firm address.

14
00:00:42,450 --> 00:00:48,840
Now, according to the functionality in the standard library, we must have some means of authenticating

15
00:00:48,840 --> 00:00:49,610
with the mail server.

16
00:00:49,620 --> 00:00:55,460
We have to give our credentials, if any, to email server that will allow us to send messages.

17
00:00:56,100 --> 00:00:59,700
So I have installed and you don't yet, but don't worry about it.

18
00:00:59,700 --> 00:01:05,340
I have installed a test mail server called Mail Hug, unfortunately named but extremely helpful when

19
00:01:05,340 --> 00:01:07,440
you're developing and sending text messages.

20
00:01:07,860 --> 00:01:10,080
And I'm going to create my authentication against that.

21
00:01:10,080 --> 00:01:12,150
So you don't need to do this.

22
00:01:12,150 --> 00:01:17,310
You don't need to do any of this at all because we're not going to be using this this mail functionality.

23
00:01:17,310 --> 00:01:18,900
But I'm just showing you that it exists.

24
00:01:18,970 --> 00:01:25,710
Orcadian authentication called off and it's using a package built into the standard library called SMTP,

25
00:01:25,740 --> 00:01:28,470
which stands for simple mail transfer protocol.

26
00:01:28,800 --> 00:01:32,550
And I'm going to use the plane off and it requires some arguments.

27
00:01:32,970 --> 00:01:35,520
First of all, it requires the identity.

28
00:01:35,520 --> 00:01:37,400
Well, I'm using a test mail server.

29
00:01:37,410 --> 00:01:38,310
There is no identity.

30
00:01:38,700 --> 00:01:40,290
Then it requires a username.

31
00:01:40,290 --> 00:01:44,220
And I'll just use my from email because it just just has to be in the form of an email address.

32
00:01:44,610 --> 00:01:48,370
Then it requires a password for the mail server, if any.

33
00:01:48,420 --> 00:01:50,820
I'll leave that empty because I'm using a test mail server.

34
00:01:51,150 --> 00:01:56,220
Then it requires a server name and I'm going to use localhost, which you will be using once you install

35
00:01:56,220 --> 00:01:57,290
the test mail server.

36
00:01:57,510 --> 00:01:58,950
But don't worry about it for right now.

37
00:01:59,520 --> 00:02:03,470
And that that's all that's required for arguments for the off package.

38
00:02:04,170 --> 00:02:10,710
Now, I will actually create and send an email message in one step, so I'm going to check for an error.

39
00:02:11,610 --> 00:02:18,450
And then I call SMTP dot sendmail and it requires an address.

40
00:02:18,660 --> 00:02:24,690
And in this case, the addresses of four hour server and the server is localhost and I'm listening on

41
00:02:24,690 --> 00:02:30,150
Porten twenty five and you will be as soon as you install the necessary package then it requires the

42
00:02:30,150 --> 00:02:38,160
auth that we just to find off then it requires the from address which we have from then it requires

43
00:02:38,160 --> 00:02:42,180
the recipient's address and for that we can use a slice of strings.

44
00:02:43,140 --> 00:02:48,150
And I'm just building that in one step and I'll send it to you at their dot com.

45
00:02:48,660 --> 00:02:52,910
As long as it's formatted as an email message that will work for our purposes right now.

46
00:02:54,150 --> 00:02:59,400
Finally, we need the actual content of the message itself, which is a slice of bytes.

47
00:03:00,330 --> 00:03:04,230
So in that slice of bytes, I will just put Hello World.

48
00:03:05,580 --> 00:03:09,960
So that sends the message and it's not going to be very pretty and it's not going to have a subject

49
00:03:09,960 --> 00:03:10,680
or anything like that.

50
00:03:10,680 --> 00:03:15,660
But I'm just demonstrating to you that there is functionality in the standard library for sending messages

51
00:03:16,260 --> 00:03:17,460
so we check for error.

52
00:03:17,640 --> 00:03:21,180
If error is not equal to nil, just log the error.

53
00:03:23,580 --> 00:03:25,440
OK, let's run this.

54
00:03:26,010 --> 00:03:34,540
So I run the application, don't run it starts up and hopefully it actually sent the message.

55
00:03:34,560 --> 00:03:35,230
Let's find out.

56
00:03:35,580 --> 00:03:42,330
So I would go to my test mail server, which is on localhost 80, 25.

57
00:03:44,580 --> 00:03:49,500
And there's a message badly formatted, that's what it used for the actual subject, but it doesn't

58
00:03:49,500 --> 00:03:49,820
matter.

59
00:03:49,830 --> 00:03:51,120
We're not going to be using this package.

60
00:03:51,130 --> 00:03:52,680
I just want to show you that it exists.

61
00:03:53,070 --> 00:03:57,660
And there is a message that I sent in plain text, not nicely formatted HTML.

62
00:03:57,990 --> 00:04:02,890
As you can see, there are some limitations in the standard libraries, mail functionality.

63
00:04:03,180 --> 00:04:10,140
So what we're going to do instead is in the next lecture or two will be installing a sample or a good

64
00:04:10,320 --> 00:04:13,980
package for sending and sending emails using go.

65
00:04:14,610 --> 00:04:19,830
And then we'll go through the process of allowing ourselves to send an email from anywhere in our application

66
00:04:20,040 --> 00:04:22,470
at any time to anyone we want.

67
00:04:22,480 --> 00:04:23,550
And that will take a little while.

68
00:04:23,550 --> 00:04:25,320
But we'll do that in the next few lectures.
