1
00:00:00,270 --> 00:00:04,740
Now let's put what you've learned into practice by creating a Monday

2
00:00:04,740 --> 00:00:08,700
motivational quote email application. To get warmed up,

3
00:00:08,850 --> 00:00:13,850
we're going to create a application that looks at the current date and time

4
00:00:15,270 --> 00:00:19,260
and if it happens to be a particular day of the week,

5
00:00:19,650 --> 00:00:21,360
let's say a Tuesday,

6
00:00:21,510 --> 00:00:26,370
then we're going to email ourselves a motivational quote.

7
00:00:27,240 --> 00:00:32,240
So I found 101 Monday motivational !uotes from a place called the positivity

8
00:00:33,090 --> 00:00:35,910
blog and I'll link to this as well.

9
00:00:36,240 --> 00:00:41,190
But the idea is that we all need a little bit of boost on Monday mornings and

10
00:00:41,190 --> 00:00:45,510
getting an email from Albert Einstein might just be the little nudge that we

11
00:00:45,510 --> 00:00:48,870
need in order to push us onto a successful week.

12
00:00:49,350 --> 00:00:50,550
So that's the idea.

13
00:00:51,000 --> 00:00:55,590
And if you take a look in the starting project that you've already downloaded

14
00:00:55,590 --> 00:00:58,980
and opened up, that should be a quotes.txt file.

15
00:00:59,580 --> 00:01:02,010
And here on each line,

16
00:01:02,070 --> 00:01:05,880
I've placed the quote from the positivity blog,

17
00:01:06,270 --> 00:01:10,950
and it's got the quote and person on the same line.

18
00:01:11,580 --> 00:01:16,470
So you can use what you've learned before where we went into how to read files,

19
00:01:16,470 --> 00:01:17,430
how to open files,

20
00:01:17,730 --> 00:01:22,730
how to create a list of all of the lines inside a particular text file.

21
00:01:25,410 --> 00:01:30,410
And you can use that along with what you've learned about sending emails to

22
00:01:30,750 --> 00:01:35,750
check and make sure that the day of the week is your current day of the week

23
00:01:36,420 --> 00:01:40,440
because obviously you need to test this. Instead of setting it to check for

24
00:01:40,440 --> 00:01:43,770
Monday, I want you to check for the current day that you are on.

25
00:01:44,190 --> 00:01:47,580
So if it happens to be Tuesday, then check for Tuesday, if it Saturday,

26
00:01:47,580 --> 00:01:51,600
then check for a Saturday and I want your Python code to make that check

27
00:01:51,990 --> 00:01:54,840
and if it matches today's weekday,

28
00:01:55,170 --> 00:01:59,940
then to send an email with a random quote from the quotes

29
00:01:59,940 --> 00:02:02,010
.txt. That's the goal.

30
00:02:02,340 --> 00:02:05,100
So I want you to pause the video and give it a go.

31
00:02:11,450 --> 00:02:11,900
Yeah.

32
00:02:11,900 --> 00:02:15,170
All right. So in order to complete this mini project,

33
00:02:15,200 --> 00:02:19,910
we first have to import those two modules we learned about; smtplib

34
00:02:20,210 --> 00:02:23,510
and also the datetime module. And again,

35
00:02:23,540 --> 00:02:27,950
I'm going to import it as dt, so give it an alias. Next

36
00:02:28,010 --> 00:02:32,750
we're going to tap into the dt module and we're going to get today's date and

37
00:02:32,750 --> 00:02:35,750
time. So we're going to use this now method.

38
00:02:36,380 --> 00:02:38,540
If now is equal to this,

39
00:02:38,930 --> 00:02:43,930
then today in terms of the weekday is equal to now.weekday.

40
00:02:45,830 --> 00:02:49,190
And once we've gotten hold of that, then we can check well,

41
00:02:49,220 --> 00:02:53,000
if the weekday is equal to say Monday

42
00:02:53,060 --> 00:02:54,500
which would be zero,

43
00:02:55,040 --> 00:03:00,040
then that means we're going to activate the rest of our code and send ourselves

44
00:03:00,190 --> 00:03:03,820
the email. But of course, in order to test our code,

45
00:03:04,150 --> 00:03:08,920
unless you have the patience to wait until next Monday and test your code once

46
00:03:08,920 --> 00:03:09,730
every week,

47
00:03:09,730 --> 00:03:14,200
then we probably want to set this to equal to the weekday that we're on

48
00:03:14,200 --> 00:03:18,700
today. So in my case, I'm on Tuesday so I'm going to set that to equal to 1,

49
00:03:19,000 --> 00:03:22,390
at least while I'm testing my code. And once I'm done with everything,

50
00:03:22,420 --> 00:03:24,820
I can reset that to 0 for Monday.

51
00:03:25,750 --> 00:03:28,540
If the weekday is equal to 1,

52
00:03:29,020 --> 00:03:32,260
then we're going to initiate our email sending mission.

53
00:03:32,860 --> 00:03:36,010
But first we have to figure out what we're going to send.

54
00:03:36,520 --> 00:03:41,520
So we're going to open up our quotes.txt and I'm going to use the with

55
00:03:42,310 --> 00:03:47,310
and then I'm going to use the open method to open the quotes.txt file that

56
00:03:48,550 --> 00:03:49,510
I've got right here.

57
00:03:49,900 --> 00:03:54,900
And I'm going to call this the quote_file. Iside this quote_file

58
00:03:55,600 --> 00:03:59,500
I've got a bunch of quotes, one on each line.

59
00:03:59,920 --> 00:04:04,920
So we can use the method called readlines in order to get a list of all of the

60
00:04:07,630 --> 00:04:11,380
lines in this quote_file and I'm gonna save it into all_quotes.

61
00:04:12,280 --> 00:04:17,279
Now we can import the random module and we can tap into a random quote.

62
00:04:20,200 --> 00:04:25,200
So we'll use random.choice and we'll get a random quote from this list of

63
00:04:26,890 --> 00:04:27,723
quotes.

64
00:04:28,840 --> 00:04:32,800
Now that we've created our quote inside this with block,

65
00:04:33,040 --> 00:04:37,360
we can actually go outside of the with block and we can still access our 

66
00:04:37,360 --> 00:04:40,630
quote. So if I go ahead and print our quote,

67
00:04:40,870 --> 00:04:44,590
you can see we've picked out a random quote. If you want to make an easy job

68
00:04:44,590 --> 00:04:48,730
seem mighty hard just keep putting off doing it by Olin Miller.

69
00:04:49,300 --> 00:04:54,300
And this is what we're going to try and send to ourselves using the smtplib.

70
00:04:55,120 --> 00:05:00,040
Again, inside the same if statement, we're going to create a new connection

71
00:05:00,460 --> 00:05:05,460
which is created from the smtplib and the SMTP class.

72
00:05:06,160 --> 00:05:10,390
Now, once we create this class, we have to provide a host.

73
00:05:10,630 --> 00:05:14,800
So that's the URL of the email server that we're going to use.

74
00:05:15,370 --> 00:05:19,870
Again, I'm going to use my email as my testing Gmail

75
00:05:20,230 --> 00:05:25,230
that was appbreweryinfo@gmail.com.

76
00:05:26,080 --> 00:05:31,080
And then I'm going to put in my password as well as a constant

77
00:05:31,720 --> 00:05:35,770
so this was abcd1234, and then brackets.

78
00:05:36,550 --> 00:05:41,550
And then we can add our SMTP URL. Because we know that we're sending it from the

79
00:05:42,820 --> 00:05:44,200
Gmail email service

80
00:05:44,530 --> 00:05:49,530
then I'm going to tap into the Gmail SMTP server so that's smtp.gmail.com.

