1
00:00:00,630 --> 00:00:00,900
Good.

2
00:00:01,350 --> 00:00:05,790
I have a few windows open up here, so I have Atom here on the left.

3
00:00:05,820 --> 00:00:07,650
I have my Gmail address.

4
00:00:07,710 --> 00:00:08,970
My Gmail account here,

5
00:00:09,010 --> 00:00:09,450
up here.

6
00:00:10,050 --> 00:00:12,040
And I have my Hotmail account.

7
00:00:12,360 --> 00:00:13,470
And also my command line.

8
00:00:14,060 --> 00:00:17,600
So I'll be using Gmail account as an email address from where 

9
00:00:17,640 --> 00:00:23,360
I'll be sending the e-mails to the users who are sending these data to my Web site.

10
00:00:24,240 --> 00:00:27,300
And then I will use my Hotmail address to test my application.

11
00:00:27,750 --> 00:00:32,940
So I'll be filling that form using my email address of my Hotmail account.

12
00:00:33,840 --> 00:00:34,470
And yeah.

13
00:00:34,830 --> 00:00:35,280
That's it.

14
00:00:36,450 --> 00:00:38,610
So where are we know, at what point are we?

15
00:00:39,150 --> 00:00:45,240
Well, we are at the point where we have access to the email and the height values that the user is submitting

16
00:00:45,320 --> 00:00:47,430
in that Web application form.

17
00:00:48,360 --> 00:00:51,040
And currently we're just printing them out.

18
00:00:51,330 --> 00:00:59,520
So what we need to do is instead of printing them out, we need to apply something like send email and

19
00:00:59,520 --> 00:01:02,510
then we pass this arguments to this, send email.

20
00:01:02,880 --> 00:01:07,500
So send email is the name of the function that I'll be creating in just a minute.

21
00:01:08,700 --> 00:01:08,880
Yes.

22
00:01:08,910 --> 00:01:17,310
So I intend to create a function that will get as input, an email address and a value, and then

23
00:01:17,480 --> 00:01:23,580
I'll construct some message, so an email incorporating this height in that email string.

24
00:01:24,310 --> 00:01:30,750
Then I'll be sending that email to this email address for my Gmail account.

25
00:01:32,100 --> 00:01:37,170
We can go ahead and write that e-mail function just down here.

26
00:01:37,920 --> 00:01:44,340
Or alternatively, which is what I'm going to do and I recommend you do that to to keep your code organized.

27
00:01:44,910 --> 00:01:50,880
So what I'm going to do is in the same level with my app.py script.

28
00:01:52,020 --> 00:02:01,130
So here, which is here, I'm going to create a new file and call that send_email.py. Good.

29
00:02:02,400 --> 00:02:04,800
And here I'm going to create a function.

30
00:02:05,100 --> 00:02:07,700
So let's call the function send email as well.

31
00:02:08,220 --> 00:02:09,980
You can call it whatever you want.

32
00:02:10,290 --> 00:02:11,070
So it doesn't matter.

33
00:02:11,550 --> 00:02:14,490
In my case, it just happened to have the same name with my script.

34
00:02:15,120 --> 00:02:18,270
And then I go here to the app folder.

35
00:02:19,200 --> 00:02:27,710
And what I do is say from send email_import send_email.

36
00:02:28,110 --> 00:02:32,580
So I'm importing from the send_email module, which is the script,

37
00:02:33,390 --> 00:02:35,020
I'm importing the send_email function.

38
00:02:35,580 --> 00:02:38,520
And then here I'm here.

39
00:02:38,910 --> 00:02:41,420
I'm using the imported function.

40
00:02:41,790 --> 00:02:44,610
So I'm calling the function that I imported in here.

41
00:02:45,990 --> 00:02:46,770
That is clear.

42
00:02:47,250 --> 00:02:50,230
I believe. One very important tip, though.

43
00:02:50,310 --> 00:02:54,110
Don't name this module email.

44
00:02:54,810 --> 00:02:57,270
So if you name it, email.py.

45
00:02:57,810 --> 00:03:02,190
And when you import you, you say like from email import

46
00:03:02,360 --> 00:03:03,000
my function.

47
00:03:04,170 --> 00:03:05,760
That would cause you problems.

48
00:03:05,790 --> 00:03:10,670
Because there's also a module built in module in Python called e-mail.

