Repository navigation
Expand file tree
/
Copy pathemail-test.py
More file actions
executable file
·99 lines (78 loc) · 2.41 KB
/
Copy pathemail-test.py
File metadata and controls
executable file
·99 lines (78 loc) · 2.41 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!venv/bin/python3
from google import genai
from google.api_core import retry
from google.genai.types import GenerateContentConfig
from typing import Literal, Optional
import dotenv
import os
import psycopg2
from psycopg2 import sql
from psycopg2.extras import execute_values
from sys import argv, stdin
import subprocess
import smtplib
import ssl
import json
from datetime import datetime
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from codeabode_model import *
is_retriable = lambda e: (isinstance(e, genai.errors.APIError) and e.code in {429, 503})
genai.models.Models.generate_content = retry.Retry(
predicate=is_retriable)(genai.models.Models.generate_content)
dotenv.load_dotenv(override=True)
# connect to the database and upload for a student
conn = psycopg2.connect(
os.getenv("DB_URL")
)
cur = conn.cursor()
print("Done. Sending email to student's accounts...")
cur.execute(
"""
SELECT account_id
FROM students
WHERE id = %s
""", (1,))
account_id_array = cur.fetchone()[0]
print(account_id_array)
if account_id_array:
cur.execute(
"""
SELECT name, email
FROM accounts
WHERE id = ANY(%s)
""",
(account_id_array,)
)
accounts = cur.fetchall()
print(accounts)
email = os.getenv("EMAIL_ADDRESS")
password = os.getenv("EMAIL_PASSWORD")
for account in accounts:
if account[1]:
msg = MIMEMultipart()
msg['From'] = f"Codeabode <{email}>"
msg['To'] = f"{account[0]} <{account[1]}>"
msg['Subject'] = "Assignment Uploaded for {current_class[10]}"
# Create the email body
body = """
Hi {account[0]},
Your son/daughter completed the {current_class[3]} class. Homework is available on the dashboard.
Class info:
Methods taught: {response.parsed.taught_methods}
What needs practice: {response.parsed.needs_practice}
Class Description: {current_class[4]}
Methods intended to be taught: {current_class[5]}
Stretch Methods: {current_class[6]}
Best,
Om
"""
msg.attach(MIMEText(body, 'plain'))
with smtplib.SMTP("smtp.gmail.com", 587) as server:
server.starttls(context=ssl.create_default_context())
server.login(email, password)
server.send_message(msg)
print("Email sent to student accounts.")
conn.commit()
cur.close()
conn.close()