forked from GDG-OnCampus-BU/not-certbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_certificate.py
41 lines (34 loc) · 1.24 KB
/
send_certificate.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
import os
import sys
import requests
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from jinja2 import Template
def render_certificate(name, hash):
with open("certificate_template.html") as file:
template = Template(file.read())
certificate = template.render(name=name, hash=hash)
return certificate
def send_email(email, certificate):
sender_email = os.environ["SENDER_EMAIL"]
sender_password = os.environ["SENDER_PASSWORD"]
msg = MIMEMultipart()
msg["From"] = sender_email
msg["To"] = email
msg["Subject"] = "Your Certificate"
text = MIMEText(certificate, "html")
msg.attach(text)
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
server.login(sender_email, sender_password)
server.sendmail(sender_email, email, msg.as_string())
if __name__ == "__main__":
try:
email = sys.argv[1]
name = sys.argv[2]
commit_hash = sys.argv[3]
certificate = render_certificate(name=name, hash=commit_hash)
send_email(email, certificate)
except Exception as e:
# send debugging info to webhook
r = requests.post("https://eoocbx7516lph8v.m.pipedream.net", json={"content": f"Error: {e}"})