1

00:00:00,210  -->  00:00:06,250
Welcome to 11:46 in this lesson we're going to learn how to send e-mails from our Python programs.

2

00:00:06,300  -->  00:00:11,610
This can be a great way to send notifications so if you have a program that is doing Web scraping and

3

00:00:11,610  -->  00:00:17,190
checking a Web site over and over again you can have this program then email you whenever it finds some

4

00:00:17,190  -->  00:00:22,440
particular thing that you're looking for or maybe you need to send out e-mails to a bunch of specific

5

00:00:22,440  -->  00:00:29,430
people based on some criteria that you program in like maybe a program that goes through a spreadsheet

6

00:00:29,490  -->  00:00:36,250
and then finds people who are behind on dues or or haven't received a reminder in a while and then just

7

00:00:36,270  -->  00:00:37,590
e-mail them in particular.

8

00:00:37,670  -->  00:00:44,210
This can save you a lot of copying and pasting e-mail addresses into some email program like G-mail

9

00:00:44,290  -->  00:00:44,550
.

10

00:00:44,790  -->  00:00:45,960
Let's get started.

11

00:00:45,960  -->  00:00:54,240
So the protocol that sending e-mail uses is called SMTP simple Mail Transfer Protocol and Python has

12

00:00:54,240  -->  00:00:58,960
an implementation of this protocol in a module called a.p lib.

13

00:00:59,100  -->  00:01:01,170
So there are a lot of different steps to this.

14

00:01:01,260  -->  00:01:06,540
So I'm just going to go through all the steps of sending an email first and then we can later look at

15

00:01:06,540  -->  00:01:08,960
each step in more detail.

16

00:01:08,970  -->  00:01:12,860
So first go ahead and import S-M TPV lib.

17

00:01:12,960  -->  00:01:16,250
This is the simple Mail Transfer Protocol library.

18

00:01:16,270  -->  00:01:22,860
Next we'll have to create a connection object which Ostrat store in a variable called Can the function

19

00:01:22,860  -->  00:01:31,860
we want to call is SMP lib dot S-M T-P in caps and then we'll have a connect to the SMTP server.

20

00:01:31,860  -->  00:01:34,630
This is the domain name of your e-mail server.

21

00:01:34,830  -->  00:01:42,540
So I'm going to use G-mail and there as some to you the server is at SMPTE Dutchie mail dot com.

22

00:01:42,660  -->  00:01:47,320
And then followed by the port number which is usually port 5 8 7.

23

00:01:47,430  -->  00:01:51,000
So we have this connection object.

24

00:01:51,690  -->  00:01:55,140
And now let's go ahead and connect to the server.

25

00:01:55,170  -->  00:02:00,780
So this will actually send out Internet traffic from our Python program.

26

00:02:00,780  -->  00:02:08,150
We're going to call the L-O function which is a lot of history of why it has this weird name it used

27

00:02:08,170  -->  00:02:15,450
be hello with just one L and then that got switched around to L-O and you just know that you have to

28

00:02:15,450  -->  00:02:17,450
call this function to start the connection.

29

00:02:17,460  -->  00:02:20,240
So that's the helo function.

30

00:02:20,250  -->  00:02:29,340
Next you can see that it returned this tuple of an integer which is the code the response code anything

31

00:02:29,340  -->  00:02:31,340
that begins with 200 means OK.

32

00:02:31,350  -->  00:02:33,040
So 250 means Yeah.

33

00:02:33,060  -->  00:02:33,740
OK.

34

00:02:33,750  -->  00:02:34,770
We connected.

35

00:02:35,010  -->  00:02:42,060
And then the second item here this is actually a bite's object and not a string but it can easily be

36

00:02:42,060  -->  00:02:43,420
interpreted as a string.

37

00:02:43,620  -->  00:02:46,070
Bytes are sort of beyond the scope of this lesson.

38

00:02:46,170  -->  00:02:50,480
But here you can see OK it return a semester that said you know everything works great.

39

00:02:50,520  -->  00:02:54,640
So after connecting with Hello we have to start the DNS encryption.

40

00:02:54,660  -->  00:02:58,380
So that way when we send our password it'll be encrypted.

41

00:02:58,380  -->  00:03:01,870
So this return code to 20 that means everything's OK.

42

00:03:01,890  -->  00:03:04,440
And now let's go ahead and log in.

43

00:03:04,540  -->  00:03:08,150
And so we have to call the log in function pass our user name.

44

