WEBVTT

0
00:00.690 --> 00:01.800
The first thing we have

1
00:01.800 --> 00:06.390
to figure out is how exactly does email work?

2
00:06.390 --> 00:08.340
We all know that we can log onto Gmail

3
00:08.340 --> 00:11.970
or yahoo.com and we can start filling out an email.

4
00:11.970 --> 00:15.240
It's pretty much second nature to everybody by now.

5
00:15.240 --> 00:18.240
But what actually happens behind the scenes?

6
00:18.240 --> 00:20.430
Well, let's say that we have a sender,

7
00:20.430 --> 00:25.430
angela@gmail.com and a recipient, timmy@yahoo.com.

8
00:25.650 --> 00:28.980
Now, in order to send this email from my Gmail account

9
00:28.980 --> 00:33.300
to another email account, what happens behind the scene is

10
00:33.300 --> 00:35.790
that there's a Gmail mail server,

11
00:35.790 --> 00:37.980
which will receive my message.

12
00:37.980 --> 00:40.380
And then there's a Yahoo Mail server,

13
00:40.380 --> 00:43.770
which will store the message until Timmy logs

14
00:43.770 --> 00:47.670
onto his computer and logs onto yahoo.com,

15
00:47.670 --> 00:50.823
which downloads the email from the Yahoo Mail server.

16
00:51.690 --> 00:56.329
So this email is going to move between all of these steps.

17
00:56.329 --> 00:58.080
And in order to do this,

18
00:58.080 --> 01:00.990
it relies on something called SMTP,

19
01:00.990 --> 01:03.726
the Simple Mail Transfer Protocol.

20
01:03.726 --> 01:07.530
And this contains all of the rules that determine

21
01:07.530 --> 01:11.160
how an email is received by mail servers passed

22
01:11.160 --> 01:12.930
onto the next mail server

23
01:12.930 --> 01:15.993
and how email can be sent around the Internet.

24
01:17.340 --> 01:19.683
Now, a good analogy for SMTP is

25
01:19.683 --> 01:24.240
if you imagine these mail servers as a post office

26
01:24.240 --> 01:27.210
and Timmy's computer being the mailbox.

27
01:27.210 --> 01:32.210
Then SMTP is basically the postman who knows how to handle

28
01:32.220 --> 01:36.120
the email and take it to various post offices

29
01:36.120 --> 01:39.510
and eventually put it into Timmy's computer.

30
01:39.510 --> 01:42.990
So in Python there's a module called smtplib,

31
01:42.990 --> 01:47.460
which allows us to use SMTP to send our email

32
01:47.460 --> 01:50.550
to any address on the Internet.

33
01:50.550 --> 01:54.360
To start, I recommend setting up two fresh email accounts.

34
01:54.360 --> 01:57.060
Create a new email account with Gmail

35
01:57.060 --> 02:00.360
and also create a new email account with Yahoo.

36
02:00.360 --> 02:03.000
These new email accounts will be perfect

37
02:03.000 --> 02:04.260
for testing your code

38
02:04.260 --> 02:06.930
and following along with the video tutorials.

39
02:06.930 --> 02:09.000
Plus, we'll be making those email accounts

40
02:09.000 --> 02:11.490
a little bit less secure to test our code.

41
02:11.490 --> 02:12.900
So that's another reason to set up

42
02:12.900 --> 02:15.750
some testing email addresses for now.

43
02:15.750 --> 02:18.150
After you've set up your new email accounts,

44
02:18.150 --> 02:21.150
head over to the Course Resources and download the zip file

45
02:21.150 --> 02:23.300
with the starting code for today's lessons.

46
02:24.270 --> 02:27.390
And then we're going to open that up using PyCharm.

47
02:27.390 --> 02:30.582
Now I want you to take a look inside the starting project.

48
02:30.582 --> 02:33.150
There is a main.py file

49
02:33.150 --> 02:36.180
and there's also a quotes.txt file.

50
02:36.180 --> 02:37.710
Don't worry about this file for now,

