From fe62cadd6fb4a7e49f7d59401fc74ae0411e32a7 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 26 Aug 2024 21:17:43 -0700 Subject: typescript --- routes/email.ts | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 routes/email.ts (limited to 'routes/email.ts') diff --git a/routes/email.ts b/routes/email.ts new file mode 100644 index 00000000..63c88ab0 --- /dev/null +++ b/routes/email.ts @@ -0,0 +1,61 @@ +import { bail } from "$lib/utils"; +import got from "got"; +import config from "$config"; +import { l, warn } from "$lib/logging"; +import { SESClient } from "@aws-sdk/client-ses"; +import { SendEmailCommand } from "@aws-sdk/client-ses"; + +export default { + async send({ body }, res) { + try { + let { email, message, username, token: response } = body; + + let Charset = "UTF-8"; + + let { recaptcha: secret } = config; + let { success } = await got + .post("https://www.google.com/recaptcha/api/siteverify", { + form: { + secret, + response, + }, + }) + .json(); + + if (success || response === config.adminpass) { + delete body.token; + + warn("support request from", email); + let client = new SESClient({ region: "us-east-2" }); + await client.send( + new SendEmailCommand({ + Destination: { + CcAddresses: [], + ToAddresses: [config.support], + }, + Message: { + Body: { + Html: { Charset, Data: message.replace(/\n/g, "
") }, + Text: { Charset, Data: message }, + }, + Subject: { + Charset, + Data: + body.subject || + "Support Request" + (username ? " From " + username : ""), + }, + }, + ReplyToAddresses: [email], + Source: config.support, + }), + ); + + res.send({ ok: true }); + } else { + bail(res, "failed captcha"); + } + } catch (e) { + console.log(e); + } + }, +}; -- cgit v1.2.3