-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_reminders.py
More file actions
68 lines (49 loc) · 1.89 KB
/
Copy pathsend_reminders.py
File metadata and controls
68 lines (49 loc) · 1.89 KB
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
from retrieval import fetch_deadlines
from dotenv import load_dotenv
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from datetime import datetime, timedelta
load_dotenv()
deadlines = fetch_deadlines()
# Gmail credentials
EMAIL = os.getenv("SENDER_EMAIL")
PASSWORD = os.getenv("APP_PASSWORD") # Google App Password
TO_EMAIL = os.getenv("RECEIVER_EMAIL")
REMINDER_HOURS = [7, 12, 18, 20, 22] # i wanna send reminders at these particular hours(24 hr format)
def send_email(subject, body, to_email=TO_EMAIL):
msg = MIMEMultipart()
msg["From"] = EMAIL
msg["To"] = to_email
msg["Subject"] = subject
msg.attach(MIMEText(body, "plain"))
try:
with smtplib.SMTP("smtp.gmail.com", 587) as server:
server.starttls()
server.login(EMAIL, PASSWORD)
server.sendmail(EMAIL, to_email, msg.as_string())
print(f"Email sent: {subject}")
except Exception as e:
print(f"Error sending email: {e}")
def schedule_reminders(deadlines):
now = datetime.now()
today = now.date()
tomorrow = today + timedelta(days=1)
"""
The script runs daily at 7 AM and checks whether if there are deadlines tom and if yes
then it starts sending reminders at specified time
"""
for task in deadlines:
due_date = datetime.strptime(task["Due_date"], "%A, %B %d, %Y %I:%M %p").date()
if due_date == tomorrow:
subject = f"Reminder: {task['Name']} due tomorrow!"
body = (
f"You have a task due tom but dont be lazy and procrastinate till the deadline , finish it RIGHT NOW!!!\n\n"
f"Task: {task['Name']}\n"
f"Deadline: {task['Due_date']}\n\n"
)
send_email(subject, body)
if __name__ == "__main__":
schedule_reminders(deadlines)
print("Code ran successfully~")