-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_email_lambda.py
189 lines (169 loc) · 4.58 KB
/
create_email_lambda.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import os
from base64 import b64decode
from urllib.parse import parse_qs
import boto3
from botocore.exceptions import ClientError
# In shell: echo -n "username:pass" | base64
AUTH_TOKEN = os.getenv("AUTH_TOKEN")
# us-east-1
REGION = os.getenv("REGION")
# domain.tld
DOMAIN = os.getenv("DOMAIN")
UNATHED_RESP = {
"statusCode": 401,
"statusDescription": "Unauthorized",
"headers": {
"WWW-Authenticate": "Basic realm=\"Private Relay\", charset=\"UTF-8\""
}
}
BASE = """
<!doctype html>
<html>
<head>
<style type="text/css">
html {{
line-height:1.15;
}}
body {{
margin: 1em;
}}
h1 {{
font-size: 2em;
margin: .67em 0;
}}
.form input[type="text"],
.form input[type="email"],
.form textarea {{
padding: 0.5em 0.6em;
display: inline-block;
border: 1px solid #ccc;
box-shadow: inset 0 1px 3px #ddd;
border-radius: 4px;
vertical-align: middle;
box-sizing: border-box;
width: 600px;
}}
.form input[type="text"]:focus,
.form input[type="email"]:focus,
.form textarea:focus {{
outline: 0;
border-color: #129FEA;
}}
.form label {{
margin: 0.5em 0 0.2em;
}}
.form fieldset {{
margin: 0;
padding: 0.35em 0 0.75em;
border: 0;
}}
.form legend {{
display: block;
width: 100%;
padding: 0.3em 0;
margin-bottom: 0.3em;
color: #333;
border-bottom: 1px solid #e5e5e5;
}}
.form-stacked input[type="text"],
.form-stacked input[type="email"],
.form-stacked label,
.form-stacked textarea {{
display: block;
margin: 0.25em 0;
}}
.button {{
font-family: inherit;
font-size: 100%;
padding: 0.5em 1em;
color: rgba(0, 0, 0, 0.80);
border: none rgba(0, 0, 0, 0);
background-color: #E6E6E6;
text-decoration: none;
border-radius: 2px;
}}
.button-hover,
.button:hover,
.button:focus {{
background-image: linear-gradient(transparent, rgba(0,0,0, 0.05) 40%, rgba(0,0,0, 0.10));
}}
.button:focus {{
outline: 0;
}}
.button-active,
.button:active {{
box-shadow: 0 0 0 1px rgba(0,0,0, 0.15) inset, 0 0 6px rgba(0,0,0, 0.20) inset;
border-color: #000;
}}
</style>
<title>Private Relay - Create</title>
</head>
<body>
<h1>Private Relay - Create</h1>
{content}
</body>
<html>
"""
FORM_CONTENT = """
<form action="/send" method="POST" class="form form-stacked">
<fieldset>
<legend>Send email</legend>
<label for="from">From (@{domain} will automatically be appended)</label>
<input type="email" id="from" name="from" placeholder="someone@{domain}" />
<label for="to">To</label>
<input type="email" id="to" name="to" placeholder="[email protected]" />
<label for="subject">Subject</label>
<input type="text" id="subject" name="subject" placeholder="Hi, how are you?" />
<label for="body">Body</label>
<textarea id="body" name="body" rows="40" cols="40"></textarea>
<button type="submit" class="button">Send</button>
</fieldset>
</form>
"""
SEND_CONTENT = """
<div>{message}</div>
<div><a href="/">Back to create page</a></div>
"""
def send_email(to_addr, from_addr, subject, body):
from_addr = f"{from_addr}@{DOMAIN}"
return boto3.client("sesv2", REGION).send_email(
FromEmailAddress=from_addr,
Destination={"ToAddresses": [to_addr,]},
Content={
"Simple": {
"Subject": {"Data": subject, "Charset": "utf-8"},
"Body": {"Text": {"Data": body, "Charset": "utf-8"}},
}
}
)
class AuthError(Exception):
pass
def do_auth(event):
try:
if event["headers"]["authorization"].split(" ")[1] == AUTH_TOKEN:
return True
else:
raise AuthError()
except:
raise AuthError()
def lambda_handler(event, context):
try:
do_auth(event)
except AuthError:
return UNATHED_RESP
if event["routeKey"] == "POST /send":
resp_content = "Message sent"
data = {k.decode(): v for k, v in parse_qs(b64decode(event["body"])).items()}
from_addr = data.get("from")[0].decode()
to_addr = data.get("to")[0].decode()
subject = "".join([x.decode() for x in data.get("subject")])
body = "".join([x.decode() for x in data.get("body")])
try:
resp = send_email(to_addr, from_addr, subject, body)
resp_content = f"""Message with Message-ID '{resp["MessageId"]} sent!'"""
except ClientError as e:
resp_content = e.response["Error"]["Message"]
body = BASE.format(content=SEND_CONTENT.format(message=resp_content))
else:
body = BASE.format(content=FORM_CONTENT.format(domain=DOMAIN))
return {"statusCode": 200, "body": body, "headers": {"Content-Type": "text/html"}}