1
00:00:00,690 --> 00:00:01,800
Instructor: The first thing we have

2
00:00:01,800 --> 00:00:06,390
to figure out is how exactly does email work?

3
00:00:06,390 --> 00:00:08,340
We all know that we can log onto Gmail

4
00:00:08,340 --> 00:00:11,970
or yahoo.com and we can start filling out an email.

5
00:00:11,970 --> 00:00:15,240
It's pretty much second nature to everybody by now.

6
00:00:15,240 --> 00:00:18,240
But what actually happens behind the scenes?

7
00:00:18,240 --> 00:00:20,430
Well, let's say that we have a sender,

8
00:00:20,430 --> 00:00:25,430
angela@gmail.com and a recipient, timmy@yahoo.com.

9
00:00:25,650 --> 00:00:28,980
Now, in order to send this email from my Gmail account

10
00:00:28,980 --> 00:00:33,300
to another email account, what happens behind the scene is

11
00:00:33,300 --> 00:00:35,790
that there's a Gmail mail server,

12
00:00:35,790 --> 00:00:37,980
which will receive my message.

13
00:00:37,980 --> 00:00:40,380
And then there's a Yahoo Mail server,

14
00:00:40,380 --> 00:00:43,770
which will store the message until Timmy logs

15
00:00:43,770 --> 00:00:47,670
onto his computer and logs onto yahoo.com,

16
00:00:47,670 --> 00:00:50,823
which downloads the email from the Yahoo Mail server.

17
00:00:51,690 --> 00:00:56,329
So this email is gonna move between all of these steps.

18
00:00:56,329 --> 00:00:58,080
And in order to do this,

19
00:00:58,080 --> 00:01:00,990
it relies on something called SMTP,

20
00:01:00,990 --> 00:01:03,726
the Simple Mail Transfer Protocol.

21
00:01:03,726 --> 00:01:07,530
And this contains all of the rules that determine

22
00:01:07,530 --> 00:01:11,160
how an email is received by mail servers passed

23
00:01:11,160 --> 00:01:12,930
onto the next mail server

24
00:01:12,930 --> 00:01:15,993
and how email can be sent around the internet.

25
00:01:17,340 --> 00:01:19,683
Now, a good analogy for SMTP is

26
00:01:19,683 --> 00:01:24,240
if you imagine these mail servers as a post office

27
00:01:24,240 --> 00:01:27,210
and Timmy's computer being the mailbox.

28
00:01:27,210 --> 00:01:32,210
Then SMTP is basically the postman who knows how to handle

29
00:01:32,220 --> 00:01:36,120
the email and take it to various post offices

30
00:01:36,120 --> 00:01:39,510
and eventually put it into Timmy's computer.

31
00:01:39,510 --> 00:01:42,990
So in Python there's a module called smtplib,

32
00:01:42,990 --> 00:01:47,460
which allows us to use SMTP to send our email

33
00:01:47,460 --> 00:01:50,550
to any address on the internet.

34
00:01:50,550 --> 00:01:54,360
To start, I recommend setting up two fresh email accounts.

35
00:01:54,360 --> 00:01:57,060
Create a new email account with Gmail

36
00:01:57,060 --> 00:02:00,360
and also create a new email account with Yahoo.

37
00:02:00,360 --> 00:02:03,000
These new email accounts will be perfect

38
00:02:03,000 --> 00:02:04,260
for testing your code

39
00:02:04,260 --> 00:02:06,930
and following along with the video tutorials.

40
00:02:06,930 --> 00:02:09,000
Plus, we'll be making those email accounts

41
00:02:09,000 --> 00:02:11,490
a little bit less secure to test our code.

42
00:02:11,490 --> 00:02:12,900
So that's another reason to set up

43
00:02:12,900 --> 00:02:15,750
some testing email addresses for now.

44
00:02:15,750 --> 00:02:18,150
After you've set up your new email accounts,

45
00:02:18,150 --> 00:02:21,150
head over to the course resources and download the zip file

46
00:02:21,150 --> 00:02:23,300
with the starting code for today's lessons.

47
00:02:24,270 --> 00:02:27,390
And then we're gonna open that up using PyCharm.

48
00:02:27,390 --> 00:02:30,582
Now I want you to take a look inside the starting project.

49
00:02:30,582 --> 00:02:33,150
There is a main.py file

50
00:02:33,150 --> 00:02:36,180
and there's also a quotes.txt file.

51
00:02:36,180 --> 00:02:37,710
Don't worry about this file for now.

