aboutsummaryrefslogtreecommitdiffstats
path: root/routes/email.js
blob: 1b5518648148c2fbbc89ebdce0585614016d9aff (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
import { bail } from "$lib/utils";
import got from "got";
import config from "$config";
import sendgrid from "@sendgrid/mail";

export default {
  async send({ body }, res) {
    let { token: response } = body;
    let { recaptcha: secret } = config;
    let { success } = await got
      .post("https://www.google.com/recaptcha/api/siteverify", {
        form: {
          secret,
          response,
        },
      })
      .json();

    if (success) {
      delete body.token;
      sendgrid.setApiKey(config.sendgrid);
      const msg = {
        to: "[email protected]",
        from: "[email protected]",
        subject: body.subject || "Email Signup",
        text: JSON.stringify(body),
        html: JSON.stringify(body),
      };

      await sendgrid.send(msg);
      res.send({ ok: true });
    } else {
      bail(res, "failed captcha");
    }
  },
};