This repository has been archived by the owner on Jul 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
new_student.py
69 lines (52 loc) · 2.06 KB
/
new_student.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
from gspread import authorize
from oauth2client.service_account import ServiceAccountCredentials
from numpy import array
import json
import os
# permissions needed from google account to access spreadsheet
scope = [
'https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive'
]
creds = ServiceAccountCredentials.from_json_keyfile_name('./credentials.json', scope)
client = authorize(creds)
# open settings file
settings_file = open(os.path.join('./settings.json'), 'r')
SETTINGS = json.load(settings_file)
# find the worksheet in google drive by its name
# student roster is the second sheet
ROSTER_SHEET = 1
sheet = client.open(SETTINGS['sheet_title']).worksheets()[ROSTER_SHEET]
# get the most recent student on the roster
students = array(sheet.get_all_values())
new_student = students[len(students) - 1]
student_first_name = new_student[2].split(' ')[0]
student_email = new_student[3]
email_body_file = open(os.path.join('./templates/new_student.html'), 'r')
email_body = email_body_file.read()
# replace contents in template with student and tutor info
email_body = email_body.replace('{{name}}', student_first_name)
email_body = email_body.replace('{{calendly_link}}', SETTINGS['calendly_link'])
email_body = email_body.replace('{{tutor_name}}', SETTINGS['tutor_name'])
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import *
sg = SendGridAPIClient(api_key=SETTINGS['sendgrid_key'])
from_email = Email(SETTINGS['email'], SETTINGS['tutor_name'])
to_email = To(student_email)
subject = "Coding Bootcamp: Tutorial Available"
content = Content("text/html", email_body)
# Emails need to always CC centraltutorsupport
cc_email = Email('[email protected]')
p = Personalization()
p.add_to(to_email)
p.add_cc(cc_email)
# send an email to the new student
mail = Mail(from_email, to_email, subject, content)
mail.add_personalization(p)
response = sg.client.mail.send.post(request_body=mail.get())
print(email_body)
print(response.status_code)
print(response.body)
print(response.headers)
email_body_file.close()
settings_file.close()