52
00:02:37,710 --> 00:02:39,060
We're gonna come back to it

53
00:02:39,060 --> 00:02:41,640
when we explore the datetime module.

54
00:02:41,640 --> 00:02:44,850
For now, we're gonna be working within the main.py.

55
00:02:44,850 --> 00:02:49,481
And I wanna show you how you can use this smtplib library

56
00:02:49,481 --> 00:02:54,377
to start sending emails straight from your Python code.

57
00:02:54,377 --> 00:02:58,290
As always, we import the module smtplib

58
00:02:58,290 --> 00:03:00,930
and then we can start using it.

59
00:03:00,930 --> 00:03:05,095
Once we've imported this SMTP library essentially,

60
00:03:05,095 --> 00:03:09,330
we can use it to create a new SMTP object.

61
00:03:09,330 --> 00:03:12,600
So we're gonna call that object a new connection

62
00:03:12,600 --> 00:03:15,000
because it's basically a way for us to be able

63
00:03:15,000 --> 00:03:20,000
to connect to our email provider's SMTP email server.

64
00:03:20,010 --> 00:03:23,130
We're gonna do this by tapping into the smtplib

65
00:03:23,130 --> 00:03:27,810
and then creating a object from the SMTP class.

66
00:03:27,810 --> 00:03:29,670
Now, when we create this object,

67
00:03:29,670 --> 00:03:33,759
one of the things that we should specify is the location

68
00:03:33,759 --> 00:03:37,648
of our email providers SMTP server.

69
00:03:37,648 --> 00:03:42,648
Now for Gmail, it's simply smtp.gmail.com

70
00:03:43,140 --> 00:03:45,870
but it's different for every email provider.

71
00:03:45,870 --> 00:03:50,310
So that means if your email ends in @gmail.com

72
00:03:50,310 --> 00:03:52,620
then this would be how you would connect

73
00:03:52,620 --> 00:03:54,450
to your email server.

74
00:03:54,450 --> 00:03:56,010
In our case, I've created

75
00:03:56,010 --> 00:04:01,010
a testing email called appbreweryinfo@gmail.com.

76
00:04:02,160 --> 00:04:06,000
And this part that's before the @ sign is

77
00:04:06,000 --> 00:04:09,390
the identity of my email account.

78
00:04:09,390 --> 00:04:11,850
And the part after the @ sign is

79
00:04:11,850 --> 00:04:14,910
the identity of my email provider.

80
00:04:14,910 --> 00:04:19,659
So in my case, I need to connect to smtp.gmail.com.

81
00:04:19,659 --> 00:04:23,160
But if you have a different email provider,

82
00:04:23,160 --> 00:04:27,240
for example if you're with Hotmail, it's smtp.live.com.

83
00:04:27,240 --> 00:04:32,190
And if you are with Yahoo, it's smtp.mail.yahoo.com.

84
00:04:32,190 --> 00:04:35,190
And if you're with a completely different email provider

85
00:04:35,190 --> 00:04:38,670
than simply just Google your email provider

86
00:04:38,670 --> 00:04:40,590
and the SMTP information.

87
00:04:40,590 --> 00:04:42,660
And you should find an article somewhere

88
00:04:42,660 --> 00:04:46,162
that describes a URL that looks something like this.

89
00:04:46,162 --> 00:04:48,780
Once I've created my connection,

90
00:04:48,780 --> 00:04:52,110
the next thing I need to do is to go ahead

91
00:04:52,110 --> 00:04:55,080
and call start TLS.

92
00:04:55,080 --> 00:04:59,100
Now, TLS stands for Transport Layer Security

93
00:04:59,100 --> 00:05:00,815
and it's a way of securing

94
00:05:00,815 --> 00:05:04,170
our connection to our email server.

95
00:05:04,170 --> 00:05:06,810
So that way when we're sending an email,

96
00:05:06,810 --> 00:05:09,420
if somebody else intercepts our email

97
00:05:09,420 --> 00:05:12,720
somewhere along the line and they try to read it.

98
00:05:12,720 --> 00:05:16,383
Because this is enabled that message will be encrypted

99
00:05:16,383 --> 00:05:18,720
and it'll be impossible for them to read

100
00:05:18,720 --> 00:05:21,360
what is in the content of our email.

101
00:05:21,360 --> 00:05:24,620
So this line basically will make this connection secure.

102
00:05:24,620 --> 00:05:27,720
Now, once we've secured our connection,

103
00:05:27,720 --> 00:05:30,990
the next thing to do is to actually log in.