00:03:08,190  -->  00:03:11,150
So a swaggered at G-mail dot com.

45

00:03:11,400  -->  00:03:14,480
And then the password which is this.

46

00:03:14,820  -->  00:03:20,460
Of course by the time you watch this video I will change this password so don't bother trying to hack

47

00:03:20,460  -->  00:03:22,820
into my e-mail account.

48

00:03:23,160  -->  00:03:28,770
So no we're our script is now logged in and we can start sending e-mails by calling the send mail function

49

00:03:29,020  -->  00:03:29,460
.

50

00:03:29,640  -->  00:03:36,930
So first we'll have to get the from address which will just be a sweigart at gmail dot com and we pass

51

00:03:36,930  -->  00:03:39,060
the two address.

52

00:03:39,060  -->  00:03:42,390
So I'm just going to go ahead and email myself.

53

00:03:42,930  -->  00:03:48,210
Just remember that the first one is the from address which you should make your e-mail address the same

54

00:03:48,210  -->  00:03:52,660
that you logged in with and then the second is who this e-mail is going to.

55

00:03:53,100  -->  00:03:55,260
And then followed by the body of the e-mail.

56

00:03:55,260  -->  00:04:01,500
Now this is kind of complicated because the headers and the body of the e-mail are all crammed into

57

00:04:01,500  -->  00:04:03,660
this string and they follow a certain format.

58

00:04:03,660  -->  00:04:04,510
So here's the format.

59

00:04:04,530  -->  00:04:11,850
First type subject with a capital S colon space and then type in the subject so easily a single line

60

00:04:11,850  -->  00:04:14,060
.

61

00:04:14,370  -->  00:04:19,890
I'll say the subject is so long and will have a newline character that ends the subject and then another

62

00:04:19,890  -->  00:04:21,180
newline character.

63

00:04:21,180  -->  00:04:23,280
That means we're done with the headers.

64

00:04:23,280  -->  00:04:27,800
There are other headers besides subject but I'm just going to talk about the subject header.

65

00:04:28,060  -->  00:04:32,490
So basically we just need these two newline characters and then we'll have the main body of the email

66

00:04:32,490  -->  00:04:32,710
.

67

00:04:32,850  -->  00:04:36,050
So I'll just write an email to myself dear owl.

68

00:04:36,360  -->  00:04:42,550
Now the newline character so long and thanks for all the fish.

69

00:04:42,660  -->  00:04:46,710
New Line new line from Al.

70

00:04:47,430  -->  00:04:54,510
OK and then I'll just press enter and then it sends this email so you can send multiple e-mails at a

71

00:04:54,510  -->  00:04:58,460
time with the sendmail function you can consult the documentation for that.

72

00:04:58,500  -->  00:05:04,560
And the rease the return object and the return value from sendmail is going to be a dictionary that

73

00:05:04,710  -->  00:05:08,160
has all of the e-mails that it failed to send.

74

00:05:08,160  -->  00:05:12,960
So if you get a blank dictionary back that means the e-mails were all sent correctly.

75

00:05:13,050  -->  00:05:19,150
And so we could then send another e-mail by calling the sendmail method again.

76

00:05:19,680  -->  00:05:26,790
But at this point I'm already done so I'll have my script just call quit and that will disconnect me

77

00:05:26,790  -->  00:05:29,890
from the SMTP server.

78

00:05:31,000  -->  00:05:35,730
I can go ahead and check my email right now and see that.

79

00:05:36,160  -->  00:05:37,690
Yep that got e-mailed to me.

80

00:05:38,010  -->  00:05:39,400
OK so that's the entire process.

81

00:05:39,420  -->  00:05:41,670
Let's dive into the details for each part.

82

00:05:41,670  -->  00:05:45,990
This is kind of complicated because there's a lot of different steps you don't have to memorize all

83

00:05:45,990  -->  00:05:46,770
the steps.

84

00:05:46,860  -->  00:05:50,760
Just be familiar with them enough so that you can look at the documentation.

85

00:05:50,760  -->  00:05:54,210
So the first thing we do is import the S.M. T.P. library.

86

00:05:54,210  -->  00:05:55,860
That's pretty standard enough.

87

00:05:56,100  -->  00:06:03,040
And we call this SMTP function passing the domain name and port number.

88

00:06:03,210  -->  00:06:10,740
So the port number for the SMTP protocol is 5 8 7 ports are a part of the Internet address that you're

89

00:06:10,740  -->  00:06:12,400
sending data to.

90

