In this session, we're going to build a script which sends an email at a certain timestamp every day.
Let's see how we can do that.
Since this script will be similar in structure with the send email periodically repl, which sends
an email every 60 seconds, I am going to fork this so duplicate it, let's say, every day.
This is a new repl with the same code as before, and now all we need to do is enter an if conditional
here because currently this loop is running over and over again without any limits.
We want to enter a condition.
If something, then indent this under if if so that this code is only executed when that's something
is satisfied.
That condition and what I'm thinking of here is to check the current timestamp.
So let's say we want to send an email at 13:10 at this time every day.
So we want to check if the time is 13:10, and we want to do that checking every minute.
So the loop will run every minute and it will check if the time is 13 10.
Now to deal with times we want to use from datetime import datetime as dt just so that we
don't have to use this long name, we use dt.
So that's basically a variable that represents this.
Therefore, now we can say if dt.now
dot hour is equal to 13 because what this outputs is.
I can show that to you.
So that can be run in there and say dt.now you get the current timestamp, but dt.now.hour
you only get the hour, which is 13.
So 1:00 p.m. So if the time, if the hour is 13, then dt.now.minute... But we are repeating
ourselves here, so we are executing dt.now two times.
Therefore, let's improve that and let's say now is dt.now.
We store that timestamp in a variable.
Then we say now.hour.
Then we extract the hour out of the timestamp.
So this timestamp again is like this one here.
So if now.hour is equal to 13 and now.minute equal to... We said 10, or let's make it 15.
So I'm trying to find a time that is like a few minutes after now to be able to test my script.
Then we sent that email and we check every 60 seconds.
So it makes sense to have the interval
60 seconds because the accuracy we want is minute.
So we are checking hour and minute.
So in order to check every minute, so we want to run the loop every minute.
If we only wanted to send an email at one p.m., it doesn't matter what minutes, then you don't have
to do this.
You could just say like, that's and then here you say three thousand six hundred seconds, which means
one hour or so, every one hour you would be checking if that's how it is met or not.
So whatever you like, I'm using the longer version just for demonstration purposes.
And that's it.
I'm going to try and round the script now.
Time is 13:15 right now, so the script started to run.
And what will happen here is the loop will start.
It will record the current date time and van it will check if the hour is 13 and the minute is 15,
it will execute this block.
Otherwise, it's going to run the loop again here, so it will not execute this block here.
If the hour is met, this block will be executed and the loop will try to run again, check the time
and so on.
So I got this error, which says that I have no keyring.
So which means we didn't include a secret here, so password.
You can see that password is provided here, but it's not included as an environment variable in this
particular repl.
So the value was flask7app#.
And that symbol.
So this is the password for this Gmail account.
The sender Gmail account, so add new secret, don't forget, set the time to be 17 this time.
Yes, that makes sense and run.
And I got this email sent.
Let's check
if we got any email here, oh, it seems like this service has changed the email address, so I'm going
to copy that and paste it as a new receiver, paste it and set it to 18 time, stop and run again.
So we had to provided the wrong email address.
We have this "sent" again.
And let's check.
Yeah, now we have a new email.
And that is how to send an email at a certain time every day with Python.
Thank you, and I'll talk to you later.