51
02:37.710 --> 02:39.060
we're going to come back to it

52
02:39.060 --> 02:41.640
when we explore the datetime module.

53
02:41.640 --> 02:44.850
For now, we're going to be working within the main.py.

54
02:44.850 --> 02:49.481
And I want to show you how you can use this smtplib library

55
02:49.481 --> 02:54.377
to start sending emails straight from your Python code.

56
02:54.377 --> 02:58.290
As always, we import the module smtplib

57
02:58.290 --> 03:00.930
and then we can start using it.

58
03:00.930 --> 03:05.095
Once we've imported this SMTP library essentially,

59
03:05.095 --> 03:09.330
we can use it to create a new SMTP object.

60
03:09.330 --> 03:12.600
So we're going to call that object a new connection

61
03:12.600 --> 03:15.000
because it's basically a way for us to be able

62
03:15.000 --> 03:20.000
to connect to our email provider's SMTP email server.

63
03:20.010 --> 03:23.130
We're going to do this by tapping into the smtplib

64
03:23.130 --> 03:27.810
and then creating an object from the SMTP class.

65
03:27.810 --> 03:29.670
Now, when we create this object,

66
03:29.670 --> 03:33.759
one of the things that we should specify is the location

67
03:33.759 --> 03:37.648
of our email providers SMTP server.

68
03:37.648 --> 03:42.648
Now for Gmail, it's simply smtp.gmail.com,

69
03:43.140 --> 03:45.870
but it's different for every email provider.

70
03:45.870 --> 03:50.310
So that means if your email ends in @gmail.com,

71
03:50.310 --> 03:52.620
then this would be how you would connect

72
03:52.620 --> 03:54.450
to your email server.

73
03:54.450 --> 03:56.010
In our case, I've created

74
03:56.010 --> 04:01.010
a testing email called appbreweryinfo@gmail.com.

75
04:02.160 --> 04:06.000
And this part that's before the @ sign is

76
04:06.000 --> 04:09.390
the identity of my email account.

77
04:09.390 --> 04:11.850
And the part after the @ sign is

78
04:11.850 --> 04:14.910
the identity of my email provider.

79
04:14.910 --> 04:19.659
So in my case, I need to connect to smtp.gmail.com.

80
04:19.659 --> 04:23.160
But if you have a different email provider,

81
04:23.160 --> 04:27.240
for example if you're with Hotmail, it's smtp.live.com,

82
04:27.240 --> 04:32.190
and if you are with Yahoo, it's smtp.mail.yahoo.com.

83
04:32.190 --> 04:35.190
And if you're with a completely different email provider

84
04:35.190 --> 04:38.670
than simply just Google your email provider

85
04:38.670 --> 04:40.590
and the SMTP information,

86
04:40.590 --> 04:42.660
and you should find an article somewhere

87
04:42.660 --> 04:46.162
that describes a URL that looks something like this.

88
04:46.162 --> 04:48.780
Once I've created my connection,

89
04:48.780 --> 04:52.110
the next thing I need to do is to go ahead

90
04:52.110 --> 04:55.080
and call starttls().

91
04:55.080 --> 04:59.100
Now, TLS stands for Transport Layer Security,

92
04:59.100 --> 05:00.815
and it's a way of securing

93
05:00.815 --> 05:04.170
our connection to our email server.

94
05:04.170 --> 05:06.810
So that way when we're sending an email,

95
05:06.810 --> 05:09.420
if somebody else intercepts our email

96
05:09.420 --> 05:12.720
somewhere along the line and they try to read it,

97
05:12.720 --> 05:16.383
because this is enabled that message will be encrypted

98
05:16.383 --> 05:18.720
and it'll be impossible for them to read

99
05:18.720 --> 05:21.360
what is in the content of our email.

100
05:21.360 --> 05:24.620
So this line basically will make this connection secure.

101
05:24.620 --> 05:27.720
Now, once we've secured our connection,

102
05:27.720 --> 05:30.990
the next thing to do is to actually log in.

103
05:30.990 --> 05:33.249
So we'll call connection.login()

