Skip to content

Commit

Permalink
fix: move resend inside function
Browse files Browse the repository at this point in the history
  • Loading branch information
kettei-sproutty committed Jun 13, 2024
1 parent b49e9e3 commit a7f2e30
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions src/routes/(vimlike)/contacts/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,51 @@


import { fail } from '@sveltejs/kit';
import type { Actions } from './$types';
import { RESEND_API } from '$env/static/private';
import { z } from "zod";
import { Resend } from 'resend';

const resend = new Resend(RESEND_API);
import { RESEND_API } from '$env/static/private'
import { fail } from '@sveltejs/kit'
import { Resend } from 'resend'
import { z } from 'zod'
import type { Actions } from './$types'

const contactMeSchema = z.object({
name: z.string().min(3).max(255),
email: z.string().email(),
subject: z.string().min(3).max(255),
message: z.string().min(10).max(5000),
});
})

export const actions: Actions = {
"send-email": async (event) => {
const formData = await event.request.formData();
const data = Object.fromEntries(formData.entries());
'send-email': async (event) => {
const resend = new Resend(RESEND_API)
const formData = await event.request.formData()
const data = Object.fromEntries(formData.entries())

const contactMe = contactMeSchema.safeParse(data);
const contactMe = contactMeSchema.safeParse(data)

if (!contactMe.success) {
const issues = contactMe.error.issues.reduce<Record<string, string>>((acc, issue) => {
acc[issue.path.join('.')] = issue.message;
return acc;
}, {});
acc[issue.path.join('.')] = issue.message
return acc
}, {})

return fail(400, { issues });
return fail(400, { issues })
}

const html = `<span>Email from: ${contactMe.data.name} (${contactMe.data.email})</span>
<br>
<span>Subject: ${contactMe.data.subject}</span>
<br>
<span>Message: ${contactMe.data.message}</span>`;
<span>Message: ${contactMe.data.message}</span>`

const { error } = await resend.emails.send({
from: '[email protected]',
to: ['[email protected]'],
subject: contactMe.data.subject,
html
});
html,
})

if (error) {
console.error(error);
return fail(500, { issues: { api: "Error while sending e-mail." } });
console.error(error)
return fail(500, { issues: { api: 'Error while sending e-mail.' } })
}

return { success: true }
}
};

},
}

0 comments on commit a7f2e30

Please sign in to comment.