104
00:05:30,990 --> 00:05:33,249
So we'll call connection.login

105
00:05:33,249 --> 00:05:38,223
and here we have to provide a username and a password.

106
00:05:38,223 --> 00:05:41,460
So the username is simply the email

107
00:05:41,460 --> 00:05:44,940
that we use to log on to our email service.

108
00:05:44,940 --> 00:05:49,560
So in my case, it's just my email and the password.

109
00:05:49,560 --> 00:05:52,497
I've just made up a new password.

110
00:05:52,497 --> 00:05:57,497
So my made up password is ABCD1234 and then two brackets.

111
00:06:02,760 --> 00:06:07,260
And once I've logged in to my email provider,

112
00:06:07,260 --> 00:06:11,970
the final thing I wanna do is to actually send my mail.

113
00:06:11,970 --> 00:06:15,120
Now the from address is my email

114
00:06:15,120 --> 00:06:18,270
and the to address is the person

115
00:06:18,270 --> 00:06:20,550
who I want to send the email to.

116
00:06:20,550 --> 00:06:21,383
So I've set up

117
00:06:21,383 --> 00:06:25,227
a new dummy account called appbrewerytesting@yahoo.com.

118
00:06:28,500 --> 00:06:33,150
And make sure that you haven't got any typos in your email,

119
00:06:33,150 --> 00:06:38,150
your password, the SMTP URL or the recipient email address.

120
00:06:40,080 --> 00:06:43,830
Now finally, the last thing we wanna add is the message.

121
00:06:43,830 --> 00:06:47,970
So this is what we actually want to send in our email.

122
00:06:47,970 --> 00:06:50,580
And just like we did with our file

123
00:06:50,580 --> 00:06:53,820
when we opened it at the very end, once we're done with it

124
00:06:53,820 --> 00:06:56,190
we're going to close it off as well.

125
00:06:56,190 --> 00:06:58,563
So now we can go ahead and hit run.

126
00:07:00,127 --> 00:07:04,230
And you might get a number of errors at this point.

127
00:07:04,230 --> 00:07:06,644
Now it's important that if you do get an error,

128
00:07:06,644 --> 00:07:10,200
then you first check to make sure that you haven't got

129
00:07:10,200 --> 00:07:15,200
any typos here, here, here, or here.

130
00:07:15,870 --> 00:07:18,720
But once you've checked that through, then the next thing

131
00:07:18,720 --> 00:07:21,614
you can do is you can actually look at this error code

132
00:07:21,614 --> 00:07:24,210
and follow the URL.

133
00:07:24,210 --> 00:07:28,920
Now in our case, the reason is because by default,

134
00:07:28,920 --> 00:07:33,420
Gmail doesn't just let anybody access your email account.

135
00:07:33,420 --> 00:07:35,760
And you have other ways of making

136
00:07:35,760 --> 00:07:38,340
your account even more secure.

137
00:07:38,340 --> 00:07:42,420
In order to send email from a Gmail account with Python,

138
00:07:42,420 --> 00:07:44,310
the first thing you have to do is create

139
00:07:44,310 --> 00:07:47,010
a special password for your application.

140
00:07:47,010 --> 00:07:50,940
This means you have to go to the security settings

141
00:07:50,940 --> 00:07:53,010
for your Google account.

142
00:07:53,010 --> 00:07:54,540
So if you're inside Gmail,

143
00:07:54,540 --> 00:07:57,810
go ahead and click on your profile.

144
00:07:57,810 --> 00:08:02,363
Go to manage your Google account and then go to security.

145
00:08:02,363 --> 00:08:04,920
Now while you're here you first have

146
00:08:04,920 --> 00:08:07,083
to turn on two step verification.

147
00:08:13,470 --> 00:08:16,050
Once you've confirmed using your phone,

148
00:08:16,050 --> 00:08:19,030
go ahead and turn on two step verification

149
00:08:20,250 --> 00:08:24,330
and then go back to the security page.

150
00:08:24,330 --> 00:08:25,830
And now you should be able to see

151
00:08:25,830 --> 00:08:28,800
this section app passwords show up.

152
00:08:28,800 --> 00:08:32,130
Go ahead and click on it and enter your password

153
00:08:32,130 --> 00:08:33,630
for your Google account again.

154
00:08:35,073 --> 00:08:38,159
(rapid typing)

155
00:08:38,159 --> 00:08:42,626
And then go ahead and create a other type of app.

156
00:08:42,626 --> 00:08:47,627
And we'll name it Birthday Wisher and click generate.

