-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
88 lines (73 loc) · 2.92 KB
/
main.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
import base64
import smtplib
import ssl
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from google.cloud import secretmanager
RECIPIENT = "me@seanhofer.com"
PROJECT_ID = "coherent-coder-193013"
CLIENT = secretmanager.SecretManagerServiceClient()
def contact(request):
"""Handles Contact Form requests.
Args:
request (flask.Request): HTTP request object.
Returns:
The response text.
"""
USER = get_secret("seanbot-user")
PASS = get_secret("seanbot-pass")
ALLOWED_DOMAINS = get_secret("contact-form-allowed-domains")
SMTP_DOMAIN = get_secret("smtp-domain")
SMTP_PORT = get_secret("smtp-port")
headers = {}
if request.headers['origin'] in ALLOWED_DOMAINS:
headers = {
'Access-Control-Allow-Origin': request.headers['origin']
}
fields = {}
data = request.form.to_dict()
for field in data:
fields[field] = data[field]
if not has_required_fields(fields):
return (f'Missing required fields. Include name email and message.', 400, headers)
try:
msg = MIMEMultipart()
msg['Subject'] = f'Message from {fields["name"]} via seanhofer.com!'
msg['From'] = USER
msg['To'] = RECIPIENT
msg.add_header('reply-to', fields["email"])
msg.attach(MIMEText(fields["message"], 'plain'))
except Exception as e:
print("Error constructing message fields: {0}".format(str(e)))
return ("Error constructing message fields: {0}".format(str(e)), 500, headers)
if "attachments" in fields:
try:
b64_files = fields["attachments"].split(',')
file_names = fields["attachment_names"].split(',')
for index, b64_file in enumerate(b64_files):
bytes_file = base64.b64decode(b64_file)
part = MIMEApplication(bytes_file, Name=file_names[index])
part['Content-Disposition'] = f'attachment; filename="{file_names[index]}"'
msg.attach(part)
except:
print("Error attaching attachments. names: {0} attachments: {1}".format(
file_names, b64_files))
return ("Error attaching attachments. names: {0} attachments: {1}".format(file_names, b64_files), 500, headers)
try:
context = ssl.create_default_context()
with smtplib.SMTP(SMTP_DOMAIN, SMTP_PORT) as server:
server.starttls(context=context)
server.login(USER, PASS)
server.sendmail(USER, RECIPIENT, msg.as_string())
except Exception as e:
print("Error sending email: {0}".format(str(e)))
return ("Error sending email: {0}".format(str(e)), 500, headers)
return (f'success: "{msg.as_string()}"', 200, headers)
def has_required_fields(fields):
return 'name' in fields and 'email' in fields and 'message' in fields
def get_secret_path(secret_id):
return f"projects/{PROJECT_ID}/secrets/{secret_id}/versions/latest"
def get_secret(secret_id):
encoded = CLIENT.access_secret_version(request={"name": get_secret_path(secret_id)})
return encoded.payload.data.decode("UTF-8")