-
Notifications
You must be signed in to change notification settings - Fork 1
/
email_grades.py
executable file
·64 lines (47 loc) · 1.63 KB
/
email_grades.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
#!/usr/bin/env python3
import csv
import getpass
import smtplib
import ssl
import sys
import config
from email.message import EmailMessage
def load_grades():
if not config.INDIVIDUAL_GRADES_PATH.is_file():
sys.exit('Error: individual grades file does not exist')
with open(config.INDIVIDUAL_GRADES_PATH, newline='') as csv_file:
reader = csv.DictReader(csv_file)
return list(reader)
def send_emails(username, password, grades):
context = ssl.create_default_context()
with smtplib.SMTP(config.SMTP_SERVER, config.SMTP_PORT) as server:
# Login
server.ehlo()
server.starttls(context=context)
server.ehlo()
server.login(username, password)
# Send emails
for grade in grades:
print(f'Sending email to {grade["Email"]}')
message = EmailMessage()
message.set_content(
config.EMAIL_CONTENT.format(
first_name=grade['FirstName'],
id=grade['ID'], team=grade['Team'],
grade=grade['Grade'],
points_total=config.POINTS_TOTAL,
feedback=grade['Feedback']
)
)
message['Subject'] = config.EMAIL_SUBJECT
message['From'] = config.SENDER_EMAIL
message['To'] = grade['Email']
server.send_message(message)
def main():
grades = load_grades()
username = input('Username: ')
password = getpass.getpass()
send_emails(username, password, grades)
if __name__ == '__main__':
main()
# Made by Misha Krieger-Raynauld and Simon Gauvin