aboutsummaryrefslogtreecommitdiff
path: root/src/routes/contact/submit/+server.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes/contact/submit/+server.js')
-rw-r--r--src/routes/contact/submit/+server.js97
1 files changed, 97 insertions, 0 deletions
diff --git a/src/routes/contact/submit/+server.js b/src/routes/contact/submit/+server.js
new file mode 100644
index 0000000..eed95fe
--- /dev/null
+++ b/src/routes/contact/submit/+server.js
@@ -0,0 +1,97 @@
+import 'dotenv/config';
+import * as nodemailer from 'nodemailer';
+import { continueRetryUntilValidation } from '$lib/utils';
+
+class MistyMountainsMailer {
+ constructor(username, password, from, domain, port) {
+ this.from = from;
+ this.username = username;
+ this.password = password;
+ this.domain = domain;
+ this.port = port;
+
+ this.transporter = nodemailer.createTransport({
+ host: this.domain,
+ port: this.port,
+ auth: {
+ user: this.username,
+ pass: this.password
+ },
+ requireTLS: true,
+ tls: {
+ rejectUnauthorized: true
+ }
+ });
+ }
+
+ async sendMail(to, subject, message) {
+ const mail = {
+ from: this.from,
+ subject,
+ html: message,
+ to
+ };
+
+ return !!(await continueRetryUntilValidation(async () => {
+ const { messageId } = await this.transporter.sendMail(mail);
+ return messageId;
+ }));
+ }
+}
+
+export async function POST({ request }) {
+ const body = await request.json();
+ const { HCAPTCHA_SECRET, FORM_TO_EMAIL } = process.env;
+ const mailer = new MistyMountainsMailer(
+ process.env.SMTP_USERNAME,
+ process.env.SMTP_PASSWORD,
+ process.env.FROM_EMAIL,
+ process.env.SMTP_SERVER,
+ Number(process.env.SMTP_PORT)
+ );
+
+ const captchaVerified = await fetch(
+ `https://hcaptcha.com/siteverify?response=${body.captchaToken}&secret=${HCAPTCHA_SECRET}`,
+ {
+ method: 'POST'
+ }
+ )
+ .then((res) => res.json())
+ .then((json) => json.success)
+ .catch(() => false);
+
+ if (!captchaVerified) {
+ return new Response(JSON.stringify({ error: 'Captcha verification failed' }), {
+ status: 400,
+ headers: { 'Content-Type': 'application/json' }
+ });
+ }
+
+ const text = `<p>New MMT Message</h1>
+<p>Name: ${body.name}</p>
+<p>Phone Number: ${body.phone || 'Not Given'}</p>
+<p>Email: ${body.email}</p>
+<hr>
+<br>
+<p>${body.message}</p>`;
+
+ const messageSent = await mailer
+ .sendMail(FORM_TO_EMAIL, `[MMT-FORM]: New Message From ${body.name}`, text)
+ .then(() => true)
+ .catch((error) => {
+ console.error(error);
+ return false;
+ });
+
+ if (!messageSent) {
+ return new Response(JSON.stringify({ error: 'Message could not be sent' }), {
+ status: 500,
+ headers: { 'Content-Type': 'application/json' }
+ });
+ }
+
+ return new Response(JSON.stringify({ success: true }), {
+ status: 200,
+ headers: { 'Content-Type': 'application/json' }
+ });
+}