Here we are (I’ve removed some details just for data protection, if you need any of them, let me know):
Backend:
Email.jsw:
//email.jsw
import {sendWithService} from ‘backend/sendGrid’;
export function sendEmail(subject, body) {
const key = “KEY HERE”;
const sender = “EMAIL ADDRESS HERE”;
const recipient = “EMAIL ADDRESS HERE”;
return sendWithService(key, sender, recipient, subject, body);
}
export function sendEmailWithRecipient(subject, body, recipient) {
const key = “KEY HERE”;
const sender = “EMAIL ADDRESS HERE”;
return sendWithService(key, sender, recipient, subject, body);
}
sendGrid.js:
//sendGrid.js
import {fetch} from ‘wix-fetch’;
export function sendWithService(key, sender, recipient, subject, body) {
const url = “https://api.sendgrid.com/api/mail.send.json”;
const headers = {
“Authorization”: "Bearer " + key,
“Content-Type”: “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:
// For full API documentation, including code examples, visit Velo API Reference - Wix.com
$w.onReady( function () {
//TODO: write your page related code here…
});
import {sendEmail, sendEmailWithRecipient} from ‘backend/email’;
$w.onReady( function () {
$w(“#dataset1”).onAfterSave(sendFormData);
});
function sendFormData() {
const subject = Thanks **for** your order!
;
const body = We've received your order and it'll be **with** you shortly! \rName: ${$w("#input1").value} \rEmail Address: ${$w("#input4").value} \rContact Number: ${$w("#input3").value} \rSuite: ${$w("#dropdown1").value} \rChoose a time: ${$w("#dropdown2").value} \rSpecial Requests: ${$w("#textBox1").value}
;
const recipient = $w(“#input4”).value;
sendEmailWithRecipient(subject, body, recipient)
.then(response => console.log(response));
sendEmail(subject, body)
.then(response => console.log(response));
}
The error showing on the page in preview is:
Cannot find module ‘backend/email’ in ‘public/pages/np106.js’
Loading the code for the Breakfast page. To debug this code, open np106.js in Developer Tools.
Thanks so much for your help, I really appreciate it!