forked from ShanghaitechGeekPie/Grade-Sender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradeSender.py
118 lines (109 loc) · 4.25 KB
/
gradeSender.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import csv
import smtplib
from email.mime.text import MIMEText
import time
import re
DEBUG = True
user_account = ""
user_password = "" # autherization code if using 163, qq, etc.
user_smtp_server = ""
user_smtp_port = 587
file_to_open = "" # currently only support csv file parsing
sent_list_file = ""
waiting_list, sent_list = {}, {}
content = ""
def send(mail, name, id, score):
msg = MIMEText(content.format(name, id, score), 'html')
msg['From'] = user_account
msg['Subject'] = "Midterm Score of SI131 Linear Algebra"
if DEBUG:
msg['To'] = user_account
else:
msg['To'] = mail
server.send_message(msg)
def connect(user_smtp_server, user_smtp_port, user_account, user_password):
server = smtplib.SMTP(user_smtp_server, user_smtp_port)
reply = server.ehlo()
if reply[0] == 250:
print('Conneted!', flush = True)
else:
print('Failed to connect!', flush = True)
exit(1)
server.starttls()
reply = server.login(user_account, user_password)
if reply[0] == 235:
print('Logged in!', flush = True)
else:
server.quit()
print('Failed to login!', flush = True)
exit(1)
return server
def read_waiting_list(file_to_open, waiting_list):
sent_list= {}
with open(file_to_open, newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for i, row in enumerate(reader):
entries = row[0].split(',')
id = entries[0]
name = entries[1]
email = entries[3]
score = entries[4]
waiting_list[i] = (id, name, email, score)
sent_list[i] = False
return waiting_list, sent_list
# print(name, email, score, sent_list[i])
def write_sent_list(waiting_list, sent_list):
with open("sent_list.txt", "w", encoding='utf8') as output:
for i in range(len(waiting_list)):
record = (', '.join(waiting_list[i]))+ ', ' + str(sent_list[i]) + '\n'
print(record)
output.write(record)
def read_sent_list(file_to_open, sent_list):
with open(file_to_open, newline='', encoding='utf-8') as verify:
reader = verify.readlines()
for i, row in enumerate(reader):
record = [x for x in re.split('[,\s]+', row) if x != '']
sent_list[i] = eval(record[4].title())
return sent_list
def print_list(waiting_list, sent_list):
for i in range(len(waiting_list)):
id, name, email, score = waiting_list[i]
print(name, email, score, sent_list[i])
# print(person)
def send_score(waiting_list, sent_list):
print('Total waiting: {}'.format(len(waiting_list)))
for i in range(len(waiting_list)):
if sent_list[i]:
print("Already sent to {}".format((waiting_list[i])[1]))
continue
if i % 25 == 0:
print('Inline waiting: {}'.format(len(waiting_list)-i))
if i == 0:
server = connect(user_smtp_server, user_smtp_port, user_account, user_password)
d = waiting_list[i]
while not sent_list[i]:
try:
print('Sending: {} ... '.format(d[2]), end = '', flush = True)
send(d[2], d[1], d[0], d[3])
print('Success!', flush = True)
except smtplib.SMTPSenderRefused:
print('Failed!', flush = True)
print('Waiting for additional timeout ... ', end = '', flush = True)
time.sleep(65)
server = connect(user_smtp_server, user_smtp_port, user_account, user_password)
print('Done.', flush = True)
except smtplib.SMTPServerDisconnected:
print('Failed: login expired!', flush = True)
connect(user_smtp_server, user_smtp_port, user_account, user_password)
print('reconnected!', flush = True)
else:
sent_list[i] = True
break
# print('checkpoint2', flush=True)
if i % 5 == 4:
print('Waiting for regular timeout ... ', end = '', flush = True)
time.sleep(65)
print('Done.', flush = True)
server = connect(user_smtp_server, user_smtp_port, user_account, user_password)
print('All sent.')
server.quit()