How to send an automated Monday Motivation Email in Python?

Ayush Dixit
Nerd For Tech
Published in
3 min readDec 13, 2021

Monday can be a struggle to begin with. Monday is the most popular sick day. According to a 2009 study conducted in the UK, 35 percent of all sick leave is taken on Mondays. :P

A little morning motivation on monday can go a long way to get off your bed and jumpstart your week, inspiring you to start your week with confidence and energy.

Python to the rescue, with this project in python you can send a motivational mail every monday to yourself, a friend or a colleague.

Let’s jump right into it.

For our purpose we would be making use of three libraries in python namely smtplib, datetime and random.

We would also need a list of quotes which would be parsed later by our program. I found a ready to import quotes list here # https://gist.github.com/robatron/a66acc0eed3835119817

Step 1 : Importing Libraries

import smtplib
import datetime as dt
import random

Step 2 : Defining the login credentials where the mail will be sent from

my_email = "<your_email>"
password = "<your_password>"

Step 3 : Taking into account the weekday

To find which day it is of the week prsently. We can use the weekday() method of the datetime module by first getting the present datetime through “datetime.now()” method.

now = dt.datetime.now()
weekday = now.weekday()

Monday would be the first day of the week, therefore it would be initialised as 0 in the module.

Step 4 : Generating a Random Quote

if weekday == 0:
with open("quotes.txt") as file:
all_quotes = file.readlines()
quote = random.choice(all_quotes)

print(quote)

Quite simply, we check if the current weekday is monday, if so we proceed with reading the “quotes.txt” file and get a random quote storing it in the “quote” variable.

Step 5 : Sending the email with a motivational quote to myself

with smtplib.SMTP("smtp.gmail.com", 587, timeout=120) as connection:
connection.starttls()
connection.login(my_email, password)
connection.sendmail(
from_addr=my_email,
to_addrs=my_email,
msg=f"Subject:Monday Motivation\n\n{quote}")

We define smtplib.SMTP which takes the smtp host running the smtp host server. Which is “smtp.gmail.com”, a port number : 587 and timeout = 120. I have explicitly defined the last two parameters as the code was running into error and the interpreter was getting stuck on reading the smtp object.

“connection.starttls()” initates the starttls protocol. STARTTLS is an email protocol command that tells an email server that an email client, including an email client running in a web browser, wants to turn an existing insecure connection into a secure one.

Also, before compilation kindly make sure that you have permitted gmail to allow access from less secure apps. To check and allow access, kindly go here.

Finally, we have the output ready:

You may also find the email in your sent box. Get the complete code here.

Did you like my efforts? If Yes, please follow me to get my latest posts and updates or better still, buy me a coffee!☕

--

--

Ayush Dixit
Nerd For Tech

Hi, I’m a postgraduate from IIT-Indore(M.Tech). Specialization in Comm. Signal Processing and Machine Learning/AI. Presently working as an Engineer in Qualcomm.