49
00:03:11,010 --> 00:03:11,770
And you know that.

50
00:03:12,420 --> 00:03:20,490
And so when you do from email import something, you are reserving the email namespace in Python for

51
00:03:20,490 --> 00:03:22,860
your custom module.

52
00:03:23,340 --> 00:03:30,570
That means when if you import email later, so if you import email, you get some problems because

53
00:03:30,810 --> 00:03:33,190
these namespace is already occupied.

54
00:03:33,690 --> 00:03:37,290
So don't use email as the name of your script.

55
00:03:37,860 --> 00:03:41,630
And also in general, don't use module names for your scripts.

56
00:03:42,780 --> 00:03:43,540
Hope that is clear.

57
00:03:43,560 --> 00:03:45,070
And now we go here.

58
00:03:45,180 --> 00:03:51,530
And before even creating that function, we need to import e-mail module.

59
00:03:51,900 --> 00:03:55,590
And I assume you have taken the previous lectures.

60
00:03:55,700 --> 00:04:00,750
Of course, where I taught you how to send e-mails from an e-mail address to another e-mail address

61
00:04:01,560 --> 00:04:02,250
via Python.

62
00:04:02,460 --> 00:04:08,170
So I'm not going to explain what the code does because I assume you have taken those lectures.

63
00:04:11,310 --> 00:04:13,080
And I'll try to be quick here.

64
00:04:13,110 --> 00:04:15,720
So define send email.

65
00:04:15,780 --> 00:04:18,420
We need to get the email address.

66
00:04:19,980 --> 00:04:22,980
So from this function call here.

67
00:04:23,250 --> 00:04:27,350
So this email address will be sent to these parameter here.

68
00:04:27,960 --> 00:04:29,870
And also, we need the height.

69
00:04:29,940 --> 00:04:34,210
And later on, we'll add some more here, like average height, some parameters.

70
00:04:34,620 --> 00:04:39,480
When we add some lines down here that calculate that average.

71
00:04:40,080 --> 00:04:42,030
But for now, let's keep things simple. Good.

72
00:04:44,340 --> 00:04:46,350
From email.

73
00:04:51,220 --> 00:05:04,360
@gmail.com, from_password "pythonlecture", I just created a temporary password just for this

74
00:05:04,360 --> 00:05:04,900
recording.

75
00:05:06,130 --> 00:05:09,710
From email, from password to email.

76
00:05:10,910 --> 00:05:11,230
Email.

77
00:05:11,570 --> 00:05:18,850
So that is this parameter, which means the user is entering

78
00:05:18,910 --> 00:05:23,510
the email address and the email address is being passed to the function call.

79
00:05:24,430 --> 00:05:26,980
That value will be passed in here.

80
00:05:27,010 --> 00:05:33,550
And that will be passed in here, and then we can use these to_email to install our smtlib library

81
00:05:34,150 --> 00:05:35,770
to send an email to this email address.

82
00:05:36,580 --> 00:05:38,950
Then we have subject.

83
00:05:41,370 --> 00:05:42,370
Let's say "Height data".

84
00:05:42,550 --> 00:05:45,190
And the actual message.

85
00:05:47,010 --> 00:05:53,140
Let's say: "Hey there, your height is", let's use you string formatting there.

86
00:05:53,530 --> 00:05:58,750
So the %s we'll be replaced by the height which we are getting from here.

87
00:05:59,320 --> 00:06:00,010
Good.

88
00:06:00,460 --> 00:06:02,920
This will be sent as plain text.

89
00:06:03,340 --> 00:06:07,310
But if you want to make this HTML enabled

90
00:06:08,010 --> 00:06:10,720
we'd want to use mime text.

91
00:06:11,440 --> 00:06:16,210
And actually, we need to import that, I imported email earlier.

92
00:06:16,240 --> 00:06:19,760
But, um, sorry if I confused you, but we don't need email.

93
00:06:19,870 --> 00:06:27,400
We need mime.text, import MIME.text.

94
00:06:27,940 --> 00:06:38,260
Actually from thos, from this we import MIME.text than what we do is we create another object there

95
00:06:38,830 --> 00:06:49,860
that will be equal to MIMEText message, variable there and HTML, which means that this line

96
00:06:49,870 --> 00:06:51,410
here will be read as HTML.

97
00:06:52,000 --> 00:06:57,090
And that means we can now add HTML email text there.

