I am not having luck with the code provided in the “Velo Tutorial: Sending an Email on Form Submission” ( Velo Tutorial: Sending an Email on Form Submission | Help Center | Wix.com ) and could use some help. I have created a custom form using input fields and a “Submit” button that, when clicked, saves the input data to a form dataset. All of that works fine - the data saves to the dataset like it’s supposed to. I would like to have an email sent when the “SUBMIT” button is clicked alerting me that a new request has been submitted on the website.
Here is the code I have:
email.jsw (copied from tutorial code, with exception of not using Secrets Manager):
//email.jsw
import {sendWithService} from 'backend/sendGrid';
export function sendEmail(body) {
const key = "MY_KEY_INSERTED_HERE";
const sender = "SENDER_EMAIL_INSERTED_HERE";
const recipient = "RECIPIENT_EMAIL_INSERTED_HERE";
return sendWithService(key, sender, recipient, subject, body);
}
sendGrid.js (copied from tutorial code exactly)
//sendGrid.js
import {fetch} from 'wix-fetch';
export function sendWithService(key, sender, subject, body) {
const url = "https://api.sendgrid.com/api/mail.send.json";
const headers = {
"Authorization": "Bearer" + key,
"Content-Type": "application/application/x-www.form/urlencoded"
};
const data = 'from=${sender}&to=${recipient}&subject=${subject}&text=${body}';
const request = {
"method": "post",
"headers": headers,
"body": data
};
return fetch(url, request)
.then(response => response.json());
}
Page Code:
import {sendEmail} from 'backend/email';
$w.onReady(function () {
$w("#announcerequestDataset").onAfterSave(sendNewRequestEmail);
});
function sendNewRequestEmail() {
const subject = 'New Announcement Request Submitted';
const body = 'A new announcement request has been received on the website.';
sendEmail(subject, body)
.then(response => console.log(response));
}
Any ideas as to why this isn’t working? Thanks!