104
05:33.249 --> 05:38.223
and here we have to provide a username and a password.

105
05:38.223 --> 05:41.460
So the username is simply the email

106
05:41.460 --> 05:44.940
that we use to log on to our email service.

107
05:44.940 --> 05:49.560
So in my case, it's just my_email and the password,

108
05:49.560 --> 05:52.497
I've just made up a new password.

109
05:52.497 --> 05:57.497
So my made-up password is ABCD1234 and then two brackets.

110
06:02.760 --> 06:07.260
And once I've logged in to my email provider,

111
06:07.260 --> 06:11.970
the final thing I want to do is to actually send my mail.

112
06:11.970 --> 06:15.120
Now the from address is my_email

113
06:15.120 --> 06:18.270
and the to address is the person

114
06:18.270 --> 06:20.550
who I want to send the email to.

115
06:20.550 --> 06:21.383
So I've set up

116
06:21.383 --> 06:25.227
a new dummy account called appbrewerytesting@yahoo.com.

117
06:28.500 --> 06:33.150
And make sure that you haven't got any typos in your email,

118
06:33.150 --> 06:38.150
your password, the SMTP URL or the recipient email address.

119
06:40.080 --> 06:43.830
Now finally, the last thing we want to add is the message.

120
06:43.830 --> 06:47.970
So this is what we actually want to send in our email.

121
06:47.970 --> 06:50.580
And just like we did with our file

122
06:50.580 --> 06:53.820
when we opened it at the very end, once we're done with it

123
06:53.820 --> 06:56.190
we're going to close it off as well.

124
06:56.190 --> 06:58.563
So now we can go ahead and hit Run.

125
07:00.127 --> 07:04.230
And you might get a number of errors at this point.

126
07:04.230 --> 07:06.644
Now it's important that if you do get an error,

127
07:06.644 --> 07:10.200
then you first check to make sure that you haven't got

128
07:10.200 --> 07:15.200
any typos here, here, here, or here.

129
07:15.870 --> 07:18.720
But once you've checked that through, then the next thing

130
07:18.720 --> 07:21.614
you can do is you can actually look at this error code

131
07:21.614 --> 07:24.210
and follow the URL.

132
07:24.210 --> 07:28.920
Now in our case, the reason is because by default,

133
07:28.920 --> 07:33.420
Gmail doesn't just let anybody access your email account.

134
07:33.420 --> 07:35.760
And you have other ways of making

135
07:35.760 --> 07:38.340
your account even more secure.

136
07:38.340 --> 07:42.420
In order to send email from a Gmail account with Python,

137
07:42.420 --> 07:44.310
the first thing you have to do is create

138
07:44.310 --> 07:47.010
a special password for your application.

139
07:47.010 --> 07:50.940
This means you have to go to the security settings

140
07:50.940 --> 07:53.010
for your Google account.

141
07:53.010 --> 07:54.540
So if you're inside Gmail,

142
07:54.540 --> 07:57.810
go ahead and click on your profile,

143
07:57.810 --> 08:02.363
go to Manage your Google account, and then go to Security.

144
08:02.363 --> 08:04.920
Now while you're here you first have

145
08:04.920 --> 08:07.083
to turn on 2-Step Verification.

146
08:13.470 --> 08:16.050
Once you've confirmed using your phone,

147
08:16.050 --> 08:19.030
go ahead and turn on 2-Step Verification

148
08:20.250 --> 08:24.330
and then go back to the Security page.

149
08:24.330 --> 08:25.830
And now you should be able to see

150
08:25.830 --> 08:28.800
this section, App passwords show up.

151
08:28.800 --> 08:32.130
Go ahead and click on it and enter your password

152
08:32.130 --> 08:33.630
for your Google account again.

153
08:35.073 --> 08:38.160
(rapid typing)

154
08:38.160 --> 08:42.627
And then go ahead and create a Other type of app.

155
08:42.627 --> 08:47.627
And we'll name it birthday_wisher and click GENERATE.