98
00:06:57,670 --> 00:06:59,440
So if we add this, for instance,

99
00:07:03,190 --> 00:07:09,870
the height value, will be in bold because we are wrapping the height value with these strong tags.

100
00:07:10,750 --> 00:07:10,960
Good.

101
00:07:12,430 --> 00:07:17,600
And out of this message of the object, we access these keys.

102
00:07:17,800 --> 00:07:21,850
So subject equals to subject.

103
00:07:22,850 --> 00:07:23,160
Mm.

104
00:07:24,530 --> 00:07:24,700
Oh.

105
00:07:24,700 --> 00:07:39,280
Also you have the ['To'] key which is equal to_email and then from_email.

106
00:07:39,810 --> 00:07:40,090
Good.

107
00:07:40,810 --> 00:07:48,700
And lastly, I need to log into my Gmail account, so let's create a Gmail variable there that would

108
00:07:48,700 --> 00:07:55,580
be equal to smtplib.SMTP() method.

109
00:07:56,440 --> 00:07:57,850
And then guess message.

110
00:08:00,740 --> 00:08:07,970
That we will get this Gmail smtp server address and one of these ports, maybe this

111
00:08:07,970 --> 00:08:08,150
one.

112
00:08:09,950 --> 00:08:15,520
So smtp.gmail.com and 587.

113
00:08:15,940 --> 00:08:20,050
So there's a port and then we apply the age ehlo() method.

114
00:08:21,670 --> 00:08:26,320
Then again, start.tls().

115
00:08:28,650 --> 00:08:37,970
And lastly, we're ready to log in, login from_email, from_password, password yeah.

116
00:08:39,530 --> 00:08:43,080
So the credential of our email address that we want to log into.

117
00:08:43,510 --> 00:08:47,190
And lastly, we are ready to send the actual message.

118
00:08:47,450 --> 00:08:49,200
So the email object and to do that.

119
00:08:49,260 --> 00:08:54,060
We need to apply the send.message() method to the Gmail object.

120
00:08:54,390 --> 00:08:58,800
And also, you need to pass the message the object that you want to send there, message.

121
00:08:58,890 --> 00:09:08,220
And one last thing you need to do is you want to make sure that in your Gmail account.

122
00:09:09,790 --> 00:09:18,680
And in my account, if you go to connected apps and sites and you go down here, you want to allow

123
00:09:18,690 --> 00:09:21,550
less secure apps to log in to your email address.

124
00:09:21,880 --> 00:09:27,400
Unfortunately, Gmail considers smptlib a less secure app.

125
00:09:28,180 --> 00:09:33,930
So you want to turn that on like I have it to you to be able to log in via assumptive pure lip.

126
00:09:35,480 --> 00:09:36,050
Good.

127
00:09:36,790 --> 00:09:48,890
And I have to import smptlib there so that I have access to its methods and  I'll save this and

128
00:09:48,910 --> 00:09:51,750
try to run it to see what we're going to get.

129
00:09:56,290 --> 00:10:04,350
So localhost5000 with my Hotmail address.

130
00:10:04,540 --> 00:10:05,080
Input a height there.

131
00:10:08,870 --> 00:10:15,310
And yeah, that looks to have gone through, but I need to check my Hotmail.

132
00:10:16,690 --> 00:10:18,520
Yeah, I have got a message here.

133
00:10:19,480 --> 00:10:20,050
Hey there.

134
00:10:20,050 --> 00:10:22,990
Your height is 176.

135
00:10:23,710 --> 00:10:25,440
So that is working perfectly.

136
00:10:26,590 --> 00:10:34,930
And, yeah, if there is anything that is not working on your side, then feel free to drop a question

137
00:10:34,960 --> 00:10:38,680
there and I'll try to explain you what's the problem.

138
00:10:39,070 --> 00:10:41,390
But make sure to include any error that you're getting.

139
00:10:42,060 --> 00:10:45,980
So we have one more lecture to go, and in that lecture

140
00:10:45,980 --> 00:10:52,270
we will calculate the average of all the heights that the users are sending to our database.

141
00:10:52,360 --> 00:10:56,620
We will be sending that average to the user via email.

142
00:10:57,580 --> 00:10:57,850
Great.

143
00:10:58,610 --> 00:10:59,260
Well, let's move on.