157
00:08:49,920 --> 00:08:52,020
Now here's the really important part.

158
00:08:52,020 --> 00:08:55,230
You're gonna get your app password show up here.

159
00:08:55,230 --> 00:08:58,660
I want you to select all of it and copy it

160
00:09:00,210 --> 00:09:04,293
and then you'll be able to paste it inside your code.

161
00:09:05,144 --> 00:09:07,920
Once we've done all of this,

162
00:09:07,920 --> 00:09:11,040
then we can go back and hit run again.

163
00:09:11,040 --> 00:09:13,260
And you'll see this time we see

164
00:09:13,260 --> 00:09:15,683
the process finish with exit code zero,

165
00:09:15,683 --> 00:09:19,590
which means all of the code ran successfully.

166
00:09:19,590 --> 00:09:22,620
And now if I take a look at my sent box,

167
00:09:22,620 --> 00:09:26,190
you can see that I've got this message that's been sent.

168
00:09:26,190 --> 00:09:28,710
And if I take a look at the email address

169
00:09:28,710 --> 00:09:30,960
which the email was sent to,

170
00:09:30,960 --> 00:09:33,120
then you can see that in my inbox

171
00:09:33,120 --> 00:09:34,770
there's actually nothing here.

172
00:09:34,770 --> 00:09:36,951
But if you take a look inside spam,

173
00:09:36,951 --> 00:09:39,600
then there is our brilliant email

174
00:09:39,600 --> 00:09:41,973
that came from our Python code.

175
00:09:43,500 --> 00:09:46,890
So first thing I'm gonna do is change that to not spam

176
00:09:46,890 --> 00:09:49,980
to make sure that it goes into the actual inbox.

177
00:09:49,980 --> 00:09:52,200
And the next thing we wanna be able to do is

178
00:09:52,200 --> 00:09:55,890
how can we make our email seem less like spam?

179
00:09:55,890 --> 00:09:59,894
So an email without a subject headline is prime target

180
00:09:59,894 --> 00:10:02,460
for being filtered as spam.

181
00:10:02,460 --> 00:10:04,830
So let's go into our email

182
00:10:04,830 --> 00:10:07,680
and see how we can add a subject line.

183
00:10:07,680 --> 00:10:08,940
It's pretty simple.

184
00:10:08,940 --> 00:10:12,300
It goes inside the message parameter

185
00:10:12,300 --> 00:10:16,410
and all we have to do is just write the word subject, colon.

186
00:10:16,410 --> 00:10:18,450
And then we can put in whatever it is

187
00:10:18,450 --> 00:10:22,080
that we wanna use as the subject of the email like this.

188
00:10:22,080 --> 00:10:25,830
Now, how do you put in the content or the body of the email?

189
00:10:25,830 --> 00:10:30,690
Well, you add two new lines using /n /n

190
00:10:30,690 --> 00:10:32,460
and then you can put the content.

191
00:10:32,460 --> 00:10:36,607
So this is the body of my email.

192
00:10:36,607 --> 00:10:39,210
And let's just split this up

193
00:10:39,210 --> 00:10:41,163
so it's a little bit easier to read.

194
00:10:43,620 --> 00:10:46,463
And now I can hit run again.

195
00:10:46,463 --> 00:10:48,840
And once that's done, you can see

196
00:10:48,840 --> 00:10:51,418
that in my Gmail in the sent folder

197
00:10:51,418 --> 00:10:56,418
that this message now has a subject line and a body.

198
00:10:57,120 --> 00:11:00,230
And it's the same thing when I go to my...

199
00:11:06,120 --> 00:11:09,210
And when I take a look at this new email that came through,

200
00:11:09,210 --> 00:11:11,340
you can see the subject line

201
00:11:11,340 --> 00:11:14,283
and the body of the email being separated.

202
00:11:15,180 --> 00:11:17,640
Now we can actually avoid having

203
00:11:17,640 --> 00:11:20,490
to write this line connection.close

204
00:11:20,490 --> 00:11:24,210
if we do the same trick as we did with file opening.

205
00:11:24,210 --> 00:11:26,640
We use the with keyword

206
00:11:26,640 --> 00:11:31,640
so we can say that with smtp.SMTP to create the connection

207
00:11:31,860 --> 00:11:34,980
and then we save that as the connection.

208
00:11:34,980 --> 00:11:37,710
Then we can indent all of the rest

209
00:11:37,710 --> 00:11:39,780
of the code inside this block.

210
00:11:39,780 --> 00:11:43,350
And once it's done with sending the email,

