-
Notifications
You must be signed in to change notification settings - Fork 0
/
emails.py
68 lines (58 loc) · 2.12 KB
/
emails.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
#!/usr/bin/env python3
from jinja2 import Environment, FileSystemLoader
import os
import sh
from termcolor import cprint
import time
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
def write_emails(data, assignment_name, total_points, regrade_date, autograder_link, dest, ceil_func):
env = Environment(loader=FileSystemLoader(os.path.dirname(__file__) + '/email_templates'))
template = env.get_template('c4cs.html')
sh.mkdir('-p', dest)
with sh.pushd(dest):
for uniq, submission in data.items():
with open(uniq, 'w') as f:
f.write(template.render(
uniq=uniq,
assignment_name=assignment_name,
total_possible=total_points,
regrade_date=regrade_date,
autograder_link=autograder_link,
raw_score=submission['score'],
final_score=ceil_func(submission['score']),
testcases=submission['test_case_results'],
))
def send_email(uniqname, body, SUBJECT, CC):
FROM = 'c4cs-staff@umich.edu'
TO = uniqname + '@umich.edu'
REPLY_TO_ADDRESS = 'c4cs-staff@umich.edu'
encoding = 'html'
cprint('Sending {}'.format(TO), 'green')
msg = MIMEMultipart()
msg['Subject'] = SUBJECT
msg['From'] = FROM
msg['To'] = TO
msg['CC'] = ','.join(CC)
msg.attach(MIMEText(body, encoding))
msg.add_header('reply-to', REPLY_TO_ADDRESS)
global sm
send_to = [TO,] + CC
sm.sendmail(FROM, send_to, msg.as_string())
def send_emails(loc, subject, cc, smtp):
global sm
sm = smtplib.SMTP_SSL(host=smtp['host'])
sm.login(smtp['user'], smtp['pass'])
with sh.pushd(loc):
num_sent = 0;
for uniq in os.listdir():
with open(uniq) as f:
send_email(uniq, f.read(), subject, cc)
num_sent += 1
# Be kind to the smtp server
if num_sent % 50 == 0:
time.sleep(300)
else:
time.sleep(1)