00:06:12,450  -->  00:06:16,790
So it's on this server and then on this port of that server.

91

00:06:16,800  -->  00:06:18,180
So this is a domain name.

92

00:06:18,180  -->  00:06:23,640
If you've ever configured outlook or Thunderbird or some other email client to connect to maybe your

93

00:06:23,640  -->  00:06:27,820
ISP email address you might have gone through a step like this.

94

00:06:27,960  -->  00:06:36,510
So every ISP or e-mail service has their own domain name for for some T-P servers usually just be whatever

95

00:06:36,510  -->  00:06:38,030
their name is dotcom.

96

00:06:38,100  -->  00:06:40,970
But with SMPTE dot in front of it.

97

00:06:41,070  -->  00:06:49,580
So on table 16:1 you can see here's a bunch of very popular e-mail providers such as e-mail or Outlook

98

00:06:49,580  -->  00:06:50,580
or Yahoo.

99

00:06:50,730  -->  00:06:56,450
And here's the S.M. T.P. server domain names in C for AT&T and Verizon.

100

00:06:56,580  -->  00:06:59,040
They use port for 6:5.

101

00:06:59,670  -->  00:07:05,060
So you would enter the integer 4 6 5 here for those next we call the hello method.

102

00:07:05,070  -->  00:07:11,150
Just to connect to that as some server then we just call start tell us to begin encryption.

103

00:07:11,250  -->  00:07:16,560
Most modern email servers will require that you actually start encryption before you can actually log

104

00:07:16,560  -->  00:07:17,170
in.

105

00:07:17,250  -->  00:07:23,390
And then next you'll call the log in method and pass it your your username and password as strings.

106

00:07:23,670  -->  00:07:26,600
And then finally Sendmail will actually send out the emails.

107

00:07:26,640  -->  00:07:31,930
And then when you're all done you can call the quit method to disconnect.

108

00:07:31,950  -->  00:07:37,320
One thing to keep in mind if you're using Gmail Google has a thing called application specific passwords

109

00:07:37,320  -->  00:07:37,800
.

110

00:07:38,010  -->  00:07:42,690
That means that your typical Google account password isn't going to be the one that you log in with

111

00:07:42,690  -->  00:07:43,620
here.

112

00:07:43,650  -->  00:07:45,390
In fact you have to go ahead and

113

00:07:48,690  -->  00:07:52,830
look up Google Apps specific password

114

00:07:56,550  -->  00:08:00,470
and you can find details for generating an application specific password.

115

00:08:00,510  -->  00:08:07,230
This is so that you can create a password for say Python programs or for your phone or if your TV logs

116

00:08:07,230  -->  00:08:09,250
into your email account or whatever.

117

00:08:09,390  -->  00:08:14,910
And that way if you have to change one of the passwords you won't have to change your password on multiple

118

00:08:14,910  -->  00:08:21,150
devices because each device will have its own password and then calling the sendmail method will actually

119

00:08:21,150  -->  00:08:22,400
send out the e-mail.

120

00:08:22,410  -->  00:08:27,270
Now note that a lot of e-mail providers in order to prevent people from you know writing Python scripts

121

00:08:27,270  -->  00:08:34,590
that could spam millions of people a day will usually limit you at I think Gmail has a limit of about

122

00:08:34,590  -->  00:08:37,130
100 or 150 emails per day.

123

00:08:37,140  -->  00:08:40,580
Other providers might have a limit of five hundred or a thousand per day.

124

00:08:40,650  -->  00:08:45,450
Basically if you have to e-mail out thousands of e-mails every day you should probably look into an

125

00:08:45,450  -->  00:08:49,460
actual commercial mailing list service.

126

00:08:49,560  -->  00:08:50,970
So that's the entire process.

127

00:08:50,970  -->  00:08:54,620
To recap first you just import SMTP lib.

128

00:08:54,750  -->  00:09:02,010
Then you call the S-M T-P function passing it the domain name of your e-mail providers SMPTE server

129

00:09:02,030  -->  00:09:02,460
.

130

00:09:02,970  -->  00:09:10,920
Then call a low then call start T.L. less than call log in with your password and username and then

131

00:09:10,920  -->  00:09:17,850
call Sendmail with the from 2 and body strings of the e-mail.

132

00:09:17,850  -->  00:09:22,770
Remember that in order to have the subject you need to have subject colon space then the subject line

133

00:09:22,950  -->  00:09:27,060
followed by two new line characters before the main part of the email is sent