211
00:11:43,350 --> 00:11:47,010
it will close off that connection automatically.

212
00:11:47,010 --> 00:11:50,160
So this is how you could send email using Python

213
00:11:50,160 --> 00:11:51,870
and smtplib.

214
00:11:51,870 --> 00:11:54,660
Now there's quite a few things here that are prone

215
00:11:54,660 --> 00:11:58,290
to errors, especially given that we're typing a lot

216
00:11:58,290 --> 00:12:01,963
of things in plain text, like our email,

217
00:12:01,963 --> 00:12:04,350
our password and a bunch of things.

218
00:12:04,350 --> 00:12:08,220
So if you are getting errors when you are running your code

219
00:12:08,220 --> 00:12:11,100
or if it's not doing what you expect it to,

220
00:12:11,100 --> 00:12:14,910
make sure that you've checked against all of these strings

221
00:12:14,910 --> 00:12:18,150
and it's actually what you expected it to be.

222
00:12:18,150 --> 00:12:20,700
Secondly, make sure that whichever account

223
00:12:20,700 --> 00:12:23,370
you're sending from that you actually go ahead

224
00:12:23,370 --> 00:12:27,390
into the account and modify the security settings.

225
00:12:27,390 --> 00:12:29,970
For example, let's say I wanted to send

226
00:12:29,970 --> 00:12:34,230
from this email address, so I'm gonna put that in here.

227
00:12:34,230 --> 00:12:37,140
And then in this case, the email server I need

228
00:12:37,140 --> 00:12:40,260
to connect to will be Yahoo's email server,

229
00:12:40,260 --> 00:12:43,950
which is under the URL that I showed you before,

230
00:12:43,950 --> 00:12:48,907
smtp.mail.yahoo.com.

231
00:12:50,160 --> 00:12:52,590
And that is actually not enough.

232
00:12:52,590 --> 00:12:54,700
So let me just change my recipient

233
00:12:58,440 --> 00:13:00,090
and I hit run.

234
00:13:00,090 --> 00:13:02,370
This is the error that you'll see.

235
00:13:02,370 --> 00:13:05,130
It says SMTP server disconnected,

236
00:13:05,130 --> 00:13:07,590
connection unexpectedly closed.

237
00:13:07,590 --> 00:13:10,200
Now this could be down to a number of reasons.

238
00:13:10,200 --> 00:13:13,692
For example, a typo in this part.

239
00:13:13,692 --> 00:13:16,260
But also if your account doesn't

240
00:13:16,260 --> 00:13:18,840
actually allow less secure apps.

241
00:13:18,840 --> 00:13:21,570
So on Yahoo, the process is a little bit different.

242
00:13:21,570 --> 00:13:25,290
You have to go into your account, go to account info

243
00:13:25,290 --> 00:13:27,213
and then go to account security.

244
00:13:28,710 --> 00:13:30,940
And you'll have to log in again

245
00:13:33,780 --> 00:13:37,098
and generate a new app password.

246
00:13:37,098 --> 00:13:39,360
So we're gonna create a new app

247
00:13:39,360 --> 00:13:43,140
and we're gonna give it a custom name, Python code.

248
00:13:43,140 --> 00:13:45,030
And then click generate.

249
00:13:45,030 --> 00:13:50,030
And now we have a app password to use for our Python code.

250
00:13:51,060 --> 00:13:55,020
So now back over here, we change this password

251
00:13:55,020 --> 00:13:57,990
to the one that we just copied over.

252
00:13:57,990 --> 00:14:01,680
And now if I hit run, that error should go away

253
00:14:01,680 --> 00:14:05,310
and we should get process finished with exit code zero.

254
00:14:05,310 --> 00:14:08,943
And when I take a look over here in my inbox,

255
00:14:11,430 --> 00:14:15,393
I've got my email from appbrewerytesting@yahoo.com.

256
00:14:17,250 --> 00:14:21,060
So check your security settings, check your spam

257
00:14:21,060 --> 00:14:24,587
and check that you haven't made any typos.

258
00:14:24,587 --> 00:14:26,880
And if none of that works,

259
00:14:26,880 --> 00:14:30,000
then just simply try doing everything I did in the video

260
00:14:30,000 --> 00:14:35,000
but with a new email account that you set up with Gmail.

261
00:14:35,040 --> 00:14:37,860
Then you can use the same SMTP address

262
00:14:37,860 --> 00:14:39,480
and the same process that you saw

263
00:14:39,480 --> 00:14:41,253
in the video to try this out.

