switch to discord hook for contact

This commit is contained in:
Benjamin Palko 2026-01-26 10:39:02 -05:00
parent 68c5fc73f4
commit 832ef7f411
5 changed files with 53 additions and 62 deletions

View file

@ -1,84 +1,59 @@
import { env } from "$env/dynamic/private";
import { fail } from "@sveltejs/kit";
import { createTransport } from "nodemailer";
import type { Actions } from "./$types";
import { logger } from "$lib/server/logger";
import { sendContactMessage } from '$lib/server/discord';
import { logger } from '$lib/server/logger';
import { fail } from '@sveltejs/kit';
import type { Actions } from './$types';
export const actions = {
default: async ({ request }) => {
const form = await request.formData();
if (!form.has("name")) {
if (!form.has('name')) {
return fail(400, { name: null, missing: true });
}
if (!form.has("email")) {
if (!form.has('email')) {
return fail(400, { email: null, missing: true });
}
if (!form.has("subject")) {
if (!form.has('subject')) {
return fail(400, { subject: null, missing: true });
}
if (!form.has("message")) {
if (!form.has('message')) {
return fail(400, { message: null, missing: true });
}
const name = form.get("name");
if (typeof name !== "string") {
const name = form.get('name');
if (typeof name !== 'string') {
return fail(400, { name, incorrect: true });
}
const email = form.get("email");
if (typeof email !== "string") {
const email = form.get('email');
if (typeof email !== 'string') {
return fail(400, { email, incorrect: true });
}
const subject = form.get("subject");
if (typeof subject !== "string") {
const subject = form.get('subject');
if (typeof subject !== 'string') {
return fail(400, { subject, incorrect: true });
}
const message = form.get("message");
if (typeof message !== "string") {
const message = form.get('message');
if (typeof message !== 'string') {
return fail(400, { message, incorrect: true });
}
const transport = createTransport({
host: env.MAIL_TRANSPORT_HOST,
port: isNaN(Number(env.MAIL_TRANSPORT_PORT))
? 587
: Number(env.MAIL_TRANSPORT_PORT),
auth: {
user: env.MAIL_TRANSPORT_USER,
pass: env.MAIL_TRANSPORT_PASS,
},
});
try {
const info1 = await transport.sendMail({
from: "no-reply@benjaminpalko.ca",
to: email,
subject: `Thank you for reaching out`,
text: `Hello ${name}!\n\nThank you for reaching out! I will be sure to get back to you ASAP.\n\nCheers,\nBenjamin`,
});
const info2 = await transport.sendMail({
from: email,
to: "contact@benjaminpalko.ca",
subject: subject,
text: message,
});
const response = await sendContactMessage(name, email, subject, message);
return {
response: {
accepted: [...info1.accepted, ...info2.accepted],
rejected: [...info1.rejected, ...info2.rejected],
},
ok: response.ok
}
};
} catch (error) {
logger.error(error);
fail(400, { incorrect: true });
}
},
}
} satisfies Actions;

View file

@ -14,8 +14,6 @@
let loading = $state(false);
let { form }: PageProps = $props();
$inspect(form);
</script>
<form
@ -33,19 +31,19 @@
<h2 class="col-span-full text-center text-4xl font-light">Reach out! 🤝</h2>
<div class="col-span-full">
{#if form?.response?.rejected.length ?? 0 > 0}
<div role="alert" class="alert alert-error w-full">
<CircleX />
{#if form?.incorrect}
<div role="alert" class="alert alert-warning w-full">
<TriangleAlert />
<span>Ruh roh raggy!</span>
</div>
{:else if form?.response?.accepted.length ?? 0 > 0}
{:else if form?.response?.ok}
<div role="alert" class="alert alert-success w-full">
<CircleCheck />
<span>Your message has been sent successfully!</span>
</div>
{:else if form?.incorrect}
<div role="alert" class="alert alert-warning w-full">
<TriangleAlert />
{:else if form?.response?.ok === false}
<div role="alert" class="alert alert-error w-full">
<CircleX />
<span>Ruh roh raggy!</span>
</div>
{/if}