81
00:05:53,230 --> 00:05:56,200
And instead of saving this as a connection,

82
00:05:56,260 --> 00:06:01,260
I'm going to use the width method with SMTP as the connection.

83
00:06:03,230 --> 00:06:07,900
Then using this connection, I'm going to firstly secure it.

84
00:06:08,230 --> 00:06:11,050
So I'm going to starttls

85
00:06:11,740 --> 00:06:15,640
and then I'm going to get this connection to log me in.

86
00:06:16,180 --> 00:06:21,180
So the username is my email and the password is my password.

87
00:06:23,830 --> 00:06:26,560
Once I've logged in to this connection,

88
00:06:26,590 --> 00:06:31,150
then I'm going to finally send my email. Now, in this case,

89
00:06:31,180 --> 00:06:34,570
the from address is going to be my email

90
00:06:35,050 --> 00:06:38,350
and the to address is also my email.

91
00:06:38,350 --> 00:06:41,230
I'm just sending an email from myself to myself,

92
00:06:41,590 --> 00:06:44,770
but it's going to have a different piece of content every time it sends it.

93
00:06:45,370 --> 00:06:49,690
And that message is going to contain that content.

94
00:06:50,200 --> 00:06:53,770
So I'm going to change this into an f-string because firstly,

95
00:06:53,770 --> 00:06:58,570
I want to have a subject which I'll call Monday Motivation

96
00:06:59,080 --> 00:07:04,080
and then we know that we can add the content by adding two line breaks.

97
00:07:05,260 --> 00:07:09,010
So let's format this so that it's easier for you to see.

98
00:07:10,570 --> 00:07:13,270
And I'm going to put it on two separate lines like this.

99
00:07:13,870 --> 00:07:17,260
Now after these two new lines comes my content

100
00:07:17,560 --> 00:07:20,200
which is basically going to be my f-string

101
00:07:20,240 --> 00:07:23,140
that's going to insert my quote right here.

102
00:07:23,800 --> 00:07:28,800
So now with all of that done and making sure that our weekday is set as the same

103
00:07:29,230 --> 00:07:31,420
day as today, so weekday ==

104
00:07:31,450 --> 00:07:35,440
1, that is Tuesday. weekday == 0 means it's Monday.

105
00:07:35,860 --> 00:07:39,490
Then we can be sort of sure that this code is actually going to run.

106
00:07:40,390 --> 00:07:42,700
So now if we run our code,

107
00:07:42,850 --> 00:07:47,850
you can see the quote being printed out here by Bertrand Russell.

108
00:07:48,490 --> 00:07:50,740
And if we go to our email,

109
00:07:50,770 --> 00:07:54,520
you can see that email from ourselves. Monday motivation.

110
00:07:54,910 --> 00:07:58,360
One of the symptoms of an approaching nervous breakdown is the belief that one's

111
00:07:58,360 --> 00:07:59,950
work is terribly important.

112
00:08:00,310 --> 00:08:04,390
So I guess today's motivation is to not take ourselves too seriously.

113
00:08:05,110 --> 00:08:08,650
But this is essentially what we're looking to achieve.

114
00:08:09,190 --> 00:08:12,220
So hopefully you managed to get that working. If not,

115
00:08:12,280 --> 00:08:14,350
you can take a look at my finished code

116
00:08:14,590 --> 00:08:18,040
including all of the previous actions on smtplib

117
00:08:18,070 --> 00:08:19,990
sending the email as well as date

118
00:08:19,990 --> 00:08:24,990
time in the day 32 ending code that you'll find on the course resources page.

119
00:08:26,920 --> 00:08:30,070
Now we're pretty much ready to tackle our final project,

120
00:08:30,160 --> 00:08:34,900
which is the automated birthday wisher. So for that,

121
00:08:34,990 --> 00:08:36,700
and more, I'll see you on the next lesson.

