1-
1+ // This file is a SvelteKit action that is called when the contact form is submitted.
22import type { Actions } from "@sveltejs/kit" ;
33import { fail } from "@sveltejs/kit"
44
5-
5+ // Form specific imports
66import z from "zod"
77import sgMail from "@sendgrid/mail/index" ;
8- // THE FOLLOWING TWO LINES ARE NOT WORKING. STILL WORKING ON THIS.
9- // import { env } from '$env/dynamic/private';
10- // const { SENDGRID_API_KEY, SENDGRID_EMAIL_FROM, VITE_CONTACT_EMAIL } = env;
118
12- // This needs to be picked up from the .env file. I'm not sure how to do this. Seems broken at the moment.
13- const SENDGRID_API_KEY = "<EXAMPLE_KEY>" ;
14- const SENDGRID_EMAIL_FROM = "no-reply@example.com" ;
15- const VITE_CONTACT_EMAIL = "username+contactform@example.com" ;
9+ // Import the SendGrid API key and email addresses from the private environment variables.
10+ import { SENDGRID_API_KEY , EMAIL_DESTINATION , EMAIL_REPLY_TO } from '$env/static/private' ;
1611
17- // Refer to https://zod.dev/ for building a desired data schema for the data we want to parse and optionally .
18- const contactSchema = z . object ( {
12+ // Refer to https://zod.dev/ for details about how to setup a validation schema .
13+ const contactFormSchema = z . object ( {
1914 fname : z . string ( { required_error : "First name is required" } ) . min ( 1 ) ,
2015 lname : z . string ( { required_error : "Last name is required" } ) . min ( 1 ) ,
2116 email : z . string ( { required_error : "Valid email address required" } ) . email ( ) ,
@@ -25,20 +20,13 @@ const contactSchema = z.object({
2520export const actions : Actions = {
2621 default : async ( { request } ) => {
2722
28- sgMail . setApiKey ( SENDGRID_API_KEY ) ;
29-
3023 const formData = Object . fromEntries ( await request . formData ( ) ) ;
31-
32- // Remove empty fields from the form data.
33- Object . keys ( formData ) . forEach ( key => formData [ key ] === '' && delete formData [ key ] ) ;
34-
35- const form = contactSchema . safeParse ( formData ) ;
36- const { ...rest } = formData ;
24+ const form = contactFormSchema . safeParse ( formData ) ;
3725
3826 // If there is a problem with the form data, return the error.
39- if ( ! form . success ) {
27+ if ( ! form . success ) { // If not successful, return the error.
4028 return fail ( 400 , {
41- data : rest ,
29+ data : formData ,
4230 errors : form . error . flatten ( ) . fieldErrors ,
4331 body : {
4432 message : 'Please review the form and try again.' ,
@@ -48,10 +36,12 @@ export const actions: Actions = {
4836 }
4937
5038 try {
39+ sgMail . setApiKey ( SENDGRID_API_KEY ) ;
40+
5141 await sgMail . send ( {
52- to : VITE_CONTACT_EMAIL , // Destination email address (Who is responsible responding to emails)
53- replyTo : form . data . email , // Email of visitor requesting contact .
54- from : SENDGRID_EMAIL_FROM , // Email of sender (verified email address or domain at SendGrid)
42+ to : EMAIL_DESTINATION , // SendGrid verified email or domain where email is send "from". This could be a reply- to address, or a sales, support, or other address.
43+ from : EMAIL_REPLY_TO , // The email address that will be displayed as the sender .
44+ replyTo : form . data . email , // The email provided provided by the visitor.
5545 subject : 'Contat form submission' ,
5646 text : 'First Name: ' + form . data . fname + '\n' +
5747 'Last Name: ' + form . data . lname + '\n' +
@@ -64,7 +54,7 @@ export const actions: Actions = {
6454 if ( err . response ) {
6555 console . error ( err . response . body ?. errors )
6656 return fail ( 400 , {
67- data : rest ,
57+ data : formData ,
6858 body : {
6959 message : "Upstream Error: " + err . response . body ?. errors [ 0 ] ?. message ,
7060 classes : 'text-2xl text-red-500'
0 commit comments