So the task was to send a random greating to a person on his/her Birthday. I picked the extrahard challenge cause this way without any tips it is much more interesting and also easierin a way cause I am free to use any approach that I want (don't get me wrong you also need to follow directions from time to time cause you also need to learn how to write readable and well-structured code where everything makes sense). From the start I had three documents with a greating in each of them
letter_1.txt
letter_2.txt
letter_3.txt
and also, I have the the birthdays.csv file with all the the names emails and birthdays
name,email,year,month,day
John,email1@email.com,2000,6,1
Jane,email2@email.com,2000,12,1
so, here we go: first, understanding that I will need some sort of if statement to compare current date to the birth date from the birthdays.csv, I use the datetimee module to get the current date. Keep in mind that datetime type is a datetime.date so for my approach I will need to convert it to a string
now = dt.datetime.now()
today = str(f"{now.year}-{now.month}-{now.day}")
#print(today) sorry, again my debugging
Also, I created the email and password variables that are holding the email of a sender and a password.
my_email = "email@outlook.com"
password = "password"
After that I read and retrieve the data I need out of the data files. Since, the birthdays.csv is a .csv I use the pandas module:
data = pandas.read_csv("birthdays.csv")
birth_dict = data.to_dict(orient="records")
For the three letters documents I use regular with
to read data and merge them into one list so it will be easier to work with it:
with open("letter_templates/letter_1.txt", "r") as letter_1,\
open("letter_templates/letter_2.txt", "r") as letter_2,\
open("letter_templates/letter_3.txt", "r") as letter_3:
l_1 = letter_1.read()
l_2 = letter_2.read()
l_3 = letter_3.read()
letters = [l_1, l_2, l_3]
After that I check if the current date is equal to the birthday date (remember previously I converted it to a string) and to do this I will need to access the items inside of the birth_dict
dictionary and extract the day and month:
for item in birth_dict:
birthday = f"2022-{item['month']}-{item['day']}"
also, extract name
and reciever_email
:
name = item["name"]
receiver_email = item["email"]
now I can finnaly compare two dates, remember how I converted the todays' date into a string. And if it is True than I modify letters replacing the [NAME] to the name of a person and finnaly sending the modified email:
if birthday == today:
letters = [letter.replace("[NAME]", name) for letter in letters]
send_email()
Finally I created the send_email
function and put it before the reading the letters documents:
def send_email():
#I connect to the corresponding email server
with smtplib.SMTP("outlook.office365.com", 587) as connection:
#secure the connection
connection.starttls()
#login to the service
connection.login(user=my_email, password=password)
#and finally send the email
connection.sendmail(from_addr=my_email, to_addrs=receiver_email,
msg=f"Subject: Birthday greating\n\n{random.choice(letters)}")