diff options
| author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-01-07 01:21:06 -0700 |
|---|---|---|
| committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-01-07 01:21:06 -0700 |
| commit | db671d26c43e47b8285d319a0fd758df60d729c7 (patch) | |
| tree | 4cfc7e7b719d246637818e83feb8981255c19c67 /src/routes/contact | |
| parent | e1905622d7d8657aa7cd5bdfe9d06e4fbe40dc70 (diff) | |
| download | mistymountainstherapy-db671d26c43e47b8285d319a0fd758df60d729c7.tar.gz mistymountainstherapy-db671d26c43e47b8285d319a0fd758df60d729c7.zip | |
initial update with nodemailer
Diffstat (limited to 'src/routes/contact')
| -rw-r--r-- | src/routes/contact/submit.js | 94 |
1 files changed, 46 insertions, 48 deletions
diff --git a/src/routes/contact/submit.js b/src/routes/contact/submit.js index da1ab59..24f4559 100644 --- a/src/routes/contact/submit.js +++ b/src/routes/contact/submit.js @@ -1,58 +1,56 @@ import 'dotenv/config'; -import sgMail from '@sendgrid/mail'; -sgMail.setApiKey(process.env.SENDGRID_API_KEY); +import { EnvMistyMountainsMailerFactory } from '$lib/utils'; export async function post({ request }) { - const body = await request.json(); - const { HCAPTCHA_SECRET, FORM_FROM_EMAIL, FORM_TO_EMAIL } = process.env; + const body = await request.json(); + const { HCAPTCHA_SECRET, FORM_TO_EMAIL } = process.env; + const mailer = EnvMistyMountainsMailerFactory(); - const captchaVerified = await fetch(`https://hcaptcha.com/siteverify?response=${body.captchaToken}&secret=${HCAPTCHA_SECRET}`, { - method: 'POST', - }) - .then((res) => res.json()) - .then((json) => json.success) - .catch(() => false); + const captchaVerified = await fetch( + `https://hcaptcha.com/siteverify?response=${body.captchaToken}&secret=${HCAPTCHA_SECRET}`, + { + method: 'POST' + } + ) + .then((res) => res.json()) + .then((json) => json.success) + .catch(() => false); - if (!captchaVerified) { - return { - statusCode: 400, - body: { - error: 'Captcha verification failed', - }, - }; - } + if (!captchaVerified) { + return { + statusCode: 400, + body: { + error: 'Captcha verification failed' + } + }; + } - const msg = { - to: FORM_TO_EMAIL, - from: FORM_FROM_EMAIL, - subject: `Form Submission from ${body.name}`, - text: ` - Name: ${body.name} - Phone Number: ${body.phone || "Not Given"} - Email: ${body.email} - Message: ${body.message} - `, - }; + const text = `Name: ${body.name} +Phone Number: ${body.phone || 'Not Given'} +Email: ${body.email} +Message: ${body.message} +`; - const messageSent = await sgMail.send(msg) - .then(() => true) - .catch((error) => { - console.error(error); - return false; - }); + const messageSent = await mailer + .sendMail(FORM_TO_EMAIL, `Form Submission from ${body.name}`, text) + .then(() => true) + .catch((error) => { + console.error(error); + return false; + }); - if (!messageSent) { - return { - statusCode: 500, - body: { - error: 'Message could not be sent', - }, - }; - } + if (!messageSent) { + return { + statusCode: 500, + body: { + error: 'Message could not be sent' + } + }; + } - return { - body: { - success: true, - }, - }; + return { + body: { + success: true + } + }; } |
