WEBVTT

0
00:00.270 --> 00:04.740
Now let's put what you've learned into practice by creating a Monday

1
00:04.740 --> 00:08.700
motivational quote email application. To get warmed up,

2
00:08.850 --> 00:13.850
we're going to create a application that looks at the current date and time

3
00:15.270 --> 00:19.260
and if it happens to be a particular day of the week,

4
00:19.650 --> 00:21.360
let's say a Tuesday,

5
00:21.510 --> 00:26.370
then we're going to email ourselves a motivational quote.

6
00:27.240 --> 00:32.240
So I found 101 Monday motivational !uotes from a place called the positivity

7
00:33.090 --> 00:35.910
blog and I'll link to this as well.

8
00:36.240 --> 00:41.190
But the idea is that we all need a little bit of boost on Monday mornings and

9
00:41.190 --> 00:45.510
getting an email from Albert Einstein might just be the little nudge that we

10
00:45.510 --> 00:48.870
need in order to push us onto a successful week.

11
00:49.350 --> 00:50.550
So that's the idea.

12
00:51.000 --> 00:55.590
And if you take a look in the starting project that you've already downloaded

13
00:55.590 --> 00:58.980
and opened up, that should be a quotes.txt file.

14
00:59.580 --> 01:02.010
And here on each line,

15
01:02.070 --> 01:05.880
I've placed the quote from the positivity blog,

16
01:06.270 --> 01:10.950
and it's got the quote and person on the same line.

17
01:11.580 --> 01:16.470
So you can use what you've learned before where we went into how to read files,

18
01:16.470 --> 01:17.430
how to open files,

19
01:17.730 --> 01:22.730
how to create a list of all of the lines inside a particular text file.

20
01:25.410 --> 01:30.410
And you can use that along with what you've learned about sending emails to

21
01:30.750 --> 01:35.750
check and make sure that the day of the week is your current day of the week

22
01:36.420 --> 01:40.440
because obviously you need to test this. Instead of setting it to check for

23
01:40.440 --> 01:43.770
Monday, I want you to check for the current day that you are on.

24
01:44.190 --> 01:47.580
So if it happens to be Tuesday, then check for Tuesday, if it Saturday,

25
01:47.580 --> 01:51.600
then check for a Saturday and I want your Python code to make that check

26
01:51.990 --> 01:54.840
and if it matches today's weekday,

27
01:55.170 --> 01:59.940
then to send an email with a random quote from the quotes

28
01:59.940 --> 02:02.010
.txt. That's the goal.

29
02:02.340 --> 02:05.100
So I want you to pause the video and give it a go.

30
02:11.450 --> 02:11.900
<v 1>Yeah.</v>

31
02:11.900 --> 02:15.170
<v 0>All right. So in order to complete this mini project,</v>

32
02:15.200 --> 02:19.910
we first have to import those two modules we learned about; smtplib

33
02:20.210 --> 02:23.510
and also the datetime module. And again,

34
02:23.540 --> 02:27.950
I'm going to import it as dt, so give it an alias. Next

35
02:28.010 --> 02:32.750
we're going to tap into the dt module and we're going to get today's date and

36
02:32.750 --> 02:35.750
time. So we're going to use this now method.

37
02:36.380 --> 02:38.540
If now is equal to this,

38
02:38.930 --> 02:43.930
then today in terms of the weekday is equal to now.weekday.

39
02:45.830 --> 02:49.190
And once we've gotten hold of that, then we can check well,

40
02:49.220 --> 02:53.000
if the weekday is equal to say Monday

41
02:53.060 --> 02:54.500
which would be zero,

42
02:55.040 --> 03:00.040
then that means we're going to activate the rest of our code and send ourselves

43
03:00.190 --> 03:03.820
the email. But of course, in order to test our code,

44
03:04.150 --> 03:08.920
unless you have the patience to wait until next Monday and test your code once

45
03:08.920 --> 03:09.730
every week,

46
03:09.730 --> 03:14.200
then we probably want to set this to equal to the weekday that we're on

47
03:14.200 --> 03:18.700
today. So in my case, I'm on Tuesday so I'm going to set that to equal to 1,

48
03:19.000 --> 03:22.390
at least while I'm testing my code. And once I'm done with everything,

49
03:22.420 --> 03:24.820
I can reset that to 0 for Monday.

50
03:25.750 --> 03:28.540
If the weekday is equal to 1,

51
03:29.020 --> 03:32.260
then we're going to initiate our email sending mission.

52
03:32.860 --> 03:36.010
But first we have to figure out what we're going to send.

53
03:36.520 --> 03:41.520
So we're going to open up our quotes.txt and I'm going to use the with

54
03:42.310 --> 03:47.310
and then I'm going to use the open method to open the quotes.txt file that

55
03:48.550 --> 03:49.510
I've got right here.

56
03:49.900 --> 03:54.900
And I'm going to call this the quote_file. Iside this quote_file

57
03:55.600 --> 03:59.500
I've got a bunch of quotes, one on each line.

58
03:59.920 --> 04:04.920
So we can use the method called readlines in order to get a list of all of the

59
04:07.630 --> 04:11.380
lines in this quote_file and I'm gonna save it into all_quotes.