156
08:49.920 --> 08:52.020
Now here's the really important part,

157
08:52.020 --> 08:55.230
you're going to get your app password show up here, and

158
08:55.230 --> 08:58.660
I want you to select all of it and copy it,

159
09:00.210 --> 09:04.293
and then you'll be able to paste it inside your code.

160
09:05.144 --> 09:07.920
Once we've done all of this,

161
09:07.920 --> 09:11.040
then we can go back and hit Run again,

162
09:11.040 --> 09:13.260
and you'll see this time we see

163
09:13.260 --> 09:15.683
the, "Process finished with exit code 0",

164
09:15.683 --> 09:19.590
which means all of the code ran successfully.

165
09:19.590 --> 09:22.620
And now if I take a look at my Sent box,

166
09:22.620 --> 09:26.190
you can see that I've got this message that's been sent.

167
09:26.190 --> 09:28.710
And if I take a look at the email address

168
09:28.710 --> 09:30.960
which the email was sent to,

169
09:30.960 --> 09:33.120
then you can see that in my inbox

170
09:33.120 --> 09:34.770
there's actually nothing here.

171
09:34.770 --> 09:36.951
But if you take a look inside spam,

172
09:36.951 --> 09:39.600
then there is our brilliant email

173
09:39.600 --> 09:41.973
that came from our Python code.

174
09:43.500 --> 09:46.890
So first thing I'm going to do is change that to Not spam

175
09:46.890 --> 09:49.980
to make sure that it goes into the actual inbox.

176
09:49.980 --> 09:52.200
And the next thing we want to be able to do is

177
09:52.200 --> 09:55.890
how can we make our email seem less like spam?

178
09:55.890 --> 09:59.894
So an email without a subject headline is prime target

179
09:59.894 --> 10:02.460
for being filtered as spam.

180
10:02.460 --> 10:04.830
So let's go into our email

181
10:04.830 --> 10:07.680
and see how we can add a subject line.

182
10:07.680 --> 10:08.940
It's pretty simple.

183
10:08.940 --> 10:12.300
It goes inside the message parameter

184
10:12.300 --> 10:16.410
and all we have to do is just write the word subject, colon,

185
10:16.410 --> 10:18.450
and then we can put in whatever it is

186
10:18.450 --> 10:22.080
that we want to use as the subject of the email, like this.

187
10:22.080 --> 10:25.830
Now, how do you put in the content or the body of the email?

188
10:25.830 --> 10:30.690
Well, you add two new lines using \n \n

189
10:30.690 --> 10:32.460
and then you can put the content.

190
10:32.460 --> 10:36.607
So, "This is the body of my email."

191
10:36.607 --> 10:39.210
And let's just split this up

192
10:39.210 --> 10:41.163
so it's a little bit easier to read.

193
10:43.620 --> 10:46.463
And now I can hit Run again.

194
10:46.463 --> 10:48.840
And once that's done, you can see

195
10:48.840 --> 10:51.418
that in my Gmail in the Sent folder

196
10:51.418 --> 10:56.418
that this message now has a subject line and a body.

197
10:57.120 --> 11:00.230
And it's the same thing when I go to my...

198
11:06.120 --> 11:09.210
And when I take a look at this new email that came through,

199
11:09.210 --> 11:11.340
you can see the Subject line

200
11:11.340 --> 11:14.283
and the body of the email being separated.

201
11:15.180 --> 11:17.640
Now we can actually avoid having

202
11:17.640 --> 11:20.490
to write this line connection.close(),

203
11:20.490 --> 11:24.210
if we do the same trick as we did with file opening,

204
11:24.210 --> 11:26.640
we use the "with" keyword.

205
11:26.640 --> 11:31.640
So we can say that with smtp.SMTP, to create the connection,

206
11:31.860 --> 11:34.980
and then we save that as the connection,

207
11:34.980 --> 11:37.710
then we can indent all of the rest

208
11:37.710 --> 11:39.780
of the code inside this block.

209
11:39.780 --> 11:43.350
And once it's done with sending the email,

