-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b49e9e3
commit a7f2e30
Showing
1 changed file
with
22 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} | ||
}; | ||
|
||
}, | ||
} |