60
04:12.280 --> 04:17.280
Now we can import the random module and we can tap into a random quote.

61
04:20.200 --> 04:25.200
So we'll use random.choice and we'll get a random quote from this list of

62
04:26.890 --> 04:27.723
quotes.

63
04:28.840 --> 04:32.800
Now that we've created our quote inside this with block,

64
04:33.040 --> 04:37.360
we can actually go outside of the with block and we can still access our 

65
04:37.360 --> 04:40.630
quote. So if I go ahead and print our quote,

66
04:40.870 --> 04:44.590
you can see we've picked out a random quote. If you want to make an easy job

67
04:44.590 --> 04:48.730
seem mighty hard just keep putting off doing it by Olin Miller.

68
04:49.300 --> 04:54.300
And this is what we're going to try and send to ourselves using the smtplib.

69
04:55.120 --> 05:00.040
Again, inside the same if statement, we're going to create a new connection

70
05:00.460 --> 05:05.460
which is created from the smtplib and the SMTP class.

71
05:06.160 --> 05:10.390
Now, once we create this class, we have to provide a host.

72
05:10.630 --> 05:14.800
So that's the URL of the email server that we're going to use.

73
05:15.370 --> 05:19.870
Again, I'm going to use my email as my testing Gmail

74
05:20.230 --> 05:25.230
that was appbreweryinfo@gmail.com.

75
05:26.080 --> 05:31.080
And then I'm going to put in my password as well as a constant

76
05:31.720 --> 05:35.770
so this was abcd1234, and then brackets.

77
05:36.550 --> 05:41.550
And then we can add our SMTP URL. Because we know that we're sending it from the

78
05:42.820 --> 05:44.200
Gmail email service

79
05:44.530 --> 05:49.530
then I'm going to tap into the Gmail SMTP server so that's smtp.gmail.com.

80
05:53.230 --> 05:56.200
And instead of saving this as a connection,

81
05:56.260 --> 06:01.260
I'm going to use the width method with SMTP as the connection.

82
06:03.230 --> 06:07.900
Then using this connection, I'm going to firstly secure it.

83
06:08.230 --> 06:11.050
So I'm going to starttls

84
06:11.740 --> 06:15.640
and then I'm going to get this connection to log me in.

85
06:16.180 --> 06:21.180
So the username is my email and the password is my password.

86
06:23.830 --> 06:26.560
Once I've logged in to this connection,

87
06:26.590 --> 06:31.150
then I'm going to finally send my email. Now, in this case,

88
06:31.180 --> 06:34.570
the from address is going to be my email

89
06:35.050 --> 06:38.350
and the to address is also my email.

90
06:38.350 --> 06:41.230
I'm just sending an email from myself to myself,

91
06:41.590 --> 06:44.770
but it's going to have a different piece of content every time it sends it.

92
06:45.370 --> 06:49.690
And that message is going to contain that content.

93
06:50.200 --> 06:53.770
So I'm going to change this into an f-string because firstly,

94
06:53.770 --> 06:58.570
I want to have a subject which I'll call Monday Motivation

95
06:59.080 --> 07:04.080
and then we know that we can add the content by adding two line breaks.

96
07:05.260 --> 07:09.010
So let's format this so that it's easier for you to see.

97
07:10.570 --> 07:13.270
And I'm going to put it on two separate lines like this.

98
07:13.870 --> 07:17.260
Now after these two new lines comes my content

99
07:17.560 --> 07:20.200
which is basically going to be my f-string

100
07:20.240 --> 07:23.140
that's going to insert my quote right here.

101
07:23.800 --> 07:28.800
So now with all of that done and making sure that our weekday is set as the same

102
07:29.230 --> 07:31.420
day as today, so weekday ==

103
07:31.450 --> 07:35.440
1, that is Tuesday. weekday == 0 means it's Monday.

104
07:35.860 --> 07:39.490
Then we can be sort of sure that this code is actually going to run.

105
07:40.390 --> 07:42.700
So now if we run our code,

106
07:42.850 --> 07:47.850
you can see the quote being printed out here by Bertrand Russell.

107
07:48.490 --> 07:50.740
And if we go to our email,

108
07:50.770 --> 07:54.520
you can see that email from ourselves. Monday motivation.

109
07:54.910 --> 07:58.360
One of the symptoms of an approaching nervous breakdown is the belief that one's

110
07:58.360 --> 07:59.950
work is terribly important.

111
08:00.310 --> 08:04.390
So I guess today's motivation is to not take ourselves too seriously.

112
08:05.110 --> 08:08.650
But this is essentially what we're looking to achieve.

113
08:09.190 --> 08:12.220
So hopefully you managed to get that working. If not,

114
08:12.280 --> 08:14.350
you can take a look at my finished code

115
08:14.590 --> 08:18.040
including all of the previous actions on smtplib

116
08:18.070 --> 08:19.990
sending the email as well as date

117
08:19.990 --> 08:24.990
time in the day 32 ending code that you'll find on the course resources page.

118
08:26.920 --> 08:30.070
Now we're pretty much ready to tackle our final project,

119
08:30.160 --> 08:34.900
which is the automated birthday wisher. So for that,

120
08:34.990 --> 08:36.700
and more, I'll see you on the next lesson.