aboutsummaryrefslogtreecommitdiffstats
path: root/routes/email.js
blob: 63c88ab0625f76eba3c30f8cf17be9d7d02458c4 (plain) (blame)
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
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, "<br>") },
                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);
    }
  },
};