switch to discord hook for contact
This commit is contained in:
parent
68c5fc73f4
commit
832ef7f411
5 changed files with 53 additions and 62 deletions
26
src/lib/server/discord.ts
Normal file
26
src/lib/server/discord.ts
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import { DISCORD_CONTACT_WEBHOOK } from '$env/static/private';
|
||||
|
||||
export async function sendContactMessage(
|
||||
name: string,
|
||||
email: string,
|
||||
subject: string,
|
||||
message: string
|
||||
) {
|
||||
const form = new FormData();
|
||||
|
||||
// We need a better way than this to format a string to markdown
|
||||
const content = `
|
||||
# Someone is reaching out!
|
||||
|
||||
From: ${name} - [${email}](mailto:${email})
|
||||
> **Subject: ${subject}**
|
||||
> ${message}
|
||||
`
|
||||
.split('\n')
|
||||
.map((str) => str.trim())
|
||||
.join('\n');
|
||||
|
||||
form.append('content', content);
|
||||
|
||||
return await fetch(DISCORD_CONTACT_WEBHOOK, { method: 'POST', body: form });
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue