Hi, I’m trying to use SendGrid to send an email on form submission, but struggling to get it to work. I’m designing a website for a healthcare clinic, which employs a number of practitioners. I’ve got dynamic pages set up for each practitioner, with a contact form on the dynamic page. I’m using SendGrid because I want the practitioner to get a notification email when someone submits the contact form on their page. This is the code I’ve got so far…
SendGrid. jsw :
import sgMail from "@sendgrid/mail";
import {getSecret} from 'wix-secrets-backend';
export async function sendEmail(recipient, subject, sender, body){
const apiKey = await getSecret("SendGridAPI");
sgMail.setApiKey(apiKey);
const message = {
to:recipient,
from:sender,
subject:subject,
text:body
}
return sgMail.send(message)
}
Page code:
import {sendEmail} from 'backend/SendGrid'
$w.onReady(function () {
$w("#practitionerContactForm").onAfterSave(sendFormData);
});
function sendFormData() {
const message = {
subject: `You have a new message from website`,
recipient: $w("#dynamicDataset").getCurrentItem()["email"],
sender: "[email@address. com]",
body: `Hi! You've received a new message from: ${$w("#inputName").value}.
\rEmail address: ${$w("#inputEmail").value}
\rPhone number: ${$w("#inputPhone").value}
\rMessage: ${$w("#inputMessage").value}
\rPlease respond at your earliest convenience. Thanks, York Clinic.`
};
sendEmail (message.subject, message.recipient, message.body)
.then(response => console.log(response));
}
I’ve been using a YouTube tutorial by Wix as a basis for this (can’t post the link but it’s called ‘Third Party Integrations Webinar SendGrid | Velo by Wix’) as well as several other (quite old) posts from the velo forum.
I’ve been struggling with this for quite a while now, so if anyone can check my code/let me know what I’m doing wrong, it’d be really really appreciated!!
Many Thanks,
Philippa