1
00:00:00,090 --> 00:00:10,500
In this session, we're going to build a script which sends an email at a certain timestamp every day.

2
00:00:11,140 --> 00:00:12,750
Let's see how we can do that.

3
00:00:13,800 --> 00:00:21,450
Since this script will be similar in structure with the send email periodically repl, which sends

4
00:00:21,450 --> 00:00:30,090
an email every 60 seconds, I am going to fork this so duplicate it, let's say, every day.

5
00:00:31,630 --> 00:00:41,220
This is a new repl with the same code as before, and now all we need to do is enter an if conditional

6
00:00:41,230 --> 00:00:49,270
here because currently this loop is running over and over again without any limits.

7
00:00:50,080 --> 00:00:52,030
We want to enter a condition.

8
00:00:52,660 --> 00:01:02,770
If something, then indent this under if if so that this code is only executed when that's something

9
00:01:03,280 --> 00:01:04,540
is satisfied.

10
00:01:04,569 --> 00:01:12,670
That condition and what I'm thinking of here is to check the current timestamp.

11
00:01:13,000 --> 00:01:21,550
So let's say we want to send an email at 13:10 at this time every day.

12
00:01:21,850 --> 00:01:30,820
So we want to check if the time is 13:10, and we want to do that checking every minute.

13
00:01:31,390 --> 00:01:38,940
So the loop will run every minute and it will check if the time is 13 10.

14
00:01:39,430 --> 00:01:49,540
Now to deal with times we want to use from datetime import datetime as dt just so that we

15
00:01:49,540 --> 00:01:53,010
don't have to use this long name, we use dt.

16
00:01:53,800 --> 00:01:57,940
So that's basically a variable that represents this.

17
00:01:58,000 --> 00:02:00,130
Therefore, now we can say if dt.now

18
00:02:00,520 --> 00:02:09,009
dot hour is equal to 13 because what this outputs is.

19
00:02:09,430 --> 00:02:11,260
I can show that to you.

20
00:02:11,560 --> 00:02:21,700
So that can be run in there and say dt.now you get the current timestamp, but dt.now.hour

21
00:02:21,700 --> 00:02:25,450
you only get the hour, which is 13.

22
00:02:25,870 --> 00:02:39,100
So 1:00 p.m. So if the time, if the hour is 13, then dt.now.minute... But we are repeating

23
00:02:39,310 --> 00:02:44,290
ourselves here, so we are executing dt.now two times.

24
00:02:44,320 --> 00:02:50,620
Therefore, let's improve that and let's say now is dt.now.

25
00:02:50,770 --> 00:02:53,400
We store that timestamp in a variable.

26
00:02:54,120 --> 00:02:55,690
Then we say now.hour.

27
00:02:56,320 --> 00:02:59,380
Then we extract the hour out of the timestamp.

28
00:02:59,440 --> 00:03:01,880
So this timestamp again is like this one here.

29
00:03:02,110 --> 00:03:14,700
So if now.hour is equal to 13 and now.minute equal to... We said 10, or let's make it 15.

30
00:03:15,190 --> 00:03:21,490
So I'm trying to find a time that is like a few minutes after now to be able to test my script.

31
00:03:21,760 --> 00:03:25,990
Then we sent that email and we check every 60 seconds.

32
00:03:26,200 --> 00:03:28,960
So it makes sense to have the interval

33
00:03:29,020 --> 00:03:34,930
60 seconds because the accuracy we want is minute.

34
00:03:35,110 --> 00:03:37,840
So we are checking hour and minute.

35
00:03:38,230 --> 00:03:42,610
So in order to check every minute, so we want to run the loop every minute.

36
00:03:42,910 --> 00:03:52,780
If we only wanted to send an email at one p.m., it doesn't matter what minutes, then you don't have

37
00:03:52,780 --> 00:03:53,470
to do this.

38
00:03:53,710 --> 00:03:59,920
You could just say like, that's and then here you say three thousand six hundred seconds, which means

39
00:03:59,920 --> 00:04:07,990
one hour or so, every one hour you would be checking if that's how it is met or not.

40
00:04:08,560 --> 00:04:15,790
So whatever you like, I'm using the longer version just for demonstration purposes.

41
00:04:16,300 --> 00:04:16,899
And that's it.

42
00:04:17,110 --> 00:04:19,480
I'm going to try and round the script now.

43
00:04:20,870 --> 00:04:27,770
Time is 13:15 right now, so the script started to run.

44
00:04:28,280 --> 00:04:32,460
And what will happen here is the loop will start.

45
00:04:32,480 --> 00:04:41,750
It will record the current date time and van it will check if the hour is 13 and the minute is 15,

46
00:04:42,050 --> 00:04:44,630
it will execute this block.

47
00:04:45,470 --> 00:04:53,570
Otherwise, it's going to run the loop again here, so it will not execute this block here.

48
00:04:54,440 --> 00:05:01,340
If the hour is met, this block will be executed and the loop will try to run again, check the time

49
00:05:01,340 --> 00:05:02,090
and so on.

50
00:05:02,750 --> 00:05:09,170
So I got this error, which says that I have no keyring.

51
00:05:09,230 --> 00:05:17,630
So which means we didn't include a secret here, so password.

52
00:05:18,380 --> 00:05:24,620
You can see that password is provided here, but it's not included as an environment variable in this

53
00:05:24,620 --> 00:05:25,820
particular repl.

54
00:05:27,550 --> 00:05:31,510
So the value was flask7app#.

55
00:05:32,930 --> 00:05:33,800
And that symbol.

56
00:05:34,490 --> 00:05:38,770
So this is the password for this Gmail account.

57
00:05:38,810 --> 00:05:47,350
The sender Gmail account, so add new secret, don't forget, set the time to be 17 this time.

58
00:05:47,360 --> 00:05:49,520
Yes, that makes sense and run.

59
00:05:55,410 --> 00:05:57,760
And I got this email sent.

60
00:05:58,890 --> 00:05:59,670
Let's check

61
00:06:00,820 --> 00:06:06,700
if we got any email here, oh, it seems like this service has changed the email address, so I'm going

62
00:06:06,700 --> 00:06:17,320
to copy that and paste it as a new receiver, paste it and set it to 18 time, stop and run again.

63
00:06:18,490 --> 00:06:20,830
So we had to provided the wrong email address.

64
00:06:21,190 --> 00:06:23,260
We have this "sent" again.

65
00:06:23,710 --> 00:06:25,090
And let's check.

66
00:06:26,110 --> 00:06:28,300
Yeah, now we have a new email.

67
00:06:30,240 --> 00:06:39,210
And that is how to send an email at a certain time every day with Python.

68
00:06:39,780 --> 00:06:41,400
Thank you, and I'll talk to you later.