210
11:43.350 --> 11:47.010
it will close off that connection automatically.

211
11:47.010 --> 11:50.160
So this is how you could send email using Python

212
11:50.160 --> 11:51.870
and smtplib.

213
11:51.870 --> 11:54.660
Now there's quite a few things here that are prone

214
11:54.660 --> 11:58.290
to errors, especially given that we're typing a lot

215
11:58.290 --> 12:01.963
of things in plain text, like our email,

216
12:01.963 --> 12:04.350
our password and a bunch of things.

217
12:04.350 --> 12:08.220
So if you are getting errors when you are running your code

218
12:08.220 --> 12:11.100
or if it's not doing what you expect it to,

219
12:11.100 --> 12:14.910
make sure that you've checked against all of these strings

220
12:14.910 --> 12:18.150
and it's actually what you expected it to be.

221
12:18.150 --> 12:20.700
Secondly, make sure that whichever account

222
12:20.700 --> 12:23.370
you're sending from that you actually go ahead

223
12:23.370 --> 12:27.390
into the account and modify the security settings.

224
12:27.390 --> 12:29.970
For example, let's say I wanted to send

225
12:29.970 --> 12:34.230
from this email address, so I'm going to put that in here.

226
12:34.230 --> 12:37.140
And then in this case, the email server I need

227
12:37.140 --> 12:40.260
to connect to will be Yahoo's email server,

228
12:40.260 --> 12:43.950
which is under the URL that I showed you before,

229
12:43.950 --> 12:48.907
smtp.mail.yahoo.com.

230
12:50.160 --> 12:52.590
And that is actually not enough.

231
12:52.590 --> 12:54.700
So let me just change my recipient

232
12:58.440 --> 13:00.090
and I hit Run.

233
13:00.090 --> 13:02.370
This is the error that you'll see.

234
13:02.370 --> 13:05.130
It says SMTP server disconnected,

235
13:05.130 --> 13:07.590
connection unexpectedly closed.

236
13:07.590 --> 13:10.200
Now this could be down to a number of reasons.

237
13:10.200 --> 13:13.692
For example, a typo in this part,

238
13:13.692 --> 13:16.260
but also if your account doesn't

239
13:16.260 --> 13:18.840
actually allow less secure apps.

240
13:18.840 --> 13:21.570
So on Yahoo, the process is a little bit different.

241
13:21.570 --> 13:25.290
You have to go into your account, go to Account Info

242
13:25.290 --> 13:27.213
and then go to Account security.

243
13:28.710 --> 13:30.940
And you'll have to log in again

244
13:33.780 --> 13:37.098
and Generate a new app password.

245
13:37.098 --> 13:39.360
So we're going to create a new app

246
13:39.360 --> 13:43.140
and we're going to give it a custom name, Python code.

247
13:43.140 --> 13:45.030
And then click Generate,

248
13:45.030 --> 13:50.030
and now we have an app password to use for our Python code.

249
13:51.060 --> 13:55.020
So now back over here, we change this password

250
13:55.020 --> 13:57.990
to the one that we just copied over.

251
13:57.990 --> 14:01.680
And now if I hit Run, that error should go away

252
14:01.680 --> 14:05.310
and we should get, "Process finished with exit code 0".

253
14:05.310 --> 14:08.943
And when I take a look over here in my Inbox,

254
14:11.430 --> 14:15.393
I've got my email from appbrewerytesting@yahoo.com.

255
14:17.250 --> 14:21.060
So check your Security settings, check your spam

256
14:21.060 --> 14:24.587
and check that you haven't made any typos.

257
14:24.587 --> 14:26.880
And if none of that works,

258
14:26.880 --> 14:30.000
then just simply try doing everything I did in the video

259
14:30.000 --> 14:35.000
but with a new email account that you set up with Gmail.

260
14:35.040 --> 14:37.860
Then you can use the same SMTP address

261
14:37.860 --> 14:39.480
and the same process that you saw

262
14:39.480 --> 14:41.253
in the video to try this out.