-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
76 lines (62 loc) · 2.7 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import datetime as dt
import pandas as pd
import os
import random
import smtplib
class BirthDex:
"""Retrieves data from "birthdays.csv" file and sends out a personal birthday email if it's someone's birthday."""
def __init__(self, file_name):
super().__init__()
# Reads "birthdays.csv" as pandas Dataframe
self.sheet_data = pd.read_csv(file_name)
# Stores all name and email data from CSV file into a list
self.name_data = self.sheet_data["name"].tolist()
self.email_data = self.sheet_data["email"].tolist()
# Modifies birthdates to following format: MM/DD/YY
def birthday_data(self):
"""Returns a list[str] of all birthdays from the CSV file in the format of 'month/day'.'"""
birth_month = self.sheet_data["month"].tolist()
birth_day = self.sheet_data["day"].tolist()
bday_data = []
for x in range(len(birth_month)):
if birth_month[x] <= 9:
m = "0" + str(birth_month[x])
else:
m = str(birth_month[x])
bday_data.append(m + "/" + str(birth_day[x]))
return bday_data
check_birthdays = BirthDex("birthdays.csv")
names = check_birthdays.name_data
emails = check_birthdays.email_data
bdays = check_birthdays.birthday_data()
# Present date
get_date = dt.datetime.now()
today = get_date.strftime("%D") # MM/DD/YY
year = str(get_date.year - 2000)
# Checks if any birthdays match present date
for x in range(len(bdays)):
check = bdays[x] + "/" + year
# Open templates folder if match found
if check == today:
# Selects a random template from fodler
path = "mail_templates/"
template = random.choice(os.listdir(path))
pathway = path + template
# Opens the chosen template
with open(pathway, "r") as temp:
draft = temp.read()
print(draft)
# Replaces [NAME] with appropriate data from CSV file
with open(pathway, "w") as temp:
final_draft = draft.replace("[NAME]", names[x])
print(final_draft)
# Sender Info
sender = "sample1@gmail.com"
password = "abc123"
# Sends updated email draft to reciever
with smtplib.SMTP("smtp.gmail.com") as connection:
connection.starttls() # Secures connection to email server
connection.login(user=sender, password=password)
connection.send_message(from_addr=sender,
to_addrs=emails[x],
msg=f"Subject:Happy Birthday! :D\n\n{final_draft}.")