Now let's put what you've learned into practice by creating a Monday
motivational quote email application. To get warmed up,
we're going to create a application that looks at the current date and time
and if it happens to be a particular day of the week,
let's say a Tuesday,
then we're going to email ourselves a motivational quote.
So I found 101 Monday motivational !uotes from a place called the positivity
blog and I'll link to this as well.
But the idea is that we all need a little bit of boost on Monday mornings and
getting an email from Albert Einstein might just be the little nudge that we
need in order to push us onto a successful week.
So that's the idea.
And if you take a look in the starting project that you've already downloaded
and opened up, that should be a quotes.txt file.
And here on each line,
I've placed the quote from the positivity blog,
and it's got the quote and person on the same line.
So you can use what you've learned before where we went into how to read files,
how to open files,
how to create a list of all of the lines inside a particular text file.
And you can use that along with what you've learned about sending emails to
check and make sure that the day of the week is your current day of the week
because obviously you need to test this. Instead of setting it to check for
Monday, I want you to check for the current day that you are on.
So if it happens to be Tuesday, then check for Tuesday, if it Saturday,
then check for a Saturday and I want your Python code to make that check
and if it matches today's weekday,
then to send an email with a random quote from the quotes
.txt. That's the goal.
So I want you to pause the video and give it a go.
Yeah.
All right. So in order to complete this mini project,
we first have to import those two modules we learned about; smtplib
and also the datetime module. And again,
I'm going to import it as dt, so give it an alias. Next
we're going to tap into the dt module and we're going to get today's date and
time. So we're going to use this now method.
If now is equal to this,
then today in terms of the weekday is equal to now.weekday.
And once we've gotten hold of that, then we can check well,
if the weekday is equal to say Monday
which would be zero,
then that means we're going to activate the rest of our code and send ourselves
the email. But of course, in order to test our code,
unless you have the patience to wait until next Monday and test your code once
every week,
then we probably want to set this to equal to the weekday that we're on
today. So in my case, I'm on Tuesday so I'm going to set that to equal to 1,
at least while I'm testing my code. And once I'm done with everything,
I can reset that to 0 for Monday.
If the weekday is equal to 1,
then we're going to initiate our email sending mission.
But first we have to figure out what we're going to send.
So we're going to open up our quotes.txt and I'm going to use the with
and then I'm going to use the open method to open the quotes.txt file that
I've got right here.
And I'm going to call this the quote_file. Iside this quote_file
I've got a bunch of quotes, one on each line.
So we can use the method called readlines in order to get a list of all of the
lines in this quote_file and I'm gonna save it into all_quotes.
Now we can import the random module and we can tap into a random quote.
So we'll use random.choice and we'll get a random quote from this list of
quotes.
Now that we've created our quote inside this with block,
we can actually go outside of the with block and we can still access our
quote. So if I go ahead and print our quote,
you can see we've picked out a random quote. If you want to make an easy job
seem mighty hard just keep putting off doing it by Olin Miller.
And this is what we're going to try and send to ourselves using the smtplib.
Again, inside the same if statement, we're going to create a new connection
which is created from the smtplib and the SMTP class.
Now, once we create this class, we have to provide a host.
So that's the URL of the email server that we're going to use.
Again, I'm going to use my email as my testing Gmail
that was appbreweryinfo@gmail.com.
And then I'm going to put in my password as well as a constant
so this was abcd1234, and then brackets.
And then we can add our SMTP URL. Because we know that we're sending it from the
Gmail email service
then I'm going to tap into the Gmail SMTP server so that's smtp.gmail.com.
And instead of saving this as a connection,
I'm going to use the width method with SMTP as the connection.
Then using this connection, I'm going to firstly secure it.
So I'm going to starttls
and then I'm going to get this connection to log me in.
So the username is my email and the password is my password.
Once I've logged in to this connection,
then I'm going to finally send my email. Now, in this case,
the from address is going to be my email
and the to address is also my email.
I'm just sending an email from myself to myself,
but it's going to have a different piece of content every time it sends it.
And that message is going to contain that content.
So I'm going to change this into an f-string because firstly,
I want to have a subject which I'll call Monday Motivation
and then we know that we can add the content by adding two line breaks.
So let's format this so that it's easier for you to see.
And I'm going to put it on two separate lines like this.
Now after these two new lines comes my content
which is basically going to be my f-string
that's going to insert my quote right here.
So now with all of that done and making sure that our weekday is set as the same
day as today, so weekday ==
1, that is Tuesday. weekday == 0 means it's Monday.
Then we can be sort of sure that this code is actually going to run.
So now if we run our code,
you can see the quote being printed out here by Bertrand Russell.
And if we go to our email,
you can see that email from ourselves. Monday motivation.
One of the symptoms of an approaching nervous breakdown is the belief that one's
work is terribly important.
So I guess today's motivation is to not take ourselves too seriously.
But this is essentially what we're looking to achieve.
So hopefully you managed to get that working. If not,
you can take a look at my finished code
including all of the previous actions on smtplib
sending the email as well as date
time in the day 32 ending code that you'll find on the course resources page.
Now we're pretty much ready to tackle our final project,
which is the automated birthday wisher. So for that,
and more, I'll see